-
-
Notifications
You must be signed in to change notification settings - Fork 745
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
135 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
82 changes: 82 additions & 0 deletions
82
...mponents/waste_collection_schedule/waste_collection_schedule/source/stirling_wa_gov_au.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,82 @@ | ||
import logging | ||
|
||
import requests | ||
from dateutil import parser | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
TITLE = "Stirling" | ||
DESCRIPTION = "Source for Stirling." | ||
URL = "https://www.stirling.wa.gov.au/" | ||
TEST_CASES = { | ||
"-31.9034183 115.8320855": {"lat": -31.9034183, "lon": 115.8320855}, | ||
"-31.878331, 115.815553": {"lat": "-31.8783052", "lon": "115.8157741"}, | ||
} | ||
|
||
|
||
ICON_MAP = { | ||
"red": "mdi:trash-can", | ||
"green": "mdi:leaf", | ||
"greenverge": "mdi:pine-tree", | ||
"yellow": "mdi:recycle", | ||
} | ||
|
||
|
||
API_URL = "https://www.stirling.wa.gov.au/aapi/map" | ||
|
||
|
||
class Source: | ||
def __init__(self, lat: float, lon: float): | ||
if isinstance(lat, str): | ||
lat = float(lat) | ||
if isinstance(lon, str): | ||
lon = float(lon) | ||
|
||
self._lat: float = lat | ||
self._lon: float = lon | ||
|
||
def fetch(self) -> list[Collection]: | ||
headers = { | ||
"Host": "www.stirling.wa.gov.au", | ||
"Accept": "application/json, text/javascript, */*; q=0.01", | ||
"configid": "7c833520-7b62-4228-8522-fb1a220b32e8", | ||
"form": "57753bab-f589-44d7-8934-098b6d5c572f", | ||
"fields": f"{self._lon},{self._lat}", | ||
"apikeylookup": "Test Map Key", | ||
"X-Requested-With": "XMLHttpRequest", | ||
"Connection": "keep-alive", | ||
} | ||
r = requests.get(API_URL, headers=headers) | ||
r.raise_for_status() | ||
|
||
data = r.json() | ||
entries = [] | ||
for collection in data: | ||
bin_type = None | ||
bin_date_str = None | ||
bin_date = None | ||
|
||
for arg in collection: | ||
if arg.get("name") == "type": | ||
bin_type = arg.get("value") | ||
if arg.get("name") == "date": | ||
bin_date_str = arg.get("value") | ||
if not isinstance(bin_date_str, str): | ||
continue | ||
bin_date_str = bin_date_str.replace(" ", " ").strip() | ||
try: | ||
# Aug 7 2024 | ||
bin_date = parser.parse(bin_date_str).date() | ||
except ValueError: | ||
_LOGGER.warning("Could not parse date %s", bin_date_str) | ||
|
||
if bin_date is None: | ||
_LOGGER.warning("Could not find date for collection %s", bin_type) | ||
continue | ||
if bin_type is None: | ||
_LOGGER.warning("Skipping invalid collection record") | ||
continue | ||
icon = ICON_MAP.get(bin_type.lower()) # Collection icon | ||
entries.append(Collection(date=bin_date, t=bin_type, icon=icon)) | ||
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,46 @@ | ||
# Stirling | ||
|
||
Support for schedules provided by [Stirling](https://www.stirling.wa.gov.au/), serving Stirling, Australia. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stirling_wa_gov_au | ||
args: | ||
lat: LATITUDE | ||
lon: LONGITUDE | ||
|
||
``` | ||
### Configuration Variables | ||
**lat** | ||
*(Float) (required)* | ||
**lon** | ||
*(Float) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stirling_wa_gov_au | ||
args: | ||
lat: -31.9034183 | ||
lon: 115.8320855 | ||
|
||
``` | ||
## How to get the source argument | ||
### Using your address coordinates | ||
1. Find the coordinates of your address using any map service like Google Maps. (the coordinates should be in decimal format with 7 decimal places) | ||
2. Write the coordinates in the configuration file. | ||
### Inspecting network requests of the Stirling website | ||
Visit <https://www.stirling.wa.gov.au/waste-and-environment/waste-and-recycling/residential-bin-collections> and open the developer tools of your browser. Go to the network tab and filter. search for your address in the websites search bar and click on you address. In your network tab look for a GET request to https://www.stirling.wa.gov.au/aapi/map. Click on that request and look for the Request Headers. The `fields` value of the Request Headers is latitude and longitude **in inverse order!!!** (longitude,latitude). Write the coordinates in the configuration file. |
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