From ca521c42ffc280298050e5a7057aa6e0a9806c3f Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Tue, 26 Dec 2023 13:55:29 +0100 Subject: [PATCH] remove all carrier returns + 1 typo --- .../source/bir_no.py | 164 ++++++++-------- .../source/cederbaum_de.py | 184 +++++++++--------- doc/source/bir_no.md | 112 +++++------ doc/source/cederbaum_de.md | 64 +++--- doc/source/samiljo_se.md | 112 +++++------ doc/source/uppsalavatten_se.md | 84 ++++---- 6 files changed, 360 insertions(+), 360 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py index 3de5651a..d8876b69 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py @@ -1,82 +1,82 @@ -import datetime - -import requests -from lxml import etree -from waste_collection_schedule import Collection # type: ignore[attr-defined] - -TITLE = "BIR (Bergensområdets Interkommunale Renovasjonsselskap)" -DESCRIPTION = "Askøy, Bergen, Bjørnafjorden, Eidfjord, Kvam, Osterøy, Samnanger, Ulvik, Vaksdal, Øygarden og Voss Kommune (Norway)." -URL = "https://bir.no" - -TEST_CASES = { - "Villa Paradiso": { - "street_name": "Nordåsgrenda", - "house_number": 7, - "house_letter": "", - }, - "Mardalsrenen 12 B": { - "street_name": "Mardalsrenen", - "house_number": "11", - }, - "Alf Bondes Veg 13 B": { - "street_name": "Alf Bondes Veg", - "house_number": "13", - "house_letter": "B", - }, -} - -API_URL = "https://bir.no/api/search/AddressSearch" -ICON_MAP = {"restavfall": "mdi:trash-can", "papir": "mdi:newspaper-variant-multiple"} - - -def map_icon(text): - for key, value in ICON_MAP.items(): - if key in text: - return value - return "mdi:trash-can" - - -class Source: - def __init__(self, street_name, house_number, house_letter=""): - self._street_name = street_name - self._house_number = house_number - self._house_letter = house_letter - - def fetch(self): - headers = {"user-agent": "Home-Assitant-waste-col-sched/0.1"} - - args = { - "q": f"{self._street_name} {self._house_number}{self._house_letter} ", # The space at the end is serving as a termination character for the query - "s": False, - } - - r = requests.get(API_URL, params=args, headers=headers) - r = requests.get( - f"{URL}/adressesoek/toemmekalender", - params={"rId": {r.json()[0]["Id"]}}, - headers=headers, - ) - doc = etree.HTML(r.content) - month_containers = doc.cssselect( - ".main-content .address-page-box .month-container" - ) - - return [ - Collection( - date=datetime.datetime.strptime( - f"{date.text.replace('des','dec').replace('mai','may').replace('okt','oct')} {container.cssselect('.month-title')[0].text.strip().split(' ')[1]}", - "%d. %b %Y", - ).date(), - t=doc.cssselect( - f'.trash-categories > .trash-row > img[src="{category_row.cssselect(".icon > img")[0].get("src")}"] + .trash-text' - )[0].text, - icon=map_icon( - doc.cssselect( - f'.trash-categories > .trash-row > img[src="{category_row.cssselect(".icon > img")[0].get("src")}"] + .trash-text' - )[0].text.lower() - ), - ) - for container in month_containers - for category_row in container.cssselect(".category-row") - for date in category_row.cssselect(".date-item > .date-item-date") - ] +import datetime + +import requests +from lxml import etree +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "BIR (Bergensområdets Interkommunale Renovasjonsselskap)" +DESCRIPTION = "Askøy, Bergen, Bjørnafjorden, Eidfjord, Kvam, Osterøy, Samnanger, Ulvik, Vaksdal, Øygarden og Voss Kommune (Norway)." +URL = "https://bir.no" + +TEST_CASES = { + "Villa Paradiso": { + "street_name": "Nordåsgrenda", + "house_number": 7, + "house_letter": "", + }, + "Mardalsrenen 12 B": { + "street_name": "Mardalsrenen", + "house_number": "11", + }, + "Alf Bondes Veg 13 B": { + "street_name": "Alf Bondes Veg", + "house_number": "13", + "house_letter": "B", + }, +} + +API_URL = "https://bir.no/api/search/AddressSearch" +ICON_MAP = {"restavfall": "mdi:trash-can", "papir": "mdi:newspaper-variant-multiple"} + + +def map_icon(text): + for key, value in ICON_MAP.items(): + if key in text: + return value + return "mdi:trash-can" + + +class Source: + def __init__(self, street_name, house_number, house_letter=""): + self._street_name = street_name + self._house_number = house_number + self._house_letter = house_letter + + def fetch(self): + headers = {"user-agent": "Home-Assitant-waste-col-sched/0.1"} + + args = { + "q": f"{self._street_name} {self._house_number}{self._house_letter} ", # The space at the end is serving as a termination character for the query + "s": False, + } + + r = requests.get(API_URL, params=args, headers=headers) + r = requests.get( + f"{URL}/adressesoek/toemmekalender", + params={"rId": {r.json()[0]["Id"]}}, + headers=headers, + ) + doc = etree.HTML(r.content) + month_containers = doc.cssselect( + ".main-content .address-page-box .month-container" + ) + + return [ + Collection( + date=datetime.datetime.strptime( + f"{date.text.replace('des','dec').replace('mai','may').replace('okt','oct')} {container.cssselect('.month-title')[0].text.strip().split(' ')[1]}", + "%d. %b %Y", + ).date(), + t=doc.cssselect( + f'.trash-categories > .trash-row > img[src="{category_row.cssselect(".icon > img")[0].get("src")}"] + .trash-text' + )[0].text, + icon=map_icon( + doc.cssselect( + f'.trash-categories > .trash-row > img[src="{category_row.cssselect(".icon > img")[0].get("src")}"] + .trash-text' + )[0].text.lower() + ), + ) + for container in month_containers + for category_row in container.cssselect(".category-row") + for date in category_row.cssselect(".date-item > .date-item-date") + ] diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/cederbaum_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cederbaum_de.py index 2539b679..7722c225 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/cederbaum_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cederbaum_de.py @@ -1,92 +1,92 @@ -import datetime -import json -import re - -import requests -from bs4 import BeautifulSoup -from waste_collection_schedule import Collection # type: ignore[attr-defined] - -TITLE = "Cederbaum Braunschweig" -DESCRIPTION = "Cederbaum Braunschweig Paperimüll" -URL = "https://www.cederbaum.de" -TEST_CASES = { - "Hans-Sommer-Str": {"street": "Hans-Sommer-Str."}, - "Adolfstr 31-42": {"street": "Adolfstr. 31-42"}, - "Am Schwarzen Berge": {"street": "am Schwarzen Berge "}, -} - -API_URL = "https://www.cederbaum.de/blaue-tonne/abfuhrkalender" -ICON_MAP = { - "PAPER": "mdi:newspaper", -} - - -class Source: - def __init__(self, street): - self._street = street - - self.page_source = None - self.street_id = None - self.collection_data = None - - def fetch_page_source(self): - resp = requests.get(API_URL) - soup = BeautifulSoup(resp.text, "html.parser") - self.page_source = soup - - def get_street_id(self): - if not self.page_source: - raise ValueError("No page source found") - - select = self.page_source.find("select") - - if not select: - raise ValueError("No tag found") + + options = select.find_all("option") + for option in options: + value = option.get("value") + text = option.get_text() + if text.lower().strip() == self._street.lower().strip(): + self.street_id = value + break + + def get_collection_data(self): + if not self.page_source: + raise ValueError("No page source found") + + script_tags = self.page_source.find_all("script") + script_with_text = [tag for tag in script_tags if tag.string] + pattern = re.compile(r"var rate = (\{.*?\});") + + for script_tag in script_with_text: + match = pattern.search(script_tag.string) + if match: + var_content = match.group(1) + self.collection_data = json.loads(var_content) + break + + def fetch(self): + self.fetch_page_source() + self.get_street_id() + self.get_collection_data() + + if not self.collection_data: + raise ValueError("No collection data found") + + entries = [] + waste_dates = self.collection_data[self.street_id]["Termine"] + for waste_date in waste_dates: + date = datetime.datetime.strptime( + waste_dates[waste_date]["Termin"], "%d.%m.%Y" + ) + + entries.append( + Collection( + date=date.date(), + t="Paper", + icon=ICON_MAP.get("PAPER"), + ) + ) + + return entries diff --git a/doc/source/bir_no.md b/doc/source/bir_no.md index b65521fe..08fd43c2 100644 --- a/doc/source/bir_no.md +++ b/doc/source/bir_no.md @@ -1,57 +1,57 @@ -# BIR - Bergensområdets Interkommunale Renovasjonsselskap - -```yaml -waste_collection_schedule: - sources: - - name: bir_no - args: - street_name: "" - house_number: "" - house_letter: "" -``` - -### Configuration Variables - -**street_name** -*(string) (required)* - -**house_number** -*(string|Integer) (required)* - -**house_letter** -*(string) (optional)* - -The arguments should be written exactly like on the website - -# Example configuration.yaml: - -```yaml -# Waste collection -waste_collection_schedule: - sources: - - name: bir_no - args: - street_name: "Alf Bondes Veg" - house_number: "13" - house_letter: "B" - customize: - - type: blue bin - alias: Papir - - type: green bin - alias: Restavfall - -# Optional Sensors -sensor: - - platform: waste_collection_schedule - name: next_collection - - platform: waste_collection_schedule - name: waste_collection_garbage - details_format: upcoming - types: - - Restavfall - - platform: waste_collection_schedule - name: waste_collection_paper - details_format: upcoming - types: - - Papir og plastemballasje +# BIR - Bergensområdets Interkommunale Renovasjonsselskap + +```yaml +waste_collection_schedule: + sources: + - name: bir_no + args: + street_name: "" + house_number: "" + house_letter: "" +``` + +### Configuration Variables + +**street_name** +*(string) (required)* + +**house_number** +*(string|Integer) (required)* + +**house_letter** +*(string) (optional)* + +The arguments should be written exactly like on the website + +# Example configuration.yaml: + +```yaml +# Waste collection +waste_collection_schedule: + sources: + - name: bir_no + args: + street_name: "Alf Bondes Veg" + house_number: "13" + house_letter: "B" + customize: + - type: blue bin + alias: Papir + - type: green bin + alias: Restavfall + +# Optional Sensors +sensor: + - platform: waste_collection_schedule + name: next_collection + - platform: waste_collection_schedule + name: waste_collection_garbage + details_format: upcoming + types: + - Restavfall + - platform: waste_collection_schedule + name: waste_collection_paper + details_format: upcoming + types: + - Papir og plastemballasje ``` \ No newline at end of file diff --git a/doc/source/cederbaum_de.md b/doc/source/cederbaum_de.md index 471d51de..02626f34 100644 --- a/doc/source/cederbaum_de.md +++ b/doc/source/cederbaum_de.md @@ -1,32 +1,32 @@ -# Cederbaum Braunschweig - -Support for paper waste collection schedules provided by [Cederbaum Container GmbH](https://www.cederbaum.de/), serving the city of Braunschweig, Germany. - -## Configuration via configuration.yaml - -```yaml -waste_collection_schedule: - sources: - - name: cederbaum_de - args: - street: STREET -``` - -### Configuration Variables - -**street** -*(String) (required)* - -## Example - -```yaml -waste_collection_schedule: - sources: - - name: cederbaum_de - args: - street: "Hans-Sommer-Str." -``` - -## How to get the source argument - -Find the parameter of your street using [https://www.cederbaum.de/blaue-tonne/abfuhrkalender](https://www.cederbaum.de/blaue-tonne/abfuhrkalender) and write them exactly like on the web page. +# Cederbaum Braunschweig + +Support for paper waste collection schedules provided by [Cederbaum Container GmbH](https://www.cederbaum.de/), serving the city of Braunschweig, Germany. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: cederbaum_de + args: + street: STREET +``` + +### Configuration Variables + +**street** +*(String) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: cederbaum_de + args: + street: "Hans-Sommer-Str." +``` + +## How to get the source argument + +Find the parameter of your street using [https://www.cederbaum.de/blaue-tonne/abfuhrkalender](https://www.cederbaum.de/blaue-tonne/abfuhrkalender) and write them exactly like on the web page. diff --git a/doc/source/samiljo_se.md b/doc/source/samiljo_se.md index 0ac843d2..4ab7651b 100644 --- a/doc/source/samiljo_se.md +++ b/doc/source/samiljo_se.md @@ -1,56 +1,56 @@ -# Samverkan Återvinning Miljö (SÅM) - -Support for schedules provided by [Samverkan Återvinning Miljö (SÅM)](https://samiljo.se/avfallshamtning/hamtningskalender/), serving the municipality of Gislaved, Gnosjö, Vaggeryd and Värnamo, Sweden. - -Helgvecka means that the pickup day might deviate from the scheduled day due to a public holiday during the week. - -## Configuration via configuration.yaml - -```yaml -waste_collection_schedule: - sources: - - name: samiljo_se - args: - street: STREET_NAME - city: CITY_NAME -``` - -### Configuration Variables - -**street** -*(string) (required)* - -**city** -*(string) (required)* - -## Example - -```yaml -waste_collection_schedule: - sources: - - name: samiljo_se - args: - street: Storgatan 1 - city: Burseryd -``` - - -## How to get the source argument - -The source argument is the street including number and the city to the house with waste collection. -The address can be tested [here](https://samiljo.se/avfallshamtning/hamtningskalender/). - -## How to add new waste types - -If your address are missing any waste types that shows in the [Hämtningskalender](https://samiljo.se/avfallshamtning/hamtningskalender/). Then there might be missing mappings in the NAME_MAP. -1. Run Samiljo_se_wastetype_searcher.py for the specific address or without arguments to scan all addresses in the database.
-Examples of valid command below. -```shell -> samiljo_se_wastetype_searcher.py --street "Storgatan 1" --city "Burseryd" -> samiljo_se_wastetype_searcher.py --city "Burseryd" -> samiljo_se_wastetype_searcher.py --street "Storgatan 1" -> samiljo_se_wastetype_searcher.py -``` -2. Missing mappings will be returned together with an address. -3. Use the [Hämtningskalender](https://samiljo.se/avfallshamtning/hamtningskalender/) to extract the corresponding common name to the new wastetype. -4. Add the new types to the NAME_MAP and optionaly to the ICON_MAP in samiljo_se.py and samiljo_se_wastetype_searcher.py. +# Samverkan Återvinning Miljö (SÅM) + +Support for schedules provided by [Samverkan Återvinning Miljö (SÅM)](https://samiljo.se/avfallshamtning/hamtningskalender/), serving the municipality of Gislaved, Gnosjö, Vaggeryd and Värnamo, Sweden. + +Helgvecka means that the pickup day might deviate from the scheduled day due to a public holiday during the week. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: samiljo_se + args: + street: STREET_NAME + city: CITY_NAME +``` + +### Configuration Variables + +**street** +*(string) (required)* + +**city** +*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: samiljo_se + args: + street: Storgatan 1 + city: Burseryd +``` + + +## How to get the source argument + +The source argument is the street including number and the city to the house with waste collection. +The address can be tested [here](https://samiljo.se/avfallshamtning/hamtningskalender/). + +## How to add new waste types + +If your address are missing any waste types that shows in the [Hämtningskalender](https://samiljo.se/avfallshamtning/hamtningskalender/). Then there might be missing mappings in the NAME_MAP. +1. Run Samiljo_se_wastetype_searcher.py for the specific address or without arguments to scan all addresses in the database.
+Examples of valid command below. +```shell +> samiljo_se_wastetype_searcher.py --street "Storgatan 1" --city "Burseryd" +> samiljo_se_wastetype_searcher.py --city "Burseryd" +> samiljo_se_wastetype_searcher.py --street "Storgatan 1" +> samiljo_se_wastetype_searcher.py +``` +2. Missing mappings will be returned together with an address. +3. Use the [Hämtningskalender](https://samiljo.se/avfallshamtning/hamtningskalender/) to extract the corresponding common name to the new wastetype. +4. Add the new types to the NAME_MAP and optionally to the ICON_MAP in samiljo_se.py and samiljo_se_wastetype_searcher.py. diff --git a/doc/source/uppsalavatten_se.md b/doc/source/uppsalavatten_se.md index ceb16e63..8863cc6b 100644 --- a/doc/source/uppsalavatten_se.md +++ b/doc/source/uppsalavatten_se.md @@ -1,42 +1,42 @@ -# Uppsala Vatten och Avfall AB - -Support for schedules provided by [Uppsala Vatten och Avfall AB](https://www.uppsalavatten.se), serving the municipality of Uppsala. - -## Configuration via configuration.yaml - -```yaml -waste_collection_schedule: - sources: - - name: uppsalavatten_se - args: - street: STREET_NAME - city: CITY_NAME -``` - -### Configuration Variables - -**street** -*(string) (required)* - -**city** -*(string) (required)* - -## Example - -```yaml -waste_collection_schedule: - sources: - - name: uppsalavatten_se - args: - street: SADELVÄGEN 1 - city: BJÖRKLINGE -``` - - -## How to get the source argument - -The source argument is the street including number and the city to the house with waste collection. -The address can be tested [here](https://www.uppsalavatten.se/sjalvservice/hamtningar-och-berakningar/dag-for-sophamtning-och-slamtomning). - - - +# Uppsala Vatten och Avfall AB + +Support for schedules provided by [Uppsala Vatten och Avfall AB](https://www.uppsalavatten.se), serving the municipality of Uppsala. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: uppsalavatten_se + args: + street: STREET_NAME + city: CITY_NAME +``` + +### Configuration Variables + +**street** +*(string) (required)* + +**city** +*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: uppsalavatten_se + args: + street: SADELVÄGEN 1 + city: BJÖRKLINGE +``` + + +## How to get the source argument + +The source argument is the street including number and the city to the house with waste collection. +The address can be tested [here](https://www.uppsalavatten.se/sjalvservice/hamtningar-och-berakningar/dag-for-sophamtning-och-slamtomning). + + +