Basildon source updated to new provider (#1622)

* Basildon source updated to new provider

* Corrected fetching icon mapping

* basildon_gov_uk allow uprn or address + reformating

---------

Co-authored-by: Eyean <ian.hartwell1986@gmail.com>
Co-authored-by: Ian Hartwell <contactcentrevision+ian@gmail.com>
Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Eyeanman
2024-01-03 11:22:31 +00:00
committed by GitHub
parent c1356b3a58
commit e1ea6a9910
2 changed files with 126 additions and 45 deletions

View File

@@ -1,63 +1,116 @@
import requests
import bs4
import re
# Credit where it's due:
# This is predominantly a refactoring of the Bristol City Council script from the UKBinCollectionData repo
# https://github.com/robbrad/UKBinCollectionData
from datetime import datetime
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Basildon Council"
DESCRIPTION = "Source for basildon.gov.uk services for Basildon Council, UK."
URL = "https://basildon.gov.uk"
TEST_CASES = {
"Test_001": {"postcode": "CM111BJ", "address": "6, HEADLEY ROAD"},
"Test_002": {"postcode": "SS14 1QU", "address": "25, LONG RIDING"}
}
HEADERS = {
"user-agent": "Mozilla/5.0",
"Test_Addres_001": {"postcode": "CM111BJ", "address": "6, HEADLEY ROAD"},
"Test_Addres_002": {"postcode": "SS14 1QU", "address": "25 LONG RIDING"},
"Test_UPRN_001": {"uprn": "100090277795"},
"Test_UPRN_002": {"uprn": 10024197625},
"Test_UPRN_003": {"uprn": "10090455610"},
}
ICON_MAP = {
"DryNext": "mdi:recycle",
"GlassNext": "mdi:glass-fragile",
"RubbishNext": "mdi:trash-can",
"GardenAndFoodNext": "mdi:leaf",
"green_waste": "mdi:leaf",
"general_waste": "mdi:trash-can",
"food_waste": "mdi:food",
"glass_waste": "mdi:bottle-wine",
"papercard_waste": "mdi:package-varient",
"plasticcans_waste": "mdi:bottle-soda-classic",
}
NAME_MAP = {
"green_waste": "Garden",
"general_waste": "General",
"food_waste": "Food",
"glass_waste": "Glass",
"papercard_waste": "Paper/Cardboard",
"plasticcans_waste": "Plastic/Cans",
}
HEADERS = {
"Accept": "*/*",
"Accept-Language": "en-GB,en;q=0.9",
"Connection": "keep-alive",
"Ocp-Apim-Trace": "true",
"Origin": "https://mybasildon.powerappsportals.com",
"Referer": "https://mybasildon.powerappsportals.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"Sec-GPC": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
}
class Source:
def __init__(self, postcode, address):
def __init__(self, postcode=None, address=None, uprn=None):
if uprn is None and (postcode is None or address is None):
raise ValueError("Either uprn or postcode and address must be provided")
self._uprn = str(uprn).zfill(12) if uprn is not None else None
self._postcode = postcode
self._address = address
def fetch(self):
s = requests.Session()
data = {
"__Click": "$Refresh",
"txtPostcode": self._postcode,
"txtAddress": self._address
}
schedule_request = s.post(
f"https://www3.basildon.gov.uk/website2/postcodes.nsf/frmMyBasildon",
headers=HEADERS,
data=data
def compare_address(self, address) -> bool:
return (
self._address.replace(",", "").replace(" ", "").upper()
== address.replace(",", "").replace(" ", "").upper()
)
html_rowdata = schedule_request.content
rowdata = bs4.BeautifulSoup(html_rowdata, "html.parser")
# Extract bin types and next collection dates
def get_uprn(self, s):
r = s.post(
"https://basildonportal.azurewebsites.net/api/listPropertiesByPostcode",
headers=HEADERS,
json={"postcode": self._postcode},
)
r.raise_for_status()
data = r.json()
if data["result"] != "success":
raise ValueError("Invalid postcode")
for item in data["properties"]:
if self.compare_address(item["line1"]):
self._uprn = item["uprn"]
break
if self._uprn is None:
raise ValueError("Invalid address")
def fetch(self):
s = requests.Session()
if self._uprn is None:
self.get_uprn(s)
# Retrieve the schedule
payload = {"uprn": self._uprn}
response = s.post(
"https://basildonportal.azurewebsites.net/api/getPropertyRefuseInformation",
headers=HEADERS,
json=payload,
)
data = response.json()["refuse"]["available_services"]
entries = []
for bintype in ICON_MAP:
for i in range(1,7):
bintype_date = rowdata.find('input', {'name': f'{bintype}{i}'})
if bintype_date:
date_str = bintype_date['value']
date_str = re.sub(r'\b(\d+)(st|nd|rd|th)\b', r'\1', date_str)
for item in ICON_MAP:
for collection_date_key in [
"current_collection_",
"next_collection_",
"last_collection_",
]:
if data[item][collection_date_key + "active"]:
date_string = data[item][collection_date_key + "date"]
entries.append(
Collection(
t=bintype.replace("Next",""),
date=datetime.strptime(date_str, "%a, %d %b %Y").date(),
icon=ICON_MAP.get(bintype)
date=datetime.strptime(
date_string,
"%Y-%m-%d",
).date(),
t=NAME_MAP[item],
icon=ICON_MAP.get(item),
)
)

View File

@@ -4,21 +4,35 @@ Support for schedules provided by [Basildon Council](https://www3.basildon.gov.u
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: basildon_gov_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
```
or
```yaml
waste_collection_schedule:
sources:
- name: basildon_gov_uk
args:
postcode: POSTCODE
address: FIRST LINE OF ADDRESS (X, STREET NAME)
address: ADDRESS
```
### Configuration Variables
**uprn**
*(string) (required if postcode and address is not provided)*
**postcode**
*(string) (required)*
*(string) (required if no uprn provided)*
**address**
*(string) (required)*
*(string) (required if no uprn provided)*
## Example
@@ -27,10 +41,24 @@ waste_collection_schedule:
sources:
- name: basildon_gov_uk
args:
postcode: CM111BJ
address: 6, HEADLEY ROAD
uprn: "100090277795"
```
## How to get the source argument
```yaml
waste_collection_schedule:
sources:
- name: basildon_gov_uk
args:
postcode: "SS14 1QU"
address: "25 LONG RIDING"
```
An easy way to discover your Postcode and address used by this service is by going to <https://www3.basildon.gov.uk/website2/postcodes.nsf/frmMyBasildon> and entering in your address details.
## How to get the source arguments
### Using postcode and address
Go to <https://mybasildon.powerappsportals.com/check/where_i_live/> and enter your postcode and address. Then use the postcode and address as arguments.
### Using UPRN
An easy way to find your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering your address details.