refactor egn_abfallkalender_de

This commit is contained in:
mampfes
2022-01-06 11:04:39 +01:00
parent e7a34e3e51
commit b10f50a71d
4 changed files with 56 additions and 47 deletions

View File

@@ -75,6 +75,7 @@ Currently the following service providers are supported:
- [AWIDO-online.de](./doc/source/awido_de.md)
- [Berlin-Recycling.de](./doc/source/berlin_recycling_de.md)
- [BSR.de / Berliner Stadtreinigungsbetriebe](./doc/source/bsr_de.md)
- [EGN-Abfallkalender.de](./doc/source/egn_abfallkalender_de.md)
- [Jumomind.de](./doc/source/jumomind_de.md)
- [Landkreis-Wittmund.de](./doc/source/landkreis_wittmund_de.md)
- [Muellmax.de](./doc/source/muellmax_de.md)

View File

@@ -1,80 +1,91 @@
import datetime
import urllib
import logging
from typing import Dict
import requests
from bs4 import BeautifulSoup
from requests.sessions import dispatch_hook
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "EGN Abfallkalender"
DESCRIPTION = "Source for EGN Abfallkalender"
URL = "https://www.egn-abfallkalender.de/kalender"
TEST_CASES: Dict[str, Dict[str, str]] = {
"Grevenbroich": {"city": "Grevenbroich" , "district": "Noithausen", "street": "Von-Immelhausen-Straße",
"housenumber": "12"},
"Dormagen": {"city": "Dormagen" , "district": "Hackenbroich", "street": "Aggerstraße",
"housenumber": "2"},
"Grefrath": {"city": "Grefrath" , "district": "Grefrath", "street": "An Haus Bruch",
"housenumber": "18"}
}
HEADERS = {
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"x-requested-with": "XMLHttpRequest",
TEST_CASES = {
"Grevenbroich": {
"city": "Grevenbroich",
"district": "Noithausen",
"street": "Von-Immelhausen-Straße",
"housenumber": 12,
},
"Dormagen": {
"city": "Dormagen",
"district": "Hackenbroich",
"street": "Aggerstraße",
"housenumber": 2,
},
"Grefrath": {
"city": "Grefrath",
"district": "Grefrath",
"street": "An Haus Bruch",
"housenumber": 18,
},
}
_LOGGER = logging.getLogger(__name__)
IconMap = {
"Grau": "mdi:trash-can",
"Gelb": "mdi:sack",
"Blau": "mdi:package-variant",
"Braun": "mdi:leaf",
}
class Source:
def __init__(self, city, district, street, housenumber):
self._city = city
self._district = district
self._street = street
self._housenumber = housenumber
self._iconMap = {
"grau": "mdi:trash-can",
"gelb": "mdi:sack",
"blau": "mdi:package-variant",
"braun": "mdi:leaf"
}
def fetch(self):
entries = []
s = requests.session()
r = s.get(URL)
soup = BeautifulSoup(r.text, features="html.parser")
tag = soup.find("meta", {"name": "csrf-token"})
if tag == None:
if tag is None:
return []
HEADERS["x-csrf-token"] = tag["content"]
post_data = urllib.parse.urlencode({"city": self._city, "district": self._district,
"street": self._street, "street_number": self._housenumber})
r = s.post(URL, data=post_data, headers=HEADERS)
headers = {"x-csrf-token": tag["content"]}
post_data = {
"city": self._city,
"district": self._district,
"street": self._street,
"street_number": self._housenumber,
}
r = s.post(URL, data=post_data, headers=headers)
data = r.json()
if data.get("error"):
for type, errormsg in data["errors"].items():
_LOGGER.error(f"{type} - {errormsg}")
return []
entries = []
for year, months in data["waste_discharge"].items():
for month, days in months.items():
for day, types in days.items():
date = datetime.datetime(year=int(year), month=int(month), day=int(day)).date()
date = datetime.datetime(
year=int(year), month=int(month), day=int(day)
).date()
for type in types:
color = data["trash_type_colors"].get(str(type).lower(), type)
icon = self._iconMap.get(color)
color = color.capitalize()
color = (
data["trash_type_colors"]
.get(str(type).lower(), type)
.capitalize()
)
entries.append(
Collection(
date, color, icon=icon
)
Collection(date=date, t=color, icon=IconMap.get(color))
)
return entries

View File

@@ -13,10 +13,9 @@ waste_collection_schedule:
district: DISTRICT_NAME
street: STREET_NAME
housenumber: HOUSE_NUMBER
days: MAX_DAYS
```
The arguments can be found above the calander after generating one [here](https://www.egn-abfallkalender.de/kalender#skill-setup-form). Select your city, street and enter your housenumber to show the schedule for your address. The complete information you need are displayed above the calander view in the format "für \<Street> \<Housenumber>, \<City> (\<District>)". See also examples below.
The arguments can be found above the calendar after generating one [here](https://www.egn-abfallkalender.de/kalender#skill-setup-form). Select your city, street and enter your housenumber to show the schedule for your address. The complete information you need are displayed above the calendar view in the format `für <street> <housenumber>, <city> (<district>)`. See also examples below.
### Configuration Variables
@@ -36,15 +35,11 @@ Street, extracted from the displayed address.
*(string) (required)*
Housenumber, extracted from the displayed address.
**days**<br>
*(int) (optional)*
How many days to parse
Default: 20
## Example
## Examples
```yaml
# Displayed address: für Albert-Schweitzer-Weg 27, Grevenbroich (Stadtmitte)
# Displayed address:
# für Albert-Schweitzer-Weg 27, Grevenbroich (Stadtmitte)
waste_collection_schedule:
sources:
@@ -57,7 +52,8 @@ waste_collection_schedule:
```
```yaml
# Displayed address: für Am Damschenpfad 81, Dormagen (Nievenheim)
# Displayed address:
# für Am Damschenpfad 81, Dormagen (Nievenheim)
waste_collection_schedule:
sources:

View File

@@ -60,6 +60,7 @@ Currently the following service providers are supported:
- [AWIDO-online.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/awido_de.md)
- [Berlin-Recycling.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/berlin_recycling_de.md)
- [BSR.de / Berliner Stadtreinigungsbetriebe](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bsr_de.md)
- [EGN-Abfallkalender.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master//doc/source/egn_abfallkalender_de.md)
- [Jumomind.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/jumomind_de.md)
- [Landkreis-Wittmund.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/landkreis_wittmund_de.md)
- [Muellmax.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/muellmax_de.md)