refactor hygea_be

- fix support for postal code only cities (like Erquelinnes)
- minor refactorings in fetch code
This commit is contained in:
mampfes
2021-11-01 10:33:31 +01:00
parent d9cd5bfe80
commit 85e4b1fc81
2 changed files with 66 additions and 48 deletions

View File

@@ -1,59 +1,60 @@
from typing import Dict
import math
import time
import json
import datetime
import json
import time
from waste_collection_schedule import Collection # type: ignore[attr-defined]
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Hygea"
DESCRIPTION = "Source for Hygea garbage collection"
URL = "https://www.hygea.be/"
TEST_CASES = {
"Soignies": {
"street_index": "3758",
},
"Frameries": {
"street_index": "4203",
},
"Erquelinnes": {
"street_index": "6560",
},
"Soignies": {"streetIndex": "3758"},
"Frameries": {"streetIndex": "4203"},
"Erquelinnes": {"cp": "6560"},
}
WASTE_MAP = {
"om": {"type": "Ordures ménagères", "icon": "mdi:trash-can"},
"pmc": {"type": "PMC", "icon": "mdi:recycle"},
"sacvert": {"type": "Déchets Organiques", "icon": "mdi:trash-can"},
"fourth": {"type": "Papier & cartons", "icon": "mdi:leaf"},
}
class Source:
def __init__(self, street_index):
self.street_index = street_index
def __init__(self, streetIndex=None, cp=None):
self._street_index = streetIndex
self._cp = cp
def fetch(self):
response = requests.get(f"https://www.hygea.be/displaycal.html?street={self.street_index}&start={math.trunc(time.time())}&end={math.trunc(time.time()) + 2678400}")
params = {"start": int(time.time()), "end": int(time.time() + 2678400)}
if self._street_index is not None:
params["street"] = self._street_index
response = requests.get(
"https://www.hygea.be/displaycal.html", params=params
)
elif self._cp is not None:
params["street"] = self._cp
response = requests.get(
"https://www.hygea.be/displaycalws.html", params=params
)
if not response.ok:
return []
data = json.loads(response.text)
entries = []
entries = []
for day in data:
if "sacvert" in day["className"]:
entries.append(
Collection(
date=datetime.datetime.strptime(day["start"], "%Y-%m-%dT%H:%M:%S%z").date(),
t="Déchets Organiques", icon="mdi:trash-can"
)
)
if "pmc" in day["className"]:
entries.append(
Collection(
date=datetime.datetime.strptime(day["start"], "%Y-%m-%dT%H:%M:%S%z").date(), t="PMC",
icon="mdi:recycle"
)
)
if "fourth" in day["className"]:
entries.append(
Collection(
date=datetime.datetime.strptime(day["start"], "%Y-%m-%dT%H:%M:%S%z").date(),
t="Papier & cartons", icon="mdi:leaf"
)
)
date = datetime.datetime.strptime(
day["start"], "%Y-%m-%dT%H:%M:%S%z"
).date()
# example for day["className"]: 12 notadded pos136 om multi
waste_types = set(day["className"].split())
for abbr, map in WASTE_MAP.items():
if abbr in waste_types:
c = Collection(date=date, t=map["type"], icon=map["icon"])
entries.append(c)
return entries

View File

@@ -7,25 +7,42 @@ Support for schedules provided by [hygea.be](https://www.hygea.be/).
```yaml
waste_collection_schedule:
sources:
- name: hygea_id
- name: hygea_be
args:
street_id: ID
streetIndex: STREET_INDEX
cp: POSTAL_CODE
```
You id can be found in the url when visiting the [the calendar](https://www.hygea.be/votre-calendrier-de-collecte.html) for your street.
It may also work by giving it your post code, but this "api" is a mess so only do that if you have no other choice
The arguments can be found in the URL after visiting the [the calendar](https://www.hygea.be/votre-calendrier-de-collecte.html). Select your city and optionally street to show the schedule for your town. Now check the URL and extract either streetIndex (if available) or postal code from the URL. See also examples below.
### Configuration Variables
**street_id**<br>
*(string) (required)*
**streetIndex**<br>
*(int)*
Street index, extracted from URL.
**cp**<br>
*(int)*
Postal code, extracted from URL.
## Example
```yaml
# URL: https://www.hygea.be/votre-calendrier-de-collecte.html?cp=7021&street=&streetIndex=13
waste_collection_schedule:
sources:
- name: hygea_id
- name: hygea_be
args:
street_id: 3758
```
streetIndex: 13
```
```yaml
# URL: https://www.hygea.be/votre-calendrier-de-collecte.html?cp=6560&streetIndex=
waste_collection_schedule:
sources:
- name: hygea_be
args:
cp: 6560
```