-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add source for recycle! / recycleapp.be
- Loading branch information
Showing
5 changed files
with
126 additions
and
2 deletions.
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
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
80 changes: 80 additions & 0 deletions
80
...om_components/waste_collection_schedule/waste_collection_schedule/source/recycleapp_be.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,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 |
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,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 | ||
``` |
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