forked from mampfes/hacs_waste_collection_schedule
-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 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
101 changes: 101 additions & 0 deletions
101
...ponents/waste_collection_schedule/waste_collection_schedule/source/cardinia_vic_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,101 @@ | ||
import time | ||
from datetime import datetime, timedelta | ||
|
||
import requests | ||
from waste_collection_schedule import Collection | ||
|
||
TITLE = "Cardinia Shire Council" | ||
DESCRIPTION = "Source script for cardinia.vic.gov.au" | ||
URL = "https://www.cardinia.vic.gov.au" | ||
TEST_CASES = { | ||
"1015 Manks Rd": {"address": "1015 Manks Rd, Dalmore Vic"}, # Monday | ||
"6-8 Main St": {"address": "6-8 Main St, Nar Nar Goon Vic"}, # Tuesday | ||
"875 Princes Hwy": {"address": "875 Princes Hwy, Pakenham Vic"}, # Thursday | ||
"124 Main St": {"address": "124 Main St, Pakenham Vic"}, # Friday | ||
} | ||
|
||
API_URL = "https://www.cardinia.vic.gov.au/info/20002/rubbish_and_recycling/385/bin_collection_days_and_putting_your_bins_out/2#section-2-check-your-bin-collection-days-online" | ||
ICON_MAP = { | ||
"Rubbish": "mdi:trash-can", | ||
"Recycling": "mdi:recycle", | ||
"Green Waste": "mdi:leaf", | ||
} | ||
|
||
|
||
class Source: | ||
def __init__(self, address): | ||
self._address = address | ||
|
||
def next_collection(self, collection_day, weeks, start_date): | ||
collection_day = time.strptime(collection_day, "%A").tm_wday | ||
days = (collection_day - datetime.now().date().weekday() + 7) % 7 | ||
next_collect = datetime.now().date() + timedelta(days=days) | ||
days = abs(next_collect-datetime.strptime(start_date, "%Y-%m-%d").date()).days | ||
if ((days//7)%weeks): | ||
next_collect = next_collect + timedelta(days=7) | ||
return next_collect | ||
|
||
def fetch(self): | ||
# Get latitude & longitude of address | ||
url = "https://geocoder.cit.api.here.com/6.2/search.json" | ||
|
||
params = { | ||
"gen": "9", | ||
"app_id": "pYZXmzEqjmR2DG66DRIr", | ||
"app_code": "T-Z-VT6e6I7IXGuqBfF_vQ", | ||
"country": "AUS", | ||
"state": "VIC", | ||
"searchtext": self._address, | ||
"bbox": "-37.86,145.36;-38.34,145.78", | ||
} | ||
|
||
r = requests.get(url, params=params) | ||
r.raise_for_status() | ||
|
||
lat_long = r.json()["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"] | ||
|
||
# Get waste collection zone by longitude and latitude | ||
url = "https://services3.arcgis.com/TJxZpUnYIJOvcYwE/arcgis/rest/services/WasteCollectionZones/FeatureServer/0/query" | ||
|
||
params ={ | ||
"f": "geojson", | ||
"outFields": "*", | ||
"returnGeometry": "true", | ||
"inSR": "4326", | ||
"spatialRel": "esriSpatialRelIntersects", | ||
"geometryType": "esriGeometryPoint", | ||
"geometry": str(lat_long["Longitude"]) + "," + str(lat_long["Latitude"]), | ||
} | ||
|
||
r = requests.get(url, params=params) | ||
r.raise_for_status() | ||
|
||
waste_schedule = r.json()["features"][0]["properties"] | ||
|
||
entries = [] | ||
|
||
entries.append( | ||
Collection( | ||
date = self.next_collection(waste_schedule["rub_day"], waste_schedule["rub_weeks"], waste_schedule["rub_start"]), | ||
t = "Rubbish", | ||
icon = ICON_MAP.get("Waste Type"), | ||
) | ||
) | ||
|
||
entries.append( | ||
Collection( | ||
date = self.next_collection(waste_schedule["rec_day"], waste_schedule["rec_weeks"], waste_schedule["rec_start"]), | ||
t = "Recycling", | ||
icon = ICON_MAP.get("Waste Type"), | ||
) | ||
) | ||
|
||
entries.append( | ||
Collection( | ||
date = self.next_collection(waste_schedule["grn_day"], waste_schedule["grn_weeks"], waste_schedule["grn_start"]), | ||
t = "Green Waste", | ||
icon = ICON_MAP.get("Waste Type"), | ||
) | ||
) | ||
|
||
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,32 @@ | ||
# Cardinia Shire Council | ||
|
||
Waste collection schedules provided by [Cardinia Shire Council](https://www.cardinia.vic.gov.au/). | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: cardinia_vic_gov_au | ||
args: | ||
address: ADDRESS # FORMATTING MUST BE EXACT, PLEASE SEE BELOW | ||
``` | ||
### Configuration Variables | ||
**address** | ||
*(string) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: cardinia_vic_gov_au | ||
args: | ||
address: 6-8 Main St, Nar Nar Goon Vic | ||
``` | ||
## How to get the correct address | ||
Search your address on [Cardinia Shire Council's Website](https://www.cardinia.vic.gov.au/info/20002/rubbish_and_recycling/385/bin_collection_days_and_putting_your_bins_out/2#check) to ensure you use the correct address format. Start typing the full address and the use autocomplete to search. After results have been found, copy the address exactly as it appears in the search box. |
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