Merge pull request #456 from craSH/nz-horowhenua-council-source

Add schedule for Horowhenua District, New Zealand
This commit is contained in:
Steffen Zimmermann
2022-12-17 12:06:39 +01:00
committed by GitHub
4 changed files with 168 additions and 0 deletions

View File

@@ -145,6 +145,7 @@ Currently the following service providers are supported:
- [Auckland](./doc/source/aucklandcouncil_govt_nz.md)
- [Christchurch](./doc/source/ccc_govt_nz.md)
- [Gore, Invercargill & Southland](./doc/source/wastenet_org_nz.md)
- [Horowhenua District](./doc/source/horowhenua_govt_nz.md)
- [Waipa District](./doc/source/waipa_nz.md)
- [Wellington](./doc/source/wellington_govt_nz.md)

View File

@@ -0,0 +1,118 @@
import datetime
import json
import requests
from bs4 import BeautifulSoup
from requests.utils import requote_uri
from waste_collection_schedule import Collection
TITLE = "Horowhenua District Council"
DESCRIPTION = "Source for Horowhenua District Council Rubbish & Recycling collection."
URL = "https://www.horowhenua.govt.nz/"
TEST_CASES = {
"House-Shannon": {
"post_code": "4821",
"town": "Shannon",
"street_name": "Bryce Street",
"street_number": "55",
},
"House-Levin": {
"post_code": "5510",
"town": "Levin",
"street_name": "McKenzie Street",
"street_number": "15",
},
"Commercial-Foxton": {
"post_code": "4814",
"town": "Foxton",
"street_name": "State Highway 1",
"street_number": "18",
},
}
API_URLS = {
"session":"https://www.horowhenua.govt.nz" ,
"search": "https://www.horowhenua.govt.nz/api/v1/myarea/search?keywords={}",
"schedule": "https://www.horowhenua.govt.nz/ocapi/Public/myarea/wasteservices?geolocationid={}&ocsvclang=en-AU",
}
HEADERS = {
"user-agent": "Mozilla/5.0",
}
ICON_MAP = {
"Rubbish": "mdi:trash-can",
"Recycling": "mdi:recycle",
}
# _LOGGER = logging.getLogger(__name__)
class Source:
def __init__(
self, post_code: str, town: str, street_name: str, street_number: str
):
self.post_code = post_code
self.town = town.upper()
self.street_name = street_name
self.street_number = 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"]))
r0 = s.get(q, headers = HEADERS)
# Do initial address search
address = "{} {} {} {}".format(self.street_number, self.street_name, self.town, self.post_code)
q = requote_uri(str(API_URLS["search"]).format(address))
r1 = s.get(q, headers = HEADERS)
data = json.loads(r1.text)
# Find the geolocation for the address
for item in data["Items"]:
normalized_input = Source.normalize_address(address)
normalized_response = Source.normalize_address(item['AddressSingleLine'])
if normalized_input in normalized_response:
locationId = item["Id"]
break
if locationId == 0:
return []
# Retrieve the upcoming collections for location
q = requote_uri(str(API_URLS["schedule"]).format(locationId))
r2 = s.get(q, headers = HEADERS)
data = json.loads(r2.text)
responseContent = data["responseContent"]
soup = BeautifulSoup(responseContent, "html.parser")
services = soup.find_all("article")
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()
entries.append(
Collection(
date = date,
t=waste_type, # api returns Recycling, Rubbish
icon=ICON_MAP.get(waste_type, "mdi:trash-can"),
)
)
return entries
@staticmethod
def normalize_address(address_str):
# Remove leading/trailing whitespace, capitalize
address_str = address_str.strip().upper()
# Replace any multiplewhite space characters with a single space
address_str = " ".join(address_str.split())
return address_str

View File

@@ -0,0 +1,48 @@
# Horowhenua District Council
Support for schedules provided by [Horowhenua District Council Kerbside Rubbish & Recycling Services](https://www.horowhenua.govt.nz/Services/Home-Property/Rubbish-Recycling/Kerbside-Rubbish-Recycling-Services).
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: horowhenua_govt_nz
args:
post_code: POST_CODE
town: TOWN
street_name: STREET_NAME
street_number: STREET_NUMBER
```
### Configuration Variables
**post_code**<br>
*(string) (required)*
**town**<br>
*(string) (required)*
**street_name**<br>
*(string) (required)*
**street_number**<br>
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: horowhenua_govt_nz
args:
post_code: 4814
town: Foxton
street_name: State Highway 1
street_number: 18
```
## How to get the source arguments
Visit the [Horowhenua District Council Waste and Recycling - Check my rubbish and recycling collection dates](https://www.horowhenua.govt.nz/Services/Home-Property/Rubbish-Recycling/Check-my-rubbish-and-recycling-collection-date) page and search for your address. The arguments should exactly match the results shown for Post Code, Town, Street and number portion of the Property.

View File

@@ -130,6 +130,7 @@ Currently the following service providers are supported:
- [Auckland](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/aucklandcouncil_govt_nz.md)
- [Christchurch](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ccc_govt_nz.md)
- [Gore, Invercargill & Southland](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/wastenet_org_nz.md)
- [Horowhenua District](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/horowhenua_govt_nz.md)
- [Waipa District](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/waipa_nz.md)
- [Wellington](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/wellington_govt_nz.md)