Skip to content

Commit

Permalink
fixed hasting_gov_uk
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Nov 18, 2024
1 parent c0090b5 commit a4d3d26
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 63 deletions.
12 changes: 8 additions & 4 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13565,12 +13565,14 @@
"description": "Configure your service provider. \n\nAn easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/hastings_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"house_number": "House Number",
"postcode": "Postcode",
"house_number": "House Number (Deprecated, leave empty)",
"postcode": "Postcode (Deprecated, leave empty)",
"uprn": "Unique Property Reference Number (UPRN)"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used.",
"house_number": "Deprecated, leave mpty, will be ignored",
"postcode": "Deprecated, leave mpty, will be ignored",
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details"
}
},
Expand All @@ -13579,11 +13581,13 @@
"description": "Configure your service provider. \n\nAn easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/hastings_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"house_number": "House Number",
"postcode": "Postcode",
"house_number": "House Number (Deprecated, leave empty)",
"postcode": "Postcode (Deprecated, leave empty)",
"uprn": "Unique Property Reference Number (UPRN)"
},
"data_description": {
"house_number": "Deprecated, leave mpty, will be ignored",
"postcode": "Deprecated, leave mpty, will be ignored",
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from datetime import datetime

import requests
from bs4 import BeautifulSoup
import urllib3
from waste_collection_schedule import Collection # type: ignore[attr-defined]

# With verify=True the POST fails due to a SSLCertVerificationError.
# Using verify=False works, but is not ideal. The following links may provide a better way of dealing with this:
# https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
# https://urllib3.readthedocs.io/en/1.26.x/user-guide.html#ssl
# This line suppresses the InsecureRequestWarning when using verify=False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


TITLE = "Hastings Borough Council"
DESCRIPTION = "Source for hastings.gov.uk services for Hastings Borough Council, UK."
URL = "https://www.hastings.gov.uk/"
API_URL = "https://www.hastings.gov.uk/waste_recycling/lookup/"
HEADERS = {"user-agent": "Mozilla/5.0"}
API_URL = "https://el.hastings.gov.uk/MyArea/CollectionDays.asmx/LookupCollectionDaysByService"
TEST_CASES = {
"Test_001": {"postcode": "TN34 1QF", "house_number": 36, "uprn": 100060038877},
"Test_002": {"postcode": "TN34 2DL", "house_number": "28A", "uprn": "10070609836"},
Expand All @@ -24,72 +31,57 @@
PARAM_TRANSLATIONS = {
"en": {
"uprn": "Unique Property Reference Number (UPRN)",
"postcode": "Postcode (Deprecated, leave empty)",
"house_number": "House Number (Deprecated, leave empty)",
}
}
PARAM_DESCRIPTIONS = {
"en": {
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details",
"postcode": "Deprecated, leave mpty, will be ignored",
"house_number": "Deprecated, leave mpty, will be ignored",
}
}


class Source:
def __init__(self, postcode: str, house_number: str | int, uprn: str | int):
self._postcode = str(postcode)
self._house = str(house_number)
def __init__(
self,
uprn: str | int,
postcode: (
str | None
) = None, # Not used but kept for compatibility with old version
house_number: (
str | int | None
) = None, # Not used but kept for compatibility with old version
):
self._uprn = str(uprn)

def get_viewstate(self, content: str) -> dict:
tags = {}
soup = BeautifulSoup(content, "html.parser")
hidden_tags = soup.findAll("input", type="hidden")
for _, tag in enumerate(hidden_tags):
tags[tag.get("name")] = tag.get("value")
return tags

def fetch(self):
s = requests.Session()
payload = {"Uprn": self._uprn}

# visit webpage to get viewstate info
r = s.get(API_URL, headers=HEADERS)
r.raise_for_status
r = requests.post(API_URL, json=payload, verify=False)
r.raise_for_status()

# update payload and perform search
payload = self.get_viewstate(r.content)
payload.update(
{
"ctl00$leftCol$postcode": self._postcode,
"ctl00$leftCol$propertyNum": self._house,
"ctl00$leftCol$ctl05": "Find Address",
}
)
r = s.post(API_URL, data=payload, headers=HEADERS)
r.raise_for_status
data = r.json()["d"]

# if more than 1 match is returned, additional search using uprn is required
if "ctl00_leftCol_AddressList" in r.text:
payload = self.get_viewstate(r.content)
payload.update(
{
"ctl00$leftCol$addresses": self._uprn,
"ctl00$leftCol$ctl07": "Find Collection",
}
entries = []
for service in data:
waste_type: str = (
service["Service"].removesuffix("collection service").strip()
)
r = s.post(API_URL, data=payload, headers=HEADERS)
r.raise_for_status

soup = BeautifulSoup(r.text, "html.parser")
icon = ICON_MAP.get(waste_type)
date_str: str

entries = []
for waste in ICON_MAP:
container = soup.findAll("p", {"id": f"{waste}"})
pickups = container[0].text.split("\r\n ")
for item in pickups[1:]:
for date_str in service["Dates"]:
entries.append(
Collection(
date=datetime.strptime(item.strip(), "%A %d %B %Y").date(),
t=waste,
icon=ICON_MAP.get(waste),
date=datetime.fromtimestamp(
int(date_str.strip("/").removeprefix("Date").strip("()"))
/ 1000
).date(),
t=waste_type,
icon=icon,
)
)

Expand Down
10 changes: 0 additions & 10 deletions doc/source/hastings_gov_uk.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ waste_collection_schedule:
sources:
- name: hastings_gov_uk
args:
postcode: POSTCODE
house_humber: HOUSE_NUMBER
uprn: UPRN
```
### Configuration Variables
**postcode**
*(string) (required)*
**house_number**
*(string) (required)*
**uprn**
*(string) (required)*
Expand All @@ -32,8 +24,6 @@ waste_collection_schedule:
sources:
- name: hastings_gov_uk
args:
postcode: "TN34 2DL"
house_number: "28A"
uprn: "10070609836"
```
Expand Down

0 comments on commit a4d3d26

Please sign in to comment.