Skip to content

Commit

Permalink
add source for recycle! / recycleapp.be
Browse files Browse the repository at this point in the history
  • Loading branch information
mampfes committed Dec 12, 2021
1 parent e181094 commit fe60178
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion custom_components/waste_collection_schedule/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"dependencies": [],
"codeowners": ["@mampfes"],
"iot_class": "cloud_polling",
"version": "1.12.1"
"version": "1.13.0"
}
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
43 changes: 43 additions & 0 deletions doc/source/recycleapp_be.md
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
```
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fe60178

Please sign in to comment.