add source for recycle! / recycleapp.be

This commit is contained in:
mampfes
2021-12-12 16:23:28 +01:00
parent e181094fbd
commit fe60178da8
5 changed files with 126 additions and 2 deletions

View File

@@ -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

View File

@@ -6,5 +6,5 @@
"dependencies": [],
"codeowners": ["@mampfes"],
"iot_class": "cloud_polling",
"version": "1.12.1"
"version": "1.13.0"
}

View File

@@ -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

View File

@@ -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**<br>
*(int)*
Postal Code.
**street**<br>
*(string)*
Street name.
**house_number**<br>
*(int)*
House number
## Example
```yaml
waste_collection_schedule:
sources:
- name: recycleapp_be
args:
postcode: 1140
street: Bazellaan
house_number: 1
```

View File

@@ -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)