-
-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
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
69 changes: 69 additions & 0 deletions
69
...mponents/waste_collection_schedule/waste_collection_schedule/source/fosenrenovasjon_no.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,69 @@ | ||
from datetime import datetime | ||
from urllib.parse import quote | ||
|
||
import requests | ||
from waste_collection_schedule import Collection | ||
|
||
TITLE = "Fosen Renovasjon" | ||
DESCRIPTION = "Source for Fosen Renovasjon." | ||
URL = "https://fosenrenovasjon.no/" | ||
TEST_CASES = {"Lysøysundveien 117": {"address": "Lysøysundveien 117"}} | ||
|
||
|
||
ICON_MAP = { | ||
"Restavfall til forbrenning": "mdi:trash-can", | ||
"Matavfall": "mdi:leaf", | ||
"Papir og plastemballasje": "mdi:recycle", | ||
} | ||
|
||
|
||
ADDRESS_URL = "https://fosen.renovasjonsportal.no/api/address/{address}" | ||
COLLECTIONS_URL = ADDRESS_URL + "/details" | ||
|
||
|
||
class Source: | ||
def __init__(self, address: str): | ||
self._address = address.lower().strip() | ||
self._address_id: str | None = None | ||
|
||
def _fetch_address_id(self): | ||
url = ADDRESS_URL.format(address=quote(self._address)) | ||
r = requests.get(url) | ||
r.raise_for_status() | ||
data = r.json() | ||
for address in data["searchResults"]: | ||
if address["title"].lower().strip() == self._address: | ||
self._address_id = address["id"] | ||
return | ||
|
||
raise ValueError("Address not found", self._address) | ||
|
||
def fetch(self) -> list[Collection]: | ||
new_id = False | ||
if self._address_id is None: | ||
new_id = False | ||
self._fetch_address_id() | ||
try: | ||
return self._get_collections() | ||
except Exception: | ||
if new_id: | ||
raise | ||
self._fetch_address_id() | ||
return self._get_collections() | ||
|
||
def _get_collections(self) -> list[Collection]: | ||
if self._address_id is None: | ||
raise ValueError("Address not found", self._address) | ||
url = COLLECTIONS_URL.format(address=self._address_id) | ||
r = requests.get(url) | ||
r.raise_for_status() | ||
data = r.json() | ||
|
||
entries: list[Collection] = [] | ||
for d in data["disposals"]: | ||
date = datetime.strptime(d["date"], "%Y-%m-%dT%H:%M:%S").date() | ||
entries.append( | ||
Collection(date=date, t=d["fraction"], icon=ICON_MAP.get(d["fraction"])) | ||
) | ||
|
||
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,34 @@ | ||
# Fosen Renovasjon | ||
|
||
Support for schedules provided by [Fosen Renovasjon](https://fosenrenovasjon.no/), serving Fosen, Norway. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: fosenrenovasjon_no | ||
args: | ||
address: ADDRESS | ||
|
||
``` | ||
### Configuration Variables | ||
**address** | ||
*(String) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: fosenrenovasjon_no | ||
args: | ||
address: Lysøysundveien 117 | ||
|
||
``` | ||
## How to get the source argument | ||
Visit <https://fosenrenovasjon.no/tommekalender/#!/main> and search for your address, then use the address spelled exactly as the autocomplete suggests. |
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