-
-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
253 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...components/waste_collection_schedule/waste_collection_schedule/source/stadt_bamberg_de.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from datetime import datetime | ||
from html.parser import HTMLParser | ||
|
||
import requests | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
from waste_collection_schedule.service.ICS import ICS | ||
|
||
# Source code based on rh_entsorgung_de.md | ||
TITLE = "Bamberg (City/Stadt)" | ||
DESCRIPTION = "Source for Bamberg (City/Stadt)." | ||
URL = "https://www.stadt.bamberg.de" | ||
|
||
|
||
TEST_CASES = { | ||
"Gartenstraße 2": { | ||
"street": "Gartenstraße", | ||
"house_number": 2, | ||
}, | ||
"Egelseestraße 41b": { | ||
"street": "Egelseestraße", | ||
"house_number": 114, | ||
"address_suffix": "a", | ||
}, | ||
} | ||
|
||
ICON_MAP = { | ||
"Restmuell": "mdi:trash-can", | ||
"Biomuell": "mdi:leaf", | ||
"Papier": "mdi:package-variant", | ||
"Gelber": "mdi:recycle", | ||
} | ||
|
||
|
||
API_URL = ( | ||
"https://ebbweb.stadt.bamberg.de/WasteManagementBamberg/WasteManagementServlet" | ||
) | ||
|
||
# Parser for HTML input (hidden) text | ||
|
||
|
||
class HiddenInputParser(HTMLParser): | ||
def __init__(self): | ||
super().__init__() | ||
self._args = {} | ||
|
||
@property | ||
def args(self): | ||
return self._args | ||
|
||
def handle_starttag(self, tag, attrs): | ||
if tag == "input": | ||
d = dict(attrs) | ||
if str(d["type"]).lower() == "hidden": | ||
self._args[d["name"]] = d["value"] if "value" in d else "" | ||
|
||
|
||
class Source: | ||
def __init__(self, street: str, house_number: int, address_suffix: str = ""): | ||
self._street = street | ||
self._hnr = house_number | ||
self._suffix = address_suffix | ||
self._ics = ICS() | ||
|
||
def fetch(self): | ||
now = datetime.now() | ||
entries = self.get_data(now.year) | ||
if now.month == 12: | ||
try: | ||
entries += self.get_data(now.year + 1) | ||
except Exception: | ||
pass | ||
return entries | ||
|
||
def get_data(self, year): | ||
session = requests.session() | ||
|
||
r = session.get( | ||
API_URL, | ||
params={"SubmitAction": "wasteDisposalServices", "InFrameMode": "TRUE"}, | ||
) | ||
r.raise_for_status() | ||
r.encoding = "utf-8" | ||
|
||
parser = HiddenInputParser() | ||
parser.feed(r.text) | ||
|
||
args = parser.args | ||
args["Ort"] = self._street[0].upper() | ||
args["Strasse"] = self._street | ||
args["Hausnummer"] = str(self._hnr) | ||
args["Hausnummerzusatz"] = self._suffix | ||
args["SubmitAction"] = "CITYCHANGED" | ||
# args["Zeitraum"] = f"Jahresübersicht {year}" | ||
r = session.post( | ||
API_URL, | ||
data=args, | ||
) | ||
r.raise_for_status() | ||
|
||
args["SubmitAction"] = "forward" | ||
for i in range(1, 10): | ||
args[f"ContainerGewaehlt_{i}"] = "on" | ||
r = session.post( | ||
API_URL, | ||
data=args, | ||
) | ||
r.raise_for_status() | ||
args["ApplicationName"] = "com.athos.nl.mvc.abfterm.AbfuhrTerminModel" | ||
args["SubmitAction"] = "filedownload_ICAL" | ||
r = session.post( | ||
API_URL, | ||
data=args, | ||
) | ||
r.raise_for_status() | ||
|
||
dates = self._ics.convert(r.text) | ||
|
||
entries = [] | ||
for d in dates: | ||
entries.append( | ||
Collection(d[0], d[1].strip(), ICON_MAP.get(d[1].strip().split()[0])) | ||
) | ||
return entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Bamberg (City/Stadt) | ||
|
||
Support for schedules provided by [Bamberg (City/Stadt)](https://www.stadt.bamberg.de), serving the city of Bamberg, Germany. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stadt_bamberg_de | ||
street: STREET | ||
house_number: HNR | ||
address_suffix: HNR_SUFFIX | ||
``` | ||
### Configuration Variables | ||
**street** | ||
*(string) (required)* | ||
**house_number** | ||
*(integer) (required)* | ||
**address_suffix** | ||
*(string) (optional) (default: "")* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stadt_bamberg_de | ||
args: | ||
street: "Egelseestraße" | ||
house_number: 114 | ||
address_suffix: "a" | ||
``` | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stadt_bamberg_de | ||
args: | ||
street: Gartenstraße | ||
house_number: 2 | ||
``` | ||
## How to get the source arguments | ||
These values are the location you want to query for. Make sure, they are spelled exactly as on <https://www.stadt.bamberg.de/B%C3%BCrgerservice/%C3%84mter/Bamberg-Service-/Abfallwirtschaft/Abfuhrtermine/>. Typos may result in an Exception. As `house_number` expects a numeric input, address suffixes have to be provided via the `address_suffix` argument. |
Oops, something went wrong.