-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Source: Blacktown City Council (#1382)
* Added Blacktown City Council * raise error instad of returning [] + Reformatting --------- Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
194 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
128 changes: 128 additions & 0 deletions
128
...onents/waste_collection_schedule/waste_collection_schedule/source/blacktown_nsw_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,128 @@ | ||
import datetime | ||
import json | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from requests.utils import requote_uri | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
TITLE = "Blacktown City Council (NSW)" | ||
DESCRIPTION = "Source for Blacktown City Council rubbish collection." | ||
URL = "https://www.blacktown.nsw.gov.au/" | ||
TEST_CASES = { | ||
"Plumpton Marketplace": { | ||
"post_code": "2761", | ||
"suburb": "Plumpton", | ||
"street_name": "Jersey Rd", | ||
"street_number": "260", | ||
}, | ||
"Rooty Hill Tennis & Squash Centre": { | ||
"post_code": "2766", | ||
"suburb": "Rooty Hill", | ||
"street_name": "Learmonth St", | ||
"street_number": "13-15", | ||
}, | ||
"Workers Blacktown": { | ||
"post_code": "2148", | ||
"suburb": "Blacktown", | ||
"street_name": "Campbell St", | ||
"street_number": "55", | ||
}, | ||
"Hythe St": { | ||
"post_code": "2770", | ||
"suburb": "Mount Druitt", | ||
"street_name": "Hythe St", | ||
"street_number": "9-11", | ||
}, | ||
} | ||
|
||
API_URLS = { | ||
"address_search": "https://www.blacktown.nsw.gov.au/api/v1/myarea/search?keywords={}", | ||
"collection": "https://www.blacktown.nsw.gov.au/ocapi/Public/myarea/wasteservices?geolocationid={}&ocsvclang=en-AU", | ||
} | ||
|
||
HEADERS = {"user-agent": "Mozilla/5.0"} | ||
|
||
ICON_MAP = { | ||
"General Waste": "trash-can", | ||
"Recycling": "mdi:recycle", | ||
} | ||
|
||
|
||
class Source: | ||
def __init__( | ||
self, post_code: str, suburb: str, street_name: str, street_number: str | ||
): | ||
self.post_code = post_code | ||
self.suburb = suburb | ||
self.street_name = street_name | ||
self.street_number = street_number | ||
|
||
def fetch(self): | ||
locationId = 0 | ||
|
||
address = "{} {} {} NSW {}".format( | ||
self.street_number, self.street_name, self.suburb, self.post_code | ||
) | ||
|
||
q = requote_uri(str(API_URLS["address_search"]).format(address)) | ||
|
||
# Retrieve suburbs | ||
r = requests.get(q, headers=HEADERS) | ||
|
||
data = json.loads(r.text) | ||
|
||
# Find the ID for our suburb | ||
for item in data["Items"]: | ||
locationId = item["Id"] | ||
break | ||
|
||
if locationId == 0: | ||
raise ValueError( | ||
f"Unable to find location ID for {address}, maybe you misspelled your address?" | ||
) | ||
|
||
# Retrieve the upcoming collections for our property | ||
q = requote_uri(str(API_URLS["collection"]).format(locationId)) | ||
|
||
r = requests.get(q, headers=HEADERS) | ||
|
||
data = json.loads(r.text) | ||
|
||
responseContent = data["responseContent"] | ||
|
||
soup = BeautifulSoup(responseContent, "html.parser") | ||
services = soup.find_all("div", attrs={"class": "waste-services-result"}) | ||
|
||
entries = [] | ||
|
||
for item in services: | ||
# test if <div> contains a valid date. If not, is is not a collection item. | ||
date_text = item.find("div", attrs={"class": "next-service"}) | ||
|
||
# The date format currently used on https://www.blacktown.nsw.gov.au/Services/Waste-services-and-collection/Waste-collection-days | ||
date_format = "%a %d/%m/%Y" | ||
|
||
try: | ||
# Strip carriage returns and newlines out of the HTML content | ||
cleaned_date_text = ( | ||
date_text.text.replace("\r", "").replace("\n", "").strip() | ||
) | ||
|
||
# Parse the date | ||
date = datetime.datetime.strptime(cleaned_date_text, date_format).date() | ||
|
||
except ValueError: | ||
continue | ||
|
||
waste_type = item.find("h3").text.strip() | ||
|
||
entries.append( | ||
Collection( | ||
date=date, | ||
t=waste_type, | ||
icon=ICON_MAP.get(waste_type, "mdi:trash-can"), | ||
) | ||
) | ||
|
||
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,64 @@ | ||
# Blacktown City Council (NSW) | ||
|
||
Support for schedules provided by [Blacktown City Council Waste and Recycling](https://www.blacktown.nsw.gov.au/Services/Waste-services-and-collection/Waste-collection-days). | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: blacktown_nsw_gov_au | ||
args: | ||
post_code: POST_CODE | ||
suburb: SUBURB | ||
street_name: STREET_NAME | ||
street_number: STREET_NUMBER | ||
``` | ||
### Configuration Variables | ||
**post_code** | ||
*(string) (required)* | ||
**suburb** | ||
*(string) (required)* | ||
**street_name** | ||
*(string) (required)* | ||
**street_number** | ||
*(string) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: blacktown_nsw_gov_au | ||
args: | ||
post_code: 2770 | ||
suburb: Emerton | ||
street_name: Helena Ave | ||
street_number: 80 | ||
``` | ||
## How to get the source arguments | ||
Visit the [Blacktown City Council Waste Collection Days](https://www.blacktown.nsw.gov.au/Services/Waste-services-and-collection/Waste-collection-days) page, follow the quick link to *Check my collection day*, and search for your address. The street address arguments used to configure hacs_waste_collection_schedule should exactly match the street address shown in the autocomplete result. | ||
## How this integration uses Blacktown Council's APIs | ||
Two API calls are currently needed to retrieve waste collection schedule results from Blacktown Council: | ||
1. The address search API at https://www.blacktown.nsw.gov.au/api/v1/myarea/search | ||
2. The waste services API at https://www.blacktown.nsw.gov.au/ocapi/Public/myarea/wasteservices | ||
This integration does the following: | ||
1. Calls the address search API to retrieve the "location ID" for the given location. Eg. https://www.blacktown.nsw.gov.au/api/v1/myarea/search?keywords=80+Helena+Ave+Emerton+NSW+2770 | ||
2. Retrieves waste/collection info from the waste services API using the "location ID" retrieved in step #1. Eg. https://www.blacktown.nsw.gov.au/ocapi/Public/myarea/wasteservices?geolocationid=6177cbfa-6f35-4fbf-9208-63d4dde7f048&ocsvclang=en-AU | ||
3. Parses the HTML returned by the waste services API in step #2 to extract the data | ||
# Similarities with other sources | ||
For future work it's good to note that Blacktown City Council uses the same APIs as Campbelltown City Council, to the point I was able to copy and paste their files with minimal modification. | ||
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