-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update Waster Collection Schedule via HACS
- Loading branch information
1 parent
a3bad3f
commit cbd543e
Showing
55 changed files
with
3,264 additions
and
544 deletions.
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
106 changes: 106 additions & 0 deletions
106
..._components/waste_collection_schedule/waste_collection_schedule/source/abfall_lippe_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,106 @@ | ||
import datetime | ||
|
||
import requests | ||
from bs4 import BeautifulSoup, Tag | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
from waste_collection_schedule.service.ICS import ICS | ||
|
||
TITLE = "Abfallwirtschaftsverbandes Lippe" | ||
DESCRIPTION = "Source for Abfallwirtschaftsverbandes Lippe." | ||
URL = "https://abfall-lippe.de" | ||
TEST_CASES = { | ||
"Bad Salzuflen BB": {"gemeinde": "Bad Salzuflen", "bezirk": "BB"}, | ||
"Augustdorf": {"gemeinde": "Augustdorf"}, | ||
"Barntrup 3B": {"gemeinde": "Barntrup", "bezirk": "3-B"}, | ||
} | ||
|
||
|
||
ICON_MAP = { | ||
"Graue": "mdi:trash-can", | ||
"Glass": "mdi:bottle-soda", | ||
"Grüne": "mdi:leaf", | ||
"Laubannahme": "mdi:leaf-maple", | ||
"Blaue": "mdi:package-variant", | ||
"Gelbe": "mdi:recycle", | ||
"Schadstoffsammlung": "mdi:biohazard", | ||
"Groß-Container Altpapier|Pappe": "mdi:package-variant-closed", | ||
} | ||
|
||
|
||
API_URL = "https://abfall-lippe.de/service/abfuhrkalender" | ||
|
||
|
||
class Source: | ||
def __init__(self, gemeinde: str, bezirk: str | None = None): | ||
self._gemeinde: str = gemeinde | ||
self._bezirk: str = bezirk if bezirk is not None else "" | ||
self._ics = ICS() | ||
|
||
def fetch(self): | ||
year = datetime.datetime.now().year | ||
urls = [ | ||
API_URL, | ||
f"{API_URL}-{year}", | ||
f"{API_URL}-{year-1}", | ||
f"{API_URL}-{year+1}", | ||
] | ||
for url in urls: | ||
r = requests.get(url) | ||
if r.status_code == 200 and r.request.url != "https://abfall-lippe.de": | ||
break | ||
if r.status_code != 200 or r.request.url == "https://abfall-lippe.de": | ||
raise Exception( | ||
"Failed to fetch data from Abfallwirtschaftsverbandes Lippe The URL may have changed." | ||
) | ||
r.raise_for_status() | ||
|
||
soup = BeautifulSoup(r.text, "html.parser") | ||
headlines = soup.find_all("div", class_="elementor-widget-heading") | ||
|
||
gemeinde_headline: Tag | None = None | ||
for headline in headlines: | ||
if not isinstance(headline, Tag): | ||
continue | ||
h3 = headline.find("h3") | ||
if not isinstance(h3, Tag): | ||
continue | ||
|
||
if h3.text.lower().strip() == self._gemeinde.lower().strip(): | ||
gemeinde_headline = headline | ||
break | ||
|
||
if gemeinde_headline is None: | ||
raise Exception("Gemeinde not found, please check spelling") | ||
|
||
links_container = gemeinde_headline.parent | ||
|
||
if links_container is None: | ||
raise Exception(f"No links found for {self._gemeinde}") | ||
|
||
link: Tag | None = None | ||
for a in links_container.find_all("a"): | ||
if not isinstance(a, Tag): | ||
continue | ||
if ( | ||
a.text.lower().replace("ics", "").strip() | ||
== self._bezirk.lower().replace("ics", "").strip() | ||
): | ||
link = a.get("href") | ||
break | ||
|
||
if link is None: | ||
raise Exception("Did not found matching ICS link for gemeinde and (bezirk)") | ||
|
||
# get ICS file | ||
r = requests.get(link) | ||
r.raise_for_status() | ||
r.encoding = "utf-8" | ||
dates = self._ics.convert(r.text) | ||
entries = [] | ||
for d in dates: | ||
icon = ICON_MAP.get(d[1].split(" ")[0]) | ||
if icon is None: | ||
icon = ICON_MAP.get(d[1]) | ||
entries.append(Collection(d[0], d[1], icon=icon)) | ||
|
||
return entries |
Oops, something went wrong.