-
-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a source for Eigenbetrieb Abfallwirtschaft Landkreis Spree-Neiße (
#1925) * Added source for Eigenbetrieb Abfallwirtschaft Landkreis Spree-Neiße. * Updated the description for source for Eigenbetrieb Abfallwirtschaft Landkreis Spree-Neiße. * Removed unused copy pasted code in source for Eigenbetrieb Abfallwirtschaft Landkreis Spree-Neiße. * refactoring ifs + reformatting --------- Co-authored-by: Silvio Mummert <[email protected]> Co-authored-by: 5ila5 <[email protected]>
- Loading branch information
1 parent
fd9c013
commit cde46d8
Showing
4 changed files
with
147 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
101 changes: 101 additions & 0 deletions
101
..._collection_schedule/waste_collection_schedule/source/eigenbetrieb_abfallwirtschaft_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,101 @@ | ||
import datetime | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
from waste_collection_schedule.service.ICS import ICS | ||
|
||
# URL, DESCRIPTION and TITLE of the website | ||
TITLE = "Eigenbetrieb Abfallwirtschaft Landkreis Spree-Neiße" | ||
DESCRIPTION = "Source for Eigenbetrieb Abfallwirtschaft Landkreis Spree-Neiße." | ||
URL = "https://www.eigenbetrieb-abfallwirtschaft.de" | ||
COUNTRY = "de" | ||
TEST_CASES = { | ||
"Forst (Lausitz), Rosenweg": {"city": "4", "street": "344"}, | ||
"Peitz, Am See": {"city": "8", "street": "1077"}, | ||
"Guben, Altsprucke": {"city": "5", "street": "410"}, | ||
"Spremberg, Gartenstrasse": {"city": 10, "street": 701}, | ||
} | ||
|
||
ICON_MAP = { | ||
"Restmüll": "mdi:trash-can", | ||
"Biotonne": "mdi:leaf", | ||
"Papiercontainer": "mdi:package-variant", | ||
"Gelbe(r) Sack/Tonne": "mdi:recycle", | ||
} | ||
|
||
|
||
class Source: | ||
def __init__(self, city: str, street: str): | ||
self._city: str = city | ||
self._street: str = street | ||
self._ics = ICS() | ||
|
||
def fetch(self): | ||
# get the current year | ||
now = datetime.datetime.now() | ||
year = now.year | ||
# build the urls and fetch the data ... | ||
# for this year ... | ||
url_this_year = ( | ||
f"{URL}/termine/abfuhrtermine/{year}/{self._city}/{self._street}.html" | ||
) | ||
entries = self.get_data(url_this_year) | ||
# and in december for the next year | ||
if now.month == 12: | ||
try: | ||
url_next_year = f"{URL}/termine/abfuhrtermine/{year +1 }/{self._city}/{self._street}.html" | ||
entries += self.get_data(url_next_year) | ||
except Exception: | ||
pass | ||
return entries | ||
|
||
def get_data(self, url): | ||
# download the site | ||
response = requests.get(url) | ||
|
||
# check if the request was successful | ||
if response.status_code != 200: | ||
raise Exception( | ||
f"Error loading page {URL}, status code {response.status_code}." | ||
) | ||
# parse the response | ||
soup = BeautifulSoup(response.text, "html.parser") | ||
|
||
# check for an input element named 'ics' | ||
input_element = soup.find("input", {"name": "ics"}) | ||
|
||
# check if the input element was found | ||
if not input_element: | ||
raise Exception("Didn't find the input named ics.") | ||
# try to find the parent form | ||
form = input_element.find_parent("form") | ||
|
||
# check if the parent form was found | ||
if not form: | ||
raise Exception("Didn't find the ics request formular.") | ||
|
||
# extract the formular inputs | ||
form_data = {} | ||
for input_tag in form.find_all("input"): | ||
name = input_tag.get("name") | ||
value = input_tag.get("value", "") | ||
form_data[name] = value | ||
|
||
# extract the url (convert the relative url to a complete one) | ||
action_url = URL + form.get("action") | ||
|
||
# send the formular to fetch the ics file | ||
response = requests.post(action_url, data=form_data) | ||
response.raise_for_status() | ||
response.encoding = "utf-8" | ||
# process the response | ||
dates = self._ics.convert(response.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 |
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,44 @@ | ||
# ASPN | ||
|
||
Support for schedules provided by [ASPN](https://www.eigenbetrieb-abfallwirtschaft.de/termine/abfuhrtermine.html) | ||
|
||
1. Go to [ASPN](https://www.eigenbetrieb-abfallwirtschaft.de/termine/abfuhrtermine.html) and select your city and street. | ||
2. The address of the browser window is changed by the website. | ||
3. Use the ids in the address to configure your waste collection schedule. | ||
|
||
**Example:** | ||
- https://www.eigenbetrieb-abfallwirtschaft.de/termine/abfuhrtermine/2024/4/344.html | ||
- for "Rosenweg" in "Forst (Lausitz)" | ||
- **4** is the id of the city | ||
- **344** is the id of the street | ||
- the year will be entered automatically by the script | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: eigenbetrieb_abfallwirtschaft_de | ||
args: | ||
city: CITY_ID | ||
street: STREET_ID | ||
``` | ||
### Configuration Variables | ||
**city** | ||
*(string) (required): The id of your city* | ||
**street** | ||
*(string) (required): The id of your street* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: eigenbetrieb_abfallwirtschaft_de | ||
args: | ||
city: "4" | ||
street: "344" | ||
``` |
Oops, something went wrong.