Skip to content

Commit

Permalink
feat: Adding North Hertfordshire District Council
Browse files Browse the repository at this point in the history
fix: #1028
  • Loading branch information
m26dvd committed Nov 20, 2024
1 parent 35691a1 commit 5460fe2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
7 changes: 7 additions & 0 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,13 @@
"wiki_name": "North East Lincolnshire Council",
"wiki_note": "Replace XXXXXXXX with your UPRN."
},
"NorthHertfordshireDistrictCouncil": {
"house_number": "2",
"postcode": "SG6 4BJ",
"url": "https://www.north-herts.gov.uk",
"wiki_name": "North Hertfordshire District Council",
"wiki_note": "Pass the house number and postcode in their respective parameters."
},
"NorthKestevenDistrictCouncil": {
"url": "https://www.n-kesteven.org.uk/bins/display?uprn=100030869513",
"wiki_command_url_override": "https://www.n-kesteven.org.uk/bins/display?uprn=XXXXXXXX",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import requests
from bs4 import BeautifulSoup

from uk_bin_collection.uk_bin_collection.common import *
from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass


# import the wonderful Beautiful Soup and the URL grabber
class CouncilClass(AbstractGetBinDataClass):
"""
Concrete classes have to implement all abstract operations of the
base class. They can also override some operations with a default
implementation.
"""

def parse_data(self, page: str, **kwargs) -> dict:

user_postcode = kwargs.get("postcode")
user_paon = kwargs.get("paon")
check_postcode(user_postcode)
check_paon(user_paon)
bindata = {"bins": []}

URI = "https://uhtn-wrp.whitespacews.com/"

session = requests.Session()

# get link from first page as has some kind of unique hash
r = session.get(
URI,
)
r.raise_for_status()
soup = BeautifulSoup(r.text, features="html.parser")

alink = soup.find("a", text="Find my bin collection day")

if alink is None:
raise Exception("Initial page did not load correctly")

# greplace 'seq' query string to skip next step
nextpageurl = alink["href"].replace("seq=1", "seq=2")

data = {
"address_name_number": user_paon,
"address_postcode": user_postcode,
}

# get list of addresses
r = session.post(nextpageurl, data)
r.raise_for_status()

soup = BeautifulSoup(r.text, features="html.parser")

# get first address (if you don't enter enough argument values this won't find the right address)
alink = soup.find("div", id="property_list").find("a")

if alink is None:
raise Exception("Address not found")

nextpageurl = URI + alink["href"]

# get collection page
r = session.get(
nextpageurl,
)
r.raise_for_status()
soup = BeautifulSoup(r.text, features="html.parser")

if soup.find("span", id="waste-hint"):
raise Exception("No scheduled services at this address")

u1s = soup.find("section", id="scheduled-collections").find_all("u1")

for u1 in u1s:
lis = u1.find_all("li", recursive=False)

date = lis[1].text.replace("\n", "")
bin_type = lis[2].text.replace("\n", "")

dict_data = {
"type": bin_type,
"collectionDate": datetime.strptime(
date,
"%d/%m/%Y",
).strftime(date_format),
}
bindata["bins"].append(dict_data)

bindata["bins"].sort(
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format)
)

return bindata
13 changes: 13 additions & 0 deletions wiki/Councils.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ This document is still a work in progress, don't worry if your council isn't lis
- [North Ayrshire Council](#north-ayrshire-council)
- [North East Derbyshire District Council](#north-east-derbyshire-district-council)
- [North East Lincolnshire Council](#north-east-lincolnshire-council)
- [North Hertfordshire District Council](#north-hertfordshire-district-council)
- [North Kesteven District Council](#north-kesteven-district-council)
- [North Lanarkshire Council](#north-lanarkshire-council)
- [North Lincolnshire Council](#north-lincolnshire-council)
Expand Down Expand Up @@ -2036,6 +2037,18 @@ Note: Replace XXXXXXXX with your UPRN.

---

### North Hertfordshire District Council
```commandline
python collect_data.py NorthHertfordshireDistrictCouncil https://www.north-herts.gov.uk -p "XXXX XXX" -n XX
```
Additional parameters:
- `-p` - postcode
- `-n` - house number

Note: Pass the house number and postcode in their respective parameters.

---

### North Kesteven District Council
```commandline
python collect_data.py NorthKestevenDistrictCouncil https://www.n-kesteven.org.uk/bins/display?uprn=XXXXXXXX
Expand Down

0 comments on commit 5460fe2

Please sign in to comment.