From fe60178da81310120690fa535ae2242888f6cdee Mon Sep 17 00:00:00 2001 From: mampfes Date: Sun, 12 Dec 2021 16:23:28 +0100 Subject: [PATCH] add source for recycle! / recycleapp.be --- README.md | 1 + .../waste_collection_schedule/manifest.json | 2 +- .../source/recycleapp_be.py | 80 +++++++++++++++++++ doc/source/recycleapp_be.md | 43 ++++++++++ info.md | 2 +- 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/recycleapp_be.py create mode 100644 doc/source/recycleapp_be.md diff --git a/README.md b/README.md index 533c1b1c6..bb344d67b 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ Currently the following service providers are supported: ### Belgium - [Hygea.be](./doc/source/hygea_be.md) +- [Recycle! / RecycleApp.be](./doc/source/recycleapp_be.md) ### Germany diff --git a/custom_components/waste_collection_schedule/manifest.json b/custom_components/waste_collection_schedule/manifest.json index 730d4c4ab..1716b600a 100644 --- a/custom_components/waste_collection_schedule/manifest.json +++ b/custom_components/waste_collection_schedule/manifest.json @@ -6,5 +6,5 @@ "dependencies": [], "codeowners": ["@mampfes"], "iot_class": "cloud_polling", - "version": "1.12.1" + "version": "1.13.0" } diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/recycleapp_be.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/recycleapp_be.py new file mode 100644 index 000000000..45c041079 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/recycleapp_be.py @@ -0,0 +1,80 @@ +import logging +from datetime import datetime, timedelta + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Recycle!" +DESCRIPTION = "Source for RecycleApp.be" +URL = "https://www.recycleapp.be" +TEST_CASES = { + "1140 Evere, Bazellaan 1": { + "postcode": 1140, + "street": "Bazellaan", + "house_number": 1, + } +} + +_LOGGER = logging.getLogger(__name__) + + +class Source: + def __init__(self, postcode, street, house_number): + self._postcode = postcode + self._street = street + self._house_number = house_number + + def fetch(self): + url = "https://recycleapp.be/api/app/v1" + headers = { + "x-secret": "Crgja3EGWe8jdapyr4EEoMBgZACYYjRRcRpaMQrLDW9HJBvmgkfGQyYqLgeXPavAGvnJqkV87PBB2b8zx43q46sUgzqio4yRZbABhtKeagkVKypTEDjKfPgGycjLyJTtLHYpzwJgp4YmmCuJZN9ZmJY8CGEoFs8MKfdJpU9RjkEVfngmmk2LYD4QzFegLNKUbcCeAdEW", + "x-consumer": "recycleapp.be", + "User-Agent": "", + "Authorization": "", + } + r = requests.get(f"{url}/access-token", headers=headers) + headers["Authorization"] = r.json()["accessToken"] + + params = {"q": self._postcode} + r = requests.get(f"{url}/zipcodes", params=params, headers=headers) + if r.status_code != 200: + _LOGGER.error("Get zip code failed") + return [] + zipcodeId = r.json()["items"][0]["id"] + + params = {"q": self._street, "zipcodes": zipcodeId} + r = requests.get(f"{url}/streets", params=params, headers=headers) + if r.status_code != 200: + _LOGGER.error("Get street id failed") + return [] + + for item in r.json()["items"]: + if item["name"] == self._street: + streetId = item["id"] + if streetId is None: + streetId = r.json()["items"][0]["id"] + + now = datetime.now() + fromDate = now.strftime("%Y-%m-%d") + untilDate = (now + timedelta(days=365)).strftime("%Y-%m-%d") + params = { + "zipcodeId": zipcodeId, + "streetId": streetId, + "houseNumber": self._house_number, + "fromDate": fromDate, + "untilDate": untilDate, + # "size":100, + } + r = requests.get(f"{url}/collections", params=params, headers=headers) + if r.status_code != 200: + _LOGGER.error("Get data failed") + return [] + + entries = [] + for item in r.json()["items"]: + if "exception" in item and "replacedBy" in item["exception"]: + continue + + date = datetime.strptime(item["timestamp"], "%Y-%m-%dT%H:%M:%S.000Z") + entries.append(Collection(date, item["fraction"]["name"]["en"])) + return entries diff --git a/doc/source/recycleapp_be.md b/doc/source/recycleapp_be.md new file mode 100644 index 000000000..c0502ca05 --- /dev/null +++ b/doc/source/recycleapp_be.md @@ -0,0 +1,43 @@ +# Recycle! + +Support for schedules provided by [Recycle / recycleapp.be](https://www.recycleapp.be/). + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: recycleapp_be + args: + postcode: POST_CODE + street: STREET + house_number: HOUSE_NUMBER +``` + +The source arguments are simply the values of the form elements on the homepage. + +### Configuration Variables + +**postcode**
+*(int)* +Postal Code. + +**street**
+*(string)* +Street name. + +**house_number**
+*(int)* +House number + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: recycleapp_be + args: + postcode: 1140 + street: Bazellaan + house_number: 1 +``` \ No newline at end of file diff --git a/info.md b/info.md index 9ff455481..2c5cdb8ce 100644 --- a/info.md +++ b/info.md @@ -46,7 +46,7 @@ Currently the following service providers are supported: ### Belgium - [Hygea](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/hygea_be.md) - +- [Recycle! / RecycleApp.be](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/recycleapp_be.md) ### Germany - [Abfall.IO / AbfallPlus.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfall_io.md)