Skip to content

Commit

Permalink
add Stadt Bamberg, Germany
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Oct 13, 2024
1 parent ac33b4d commit bee0bfd
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [Bad Wünnenberg](/doc/source/lobbe_app.md) / lobbe.app
- [Bad-König](/doc/source/reso_gmbh_de.md) / reso-gmbh.de
- [Balve](/doc/source/lobbe_app.md) / lobbe.app
- [Bamberg (City/Stadt)](/doc/source/stadt_bamberg_de.md) / stadt.bamberg.de
- [Bamberg (Landkreis)](/doc/ics/abfalltermine_bamberg_de.md) / abfalltermine-bamberg.de
- [Barnim](/doc/source/jumomind_de.md) / kreiswerke-barnim.de
- [Battenberg](/doc/source/lobbe_app.md) / lobbe.app
Expand Down
6 changes: 6 additions & 0 deletions custom_components/waste_collection_schedule/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -4907,6 +4907,12 @@
},
"id": "lobbe_app"
},
{
"title": "Bamberg (City/Stadt)",
"module": "stadt_bamberg_de",
"default_params": {},
"id": "stadt_bamberg_de"
},
{
"title": "Bamberg (Landkreis)",
"module": "ics",
Expand Down
24 changes: 24 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -4868,6 +4868,30 @@
},
"data_description": {}
},
"args_stadt_bamberg_de": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadt_bamberg_de.md",
"data": {
"calendar_title": "Kalender Titel",
"address_suffix": "Address Suffix",
"house_number": "Hausnummer",
"street": "Straße"
},
"data_description": {
"calendar_title": "Ein lesbarerer oder benutzerfreundlicherer Name für den Müllkalender. Wenn nichts angegeben wird, wird der Name der Quelle verwendet."
}
},
"reconfigure_stadt_bamberg_de": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadt_bamberg_de.md",
"data": {
"calendar_title": "Kalender Titel",
"address_suffix": "Address Suffix",
"house_number": "Hausnummer",
"street": "Straße"
},
"data_description": {}
},
"args_ics_abfalltermine_bamberg_de": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\n- Go to https://www.abfalltermine-bamberg.de/ and select your location. \n- Copy the link of the Herunterladen button below Digitaler Kalender.\n- Replace the `url` in the example configuration with this link.\n\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/ics/abfalltermine_bamberg_de.md",
Expand Down
24 changes: 24 additions & 0 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4871,6 +4871,30 @@
},
"data_description": {}
},
"args_stadt_bamberg_de": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadt_bamberg_de.md.",
"data": {
"calendar_title": "Calendar Title",
"address_suffix": "Address Suffix",
"house_number": "House Number",
"street": "Street"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used."
}
},
"reconfigure_stadt_bamberg_de": {
"title": "Reconfigure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadt_bamberg_de.md.",
"data": {
"calendar_title": "Calendar Title",
"address_suffix": "Address Suffix",
"house_number": "House Number",
"street": "Street"
},
"data_description": {}
},
"args_ics_abfalltermine_bamberg_de": {
"title": "Configure Source",
"description": "Configure your service provider. \n\n- Go to https://www.abfalltermine-bamberg.de/ and select your location. \n- Copy the link of the Herunterladen button below Digitaler Kalender.\n- Replace the `url` in the example configuration with this link.\n\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/ics/abfalltermine_bamberg_de.md.",
Expand Down
24 changes: 24 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -4868,6 +4868,30 @@
},
"data_description": {}
},
"args_stadt_bamberg_de": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadt_bamberg_de.md.",
"data": {
"calendar_title": "Nome Calendario",
"address_suffix": "Address Suffix",
"house_number": "Civico",
"street": "Strada"
},
"data_description": {
"calendar_title": "Puoi cambiare il nome del calendario della raccolta dei rifiuti, altrimenti di default verra' utilizzato il nome del tuo fornitore di servizi."
}
},
"reconfigure_stadt_bamberg_de": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Per maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadt_bamberg_de.md.",
"data": {
"calendar_title": "Nome Calendario",
"address_suffix": "Address Suffix",
"house_number": "Civico",
"street": "Strada"
},
"data_description": {}
},
"args_ics_abfalltermine_bamberg_de": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\n- Go to https://www.abfalltermine-bamberg.de/ and select your location. \n- Copy the link of the Herunterladen button below Digitaler Kalender.\n- Replace the `url` in the example configuration with this link.\n\n\nMaggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/ics/abfalltermine_bamberg_de.md.",
Expand Down
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
50 changes: 50 additions & 0 deletions doc/source/stadt_bamberg_de.md
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.
Loading

0 comments on commit bee0bfd

Please sign in to comment.