Skip to content

Commit

Permalink
kuringgai_nsw_gov_au accept integer post_code and street_number
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Jul 13, 2024
1 parent 50953dc commit a88a8b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
}
Expand All @@ -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(
Expand All @@ -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"]

Expand All @@ -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),
)
)
Expand Down
4 changes: 2 additions & 2 deletions doc/source/kuringgai_nsw_gov_au.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a88a8b1

Please sign in to comment.