From a88a8b1d781699f9d1beec5601fe17dd5360b16d Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Sat, 13 Jul 2024 17:41:41 +0200 Subject: [PATCH] kuringgai_nsw_gov_au accept integer post_code and street_number --- .../source/kuringgai_nsw_gov_au.py | 45 +++++++++++-------- doc/source/kuringgai_nsw_gov_au.md | 4 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/kuringgai_nsw_gov_au.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/kuringgai_nsw_gov_au.py index b2931905b..b73b47433 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/kuringgai_nsw_gov_au.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/kuringgai_nsw_gov_au.py @@ -1,8 +1,8 @@ import datetime import json import re -import requests +import requests from bs4 import BeautifulSoup from requests.utils import requote_uri from waste_collection_schedule import Collection @@ -12,10 +12,10 @@ URL = "https://www.krg.nsw.gov.au" TEST_CASES = { "randomHouse": { - "post_code": "2070", + "post_code": 2070, "suburb": "LINDFIELD", "street_name": "Wolseley Road", - "street_number": "42", + "street_number": 42, }, "randomAppartment": { "post_code": "2074", @@ -24,7 +24,7 @@ "street_number": "4/9", }, "randomMultiunit": { - "post_code": "2075", + "post_code": 2075, "suburb": "ST IVES", "street_name": "Kitchener Street", "street_number": "99/2-8", @@ -33,7 +33,7 @@ "post_code": "2073", "suburb": "PYMBLE", "street_name": "Latona Street", - "street_number": "1", + "street_number": 1, }, "1A Latona St": { "post_code": "2073", @@ -45,7 +45,7 @@ API_URLS = { - "session":"https://www.krg.nsw.gov.au" , + "session": "https://www.krg.nsw.gov.au", "search": "https://www.krg.nsw.gov.au/api/v1/myarea/search?keywords={}", "schedule": "https://www.krg.nsw.gov.au/ocapi/Public/myarea/wasteservices?geolocationid={}&ocsvclang=en-AU", } @@ -71,26 +71,29 @@ class Source: def __init__( - self, post_code: str, suburb: str, street_name: str, street_number: str + self, + post_code: str | int, + suburb: str, + street_name: str, + street_number: str | int, ): - self.post_code = post_code + self.post_code = str(post_code) self.suburb = suburb.upper() self.street_name = street_name - self.street_number = street_number + self.street_number = str(street_number) def fetch(self): - locationId = 0 # 'collection' api call seems to require an ASP.Net_sessionID, so obtain the relevant cookie s = requests.Session() q = requote_uri(str(API_URLS["session"])) - s.get(q, headers = HEADERS) + s.get(q, headers=HEADERS) # Do initial address search - address = "{} {}, {} NSW {}".format(self.street_number, self.street_name, self.suburb, self.post_code) + address = f"{self.street_number} {self.street_name}, {self.suburb} NSW {self.post_code}" q = requote_uri(str(API_URLS["search"]).format(address)) - r1 = s.get(q, headers = HEADERS) + r1 = s.get(q, headers=HEADERS) data = json.loads(r1.text)["Items"] expected_address_regexp = re.compile( @@ -113,7 +116,7 @@ def fetch(self): # Retrieve the upcoming collections for location q = requote_uri(str(API_URLS["schedule"]).format(locationId)) - r2 = s.get(q, headers = HEADERS) + r2 = s.get(q, headers=HEADERS) data = json.loads(r2.text) responseContent = data["responseContent"] @@ -123,13 +126,17 @@ def fetch(self): entries = [] for item in services: - waste_type = item.find('h3').text - date = datetime.datetime.strptime(item.find('div', {'class': 'next-service'}).text.strip(), "%a %d/%m/%Y").date() + waste_type = item.find("h3").text + date = datetime.datetime.strptime( + item.find("div", {"class": "next-service"}).text.strip(), "%a %d/%m/%Y" + ).date() entries.append( Collection( - date = date, - # t=waste_type, # api returns GeneralWaste, Recycling, GreenWaste - t = ROUNDS.get(waste_type), # returns user-friendly General Waste, Recycling, Green Waste + date=date, + # t=waste_type, # api returns GeneralWaste, Recycling, GreenWaste + t=ROUNDS.get( + waste_type + ), # returns user-friendly General Waste, Recycling, Green Waste icon=ICON_MAP.get(waste_type), ) ) diff --git a/doc/source/kuringgai_nsw_gov_au.md b/doc/source/kuringgai_nsw_gov_au.md index 6416a1080..53ed2cefb 100644 --- a/doc/source/kuringgai_nsw_gov_au.md +++ b/doc/source/kuringgai_nsw_gov_au.md @@ -36,10 +36,10 @@ waste_collection_schedule: sources: - name: kuringgai_nsw_gov_au args: - post_code: 2070 + post_code: "2070" suburb: LINDFIELD street_name: Wolseley Road - street_number: 42 + street_number: "42" ``` ## How to get the source arguments