refactor sepan_remondis_pl

This commit is contained in:
mampfes
2023-03-05 20:33:42 +01:00
parent 979a641c31
commit f238053025
4 changed files with 53 additions and 59 deletions

View File

@@ -332,8 +332,8 @@ Waste collection schedules in the following formats and countries are supported.
<summary>Poland</summary> <summary>Poland</summary>
- [Ecoharmonogram](/doc/source/ecoharmonogram_pl.md) / ecoharmonogram.pl - [Ecoharmonogram](/doc/source/ecoharmonogram_pl.md) / ecoharmonogram.pl
- [Poznań/Koziegłowy/Objezierze/Oborniki](/doc/source/sepan_remondis_pl.md) / sepan.remondis.pl
- [Warsaw](/doc/source/warszawa19115_pl.md) / warszawa19115.pl - [Warsaw](/doc/source/warszawa19115_pl.md) / warszawa19115.pl
- [Poznań/Koziegłowy/Objezierze/Oborniki](/doc/source/sepan_remondis_pl.md) / sepan.remondis.pl/harmonogram
</details> </details>
<details> <details>

View File

@@ -1,30 +1,26 @@
import logging
import datetime import datetime
import json
import logging
import xml.etree.ElementTree import xml.etree.ElementTree
import json
import requests import requests
from waste_collection_schedule import Collection from waste_collection_schedule import Collection
TITLE = "Poznań/Koziegłowy/Objezierze/Oborniki" TITLE = "Poznań/Koziegłowy/Objezierze/Oborniki"
DESCRIPTION = "Source for Poznań/Koziegłowy/Objezierze/Oborniki city garbage collection" DESCRIPTION = "Source for Poznań/Koziegłowy/Objezierze/Oborniki city garbage collection"
URL = "https://sepan.remondis.pl/harmonogram/" URL = "https://sepan.remondis.pl"
TEST_CASES = { TEST_CASES = {
"Street Name": {"city": "Poznań", "street_name": "ŚWIĘTY MARCIN", "street_number": "1"}, "Street Name": {
"city": "Poznań",
"street_name": "ŚWIĘTY MARCIN",
"street_number": "1",
},
} }
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class SourceConfigurationError(ValueError): API_URL = "https://sepan.remondis.pl/harmonogram"
pass
class SourceParseError(ValueError):
pass
PAGE_URL = "https://sepan.remondis.pl/harmonogram"
NAME_MAP = { NAME_MAP = {
1: "Zmieszane odpady komunalne", 1: "Zmieszane odpady komunalne",
@@ -46,70 +42,68 @@ ICON_MAP = {
7: "mdi:trash-can", 7: "mdi:trash-can",
} }
class Source: class Source:
def __init__(self, city=None, street_name=None, street_number=None): def __init__(self, city, street_name, street_number):
if city is None or street_name is None or street_number is None:
raise SourceConfigurationError(
"All params city, street_name and street_number must have a value"
)
self._city = city.upper() self._city = city.upper()
self._street_name = street_name.upper() self._street_name = street_name.upper()
self._street_number = street_number.upper() self._street_number = street_number.upper()
def fetch(self): def fetch(self):
entries = [] r = requests.get(f"{API_URL}/addresses/cities")
r.raise_for_status()
url = f"{PAGE_URL}/addresses/cities"
r = requests.get(url)
city_id = 0 city_id = 0
cities = json.loads(r.text) cities = json.loads(r.text)
for item in cities: for item in cities:
if item["value"] == self._city: if item["value"] == self._city:
city_id = item["id"] city_id = item["id"]
if city_id == 0: if city_id == 0:
_LOGGER.debug(f"No such city") raise Exception("city not found")
return entries
url = f"{PAGE_URL}/addresses/streets/{city_id}" r = requests.get(f"{API_URL}/addresses/streets/{city_id}")
r = requests.get(url) r.raise_for_status()
street_id = 0 street_id = 0
streets = json.loads(r.text) streets = json.loads(r.text)
for item in streets: for item in streets:
if item["value"] == self._street_name: if item["value"] == self._street_name:
street_id = item["id"] street_id = item["id"]
if street_id == 0: if street_id == 0:
_LOGGER.debug(f"No such street") raise Exception("street not found")
return entries
r = requests.get(f"{API_URL}/addresses/numbers/{city_id}/{street_id}")
url = f"{PAGE_URL}/addresses/numbers/{city_id}/{street_id}" r.raise_for_status()
r = requests.get(url)
number_id = 0 number_id = 0
numbers = json.loads(r.text) numbers = json.loads(r.text)
for item in numbers: for item in numbers:
if item["value"] == self._street_number: if item["value"] == self._street_number:
number_id = item["id"] number_id = item["id"]
if number_id == 0: if number_id == 0:
_LOGGER.debug(f"No such number") raise Exception("number not found")
return entries
url = f"{PAGE_URL}/reports?type=html&id={number_id}" r = requests.get(f"{API_URL}/reports?type=html&id={number_id}")
r = requests.get(url) r.raise_for_status()
report = json.loads(r.text) report = json.loads(r.text)
if report["status"] == "success": if report["status"] != "success":
url = report["filePath"] raise Exception("fetch report failed")
if url == "":
_LOGGER.debug(f"Error fetching report")
return entries
r = requests.get(url) r = requests.get(report["filePath"])
table = r.text[r.text.find('<table'):r.text.rfind('</table>')+8] r.raise_for_status()
table = r.text[r.text.find("<table") : r.text.rfind("</table>") + 8]
tree = xml.etree.ElementTree.fromstring(table) tree = xml.etree.ElementTree.fromstring(table)
year = datetime.date.today().year year = datetime.date.today().year
for row_index, row in enumerate(tree.findall('.//tr')):
if row_index > 0: entries = []
for cell_index, cell in enumerate(row.findall('.//td')): for row_index, row in enumerate(tree.findall(".//tr")):
if cell_index > 0 and isinstance(cell.text, str): if row_index > 0:
for day in cell.text.split(','): for cell_index, cell in enumerate(row.findall(".//td")):
entries.append(Collection(datetime.date(year, row_index-1, int(day)), NAME_MAP[cell_index], ICON_MAP[cell_index])) if cell_index > 0 and isinstance(cell.text, str):
for day in cell.text.split(","):
entries.append(
Collection(
datetime.date(year, row_index - 1, int(day)),
NAME_MAP[cell_index],
ICON_MAP[cell_index],
)
)
return entries return entries

View File

@@ -21,13 +21,13 @@ waste_collection_schedule:
### Configuration Variables ### Configuration Variables
**city** **city**
*(string)* *(string) (required)*
**street_address** **street_address**
*(string)* *(string) (required)*
**street_number** **street_number**
*(string)* *(string)(required)*
All arguments must be provided. All arguments must be provided.

View File

@@ -25,7 +25,7 @@ Waste collection schedules from service provider web sites are updated daily, de
| Netherlands | ACV Group, Alpen an den Rijn, Area Afval, Avalex, Avri, Bar Afvalbeheer, Circulus, Cyclus NV, Dar, Den Haag, GAD, Gemeente Almere, Gemeente Berkelland, Gemeente Cranendonck, Gemeente Hellendoorn, Gemeente Lingewaard, Gemeente Meppel, Gemeente Middelburg + Vlissingen, Gemeente Peel en Maas, Gemeente Schouwen-Duiveland, Gemeente Sudwest-Fryslan, Gemeente Venray, Gemeente Voorschoten, Gemeente Wallre, Gemeente Westland, HVC Groep, Meerlanden, Mijn Blink, PreZero, Purmerend, RAD BV, Reinigingsbedrijf Midden Nederland, Reinis, Spaarne Landen, Stadswerk 072, Twente Milieu, Waardlanden, Ximmio, ZRD | | Netherlands | ACV Group, Alpen an den Rijn, Area Afval, Avalex, Avri, Bar Afvalbeheer, Circulus, Cyclus NV, Dar, Den Haag, GAD, Gemeente Almere, Gemeente Berkelland, Gemeente Cranendonck, Gemeente Hellendoorn, Gemeente Lingewaard, Gemeente Meppel, Gemeente Middelburg + Vlissingen, Gemeente Peel en Maas, Gemeente Schouwen-Duiveland, Gemeente Sudwest-Fryslan, Gemeente Venray, Gemeente Voorschoten, Gemeente Wallre, Gemeente Westland, HVC Groep, Meerlanden, Mijn Blink, PreZero, Purmerend, RAD BV, Reinigingsbedrijf Midden Nederland, Reinis, Spaarne Landen, Stadswerk 072, Twente Milieu, Waardlanden, Ximmio, ZRD |
| New Zealand | Auckland Council, Christchurch City Council, Gore, Invercargill & Southland, Horowhenua District Council, Waipa District Council, Wellington City Council | | New Zealand | Auckland Council, Christchurch City Council, Gore, Invercargill & Southland, Horowhenua District Council, Waipa District Council, Wellington City Council |
| Norway | Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Stavanger Kommune | | Norway | Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Stavanger Kommune |
| Poland | Ecoharmonogram, Warsaw, Poznań/Koziegłowy/Objezierze/Oborniki | | Poland | Ecoharmonogram, Poznań/Koziegłowy/Objezierze/Oborniki, Warsaw |
| Slovenia | Moji odpadki, Ljubljana | | Slovenia | Moji odpadki, Ljubljana |
| Sweden | Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Region Gotland, Ronneby Miljöteknik, SRV Återvinning, SSAM, Sysav Sophämntning, VA Syd Sophämntning | | Sweden | Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Region Gotland, Ronneby Miljöteknik, SRV Återvinning, SSAM, Sysav Sophämntning, VA Syd Sophämntning |
| Switzerland | A-Region, Andwil, Appenzell, Berg, Bühler, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, Rehetobel, Rorschach, Rorschacherberg, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden | | Switzerland | A-Region, Andwil, Appenzell, Berg, Bühler, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, Rehetobel, Rorschach, Rorschacherberg, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden |