New Source: cederbaum bs (#1359)

* added source

* added docs for source

* update info and readme

* remove added carrier return before line feed, Made street matching slightly more resilient

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Patrick-Oliver Groß
2023-10-28 14:18:15 +02:00
committed by GitHub
parent b831342fcc
commit d5b1bd960f
4 changed files with 126 additions and 1 deletions

View File

@@ -465,6 +465,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Burgenland (Landkreis)](/doc/source/app_abfallplus_de.md) / Abfall+ App: udb
- [Bürgerportal](/doc/source/buergerportal_de.md) / c-trace.de
- [C-Trace](/doc/source/c_trace_de.md) / c-trace.de
- [Cederbaum Braunschweig](/doc/source/cederbaum_de.md) / cederbaum.de
- [Cham Landkreis](/doc/ics/entsorgung_cham_de.md) / entsorgung-cham.de
- [Chemnitz (ASR)](/doc/source/hausmuell_info.md) / asr-chemnitz.de
- [Chiemgau Recycling - Landkreis Rosenheim](/doc/source/chiemgau_recycling_lk_rosenheim.md) / chiemgau-recycling.de

View File

@@ -0,0 +1,92 @@
import datetime
import json
import re
import requests
from bs4 import BeautifulSoup
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Cederbaum Braunschweig"
DESCRIPTION = "Cederbaum Braunschweig Paperimüll"
URL = "https://www.cederbaum.de"
TEST_CASES = {
"Hans-Sommer-Str": {"street": "Hans-Sommer-Str."},
"Adolfstr 31-42": {"street": "Adolfstr. 31-42"},
"Am Schwarzen Berge": {"street": "am Schwarzen Berge "},
}
API_URL = "https://www.cederbaum.de/blaue-tonne/abfuhrkalender"
ICON_MAP = {
"PAPER": "mdi:newspaper",
}
class Source:
def __init__(self, street):
self._street = street
self.page_source = None
self.street_id = None
self.collection_data = None
def fetch_page_source(self):
resp = requests.get(API_URL)
soup = BeautifulSoup(resp.text, "html.parser")
self.page_source = soup
def get_street_id(self):
if not self.page_source:
raise ValueError("No page source found")
select = self.page_source.find("select")
if not select:
raise ValueError("No <select> tag found")
options = select.find_all("option")
for option in options:
value = option.get("value")
text = option.get_text()
if text.lower().strip() == self._street.lower().strip():
self.street_id = value
break
def get_collection_data(self):
if not self.page_source:
raise ValueError("No page source found")
script_tags = self.page_source.find_all("script")
script_with_text = [tag for tag in script_tags if tag.string]
pattern = re.compile(r"var rate = (\{.*?\});")
for script_tag in script_with_text:
match = pattern.search(script_tag.string)
if match:
var_content = match.group(1)
self.collection_data = json.loads(var_content)
break
def fetch(self):
self.fetch_page_source()
self.get_street_id()
self.get_collection_data()
if not self.collection_data:
raise ValueError("No collection data found")
entries = []
waste_dates = self.collection_data[self.street_id]["Termine"]
for waste_date in waste_dates:
date = datetime.datetime.strptime(
waste_dates[waste_date]["Termin"], "%d.%m.%Y"
)
entries.append(
Collection(
date=date.date(),
t="Paper",
icon=ICON_MAP.get("PAPER"),
)
)
return entries

View File

@@ -0,0 +1,32 @@
# Cederbaum Braunschweig
Support for paper waste collection schedules provided by [Cederbaum Container GmbH](https://www.cederbaum.de/), serving the city of Braunschweig, Germany.
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: cederbaum_de
args:
street: STREET
```
### Configuration Variables
**street**
*(String) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: cederbaum_de
args:
street: "Hans-Sommer-Str."
```
## How to get the source argument
Find the parameter of your street using [https://www.cederbaum.de/blaue-tonne/abfuhrkalender](https://www.cederbaum.de/blaue-tonne/abfuhrkalender) and write them exactly like on the web page.

File diff suppressed because one or more lines are too long