Add source: darebin_vic_gov_au (#2199)

* Add source: darebin_vic_gov_au

* add test case + fix _get_next_n_dates delta being optional + reformatting

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Joshua Morgan
2024-06-30 01:44:37 +10:00
committed by GitHub
parent da69463c06
commit f2cd962eca
4 changed files with 150 additions and 1 deletions

View File

@@ -37,6 +37,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Cardinia Shire Council](/doc/source/cardinia_vic_gov_au.md) / cardinia.vic.gov.au
- [City of Ballarat](/doc/source/ballarat_vic_gov_au.md) / ballarat.vic.gov.au
- [City of Canada Bay Council](/doc/source/canadabay_nsw_gov_au.md) / canadabay.nsw.gov.au
- [City of Darebin](/doc/source/darebin_vic_gov_au.md) / darebin.vic.gov.au
- [City of Greater Geelong](/doc/source/geelongaustralia_com_au.md) / geelongaustralia.com.au
- [City of Kingston](/doc/source/kingston_vic_gov_au.md) / kingston.vic.gov.au
- [City of Onkaparinga Council](/doc/source/onkaparingacity_com.md) / onkaparingacity.com

View File

@@ -0,0 +1,115 @@
from datetime import date, timedelta
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "City of Darebin"
DESCRIPTION = "Source for City of Darebin waste collection."
URL = "https://www.darebin.vic.gov.au/"
TEST_CASES = {
"274 Gower Street PRESTON 3072": {
"property_location": "274 Gower Street PRESTON 3072"
},
}
API_URL = "https://services-ap1.arcgis.com/1WJBRkF3v1EEG5gz/arcgis/rest/services/Waste_Collection_Date/FeatureServer/0/query"
WEEKDAY_MAP = {
"Monday": 0,
"Tuesday": 1,
"Wednesday": 2,
"Thursday": 3,
"Friday": 4,
"Saturday": 5,
"Sunday": 6,
}
def _get_next_n_dates(date_obj: date, n: int, delta: timedelta):
next_dates = []
for _ in range(n):
date_obj += delta
while date_obj <= date.today():
date_obj += delta
next_dates.append(date_obj)
return next_dates
def _get_previous_date_for_day_of_week(day_of_week: int):
today = date.today()
return today - timedelta((today.weekday() - day_of_week + 7) % 7)
class Source:
def __init__(self, property_location: str):
self.property_location = property_location
def fetch(self) -> list[Collection]:
params = {
"where": f"EZI_ADDRESS LIKE '{self.property_location}%'",
"outFields": "EZI_ADDRESS,OBJECTID",
"f": "json",
}
r = requests.get(API_URL, params=params)
r.raise_for_status()
data = r.json()
features = data.get("features")
attributes = features[0]["attributes"]
object_id = attributes["OBJECTID"]
params = {
"f": "json",
"objectIds": object_id,
"outFields": "Collection_Day,Condition,EZI_ADDRESS,Green_Collection,Hard_Rubbish_Week_Start,Recycle_Collection",
}
r = requests.get(API_URL, params=params)
r.raise_for_status()
data = r.json()
features = data.get("features")
attributes = features[0]["attributes"]
green_collection_epoch_seconds = attributes["Green_Collection"] / 1000
recycle_collection_epoch_seconds = attributes["Recycle_Collection"] / 1000
collection_day = attributes["Collection_Day"]
next_collection_date = _get_previous_date_for_day_of_week(
WEEKDAY_MAP[collection_day]
)
green_collection = date.fromtimestamp(green_collection_epoch_seconds)
recycle_collection = date.fromtimestamp(recycle_collection_epoch_seconds)
green_collection_dates = _get_next_n_dates(
green_collection, 52, timedelta(days=14)
)
recycle_collection_dates = _get_next_n_dates(
recycle_collection, 52, timedelta(days=14)
)
waste_collection_dates = _get_next_n_dates(
next_collection_date, 52, timedelta(days=7)
)
entries = []
entries.extend(
[
Collection(date=collection_date, t="Green Waste", icon="mdi:leaf")
for collection_date in green_collection_dates
]
)
entries.extend(
[
Collection(date=collection_date, t="Recycling", icon="mdi:recycle")
for collection_date in recycle_collection_dates
]
)
entries.extend(
[
Collection(date=collection_day, t="Rubbish", icon="mdi:trash-can")
for collection_day in waste_collection_dates
]
)
return entries

View File

@@ -0,0 +1,33 @@
# City of Darebin
Support for schedules provided by [City of Darebin](https://www.darebin.vic.gov.au/Waste-environment-and-climate/Bins-and-waste-collection/When-is-my-bin-collection-day).
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: darebin_vic_gov_au
args:
property_location: PROPERTY_LOCATION
```
### Configuration Variables
**property_location**
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: darebin_vic_gov_au
args:
property_location: 274 Gower Street PRESTON 3072
```
## How to get the source arguments
Visit the [City of Darebin Find my bin collection day page](https://darebin.maps.arcgis.com/apps/instant/basic/index.html?appid=51d4de7339f84dd5a6d2790cb2081be2) page and search for your address. The argument should exactly match the result shown for Address portion of the Property Information.

File diff suppressed because one or more lines are too long