Skip to content

Commit

Permalink
feat: Adding Brent Council
Browse files Browse the repository at this point in the history
fix: #1109
  • Loading branch information
m26dvd committed Jan 5, 2025
1 parent 6b451e8 commit 5de7b0a
Show file tree
Hide file tree
Showing 3 changed files with 135 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 @@ -244,6 +244,13 @@
"wiki_name": "Breckland Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"BrentCouncil": {
"house_number": "25",
"postcode": "HA3 0QU",
"url": "https://recyclingservices.brent.gov.uk/waste",
"wiki_name": "Brent Council",
"wiki_note": "Pass the house number and postcode in their respective parameters."
},
"BrightonandHoveCityCouncil": {
"house_number": "44 Carden Avenue, Brighton, BN1 8NE",
"postcode": "BN1 8NE",
Expand Down
115 changes: 115 additions & 0 deletions uk_bin_collection/uk_bin_collection/councils/BrentCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from time import sleep

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:
data = {"bins": []}
user_postcode = kwargs.get("postcode")
user_paon = kwargs.get("paon")
check_postcode(user_postcode)
check_paon(user_paon)

URI = "https://recyclingservices.brent.gov.uk/waste"

payload = {"postcode": user_postcode}

s = requests.Session()

# Make the POST request
response = s.post(URI, data=payload)

# Make a BS4 object
soup = BeautifulSoup(response.content, features="html.parser")

address_list = soup.find_all("option")

current_year = datetime.now().year
next_year = current_year + 1

for address in address_list:
if user_paon in (address.text):
address_id = address.get("value")
URI = f"https://recyclingservices.brent.gov.uk/waste/{address_id}"

counter = 0
r = s.get(URI)
while "Loading your bin days..." in r.text:
counter = counter + 1
if counter == 20:
return data
sleep(2)
r = s.get(URI)

r.raise_for_status()

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

wastecollections = soup.find("div", {"class": "waste__collections"})

# Find all waste service sections
waste_services = wastecollections.find_all(
"h3", class_="govuk-heading-m waste-service-name"
)

for service in waste_services:
# Get the collection type (e.g., Rubbish, Recycling)
collection_type = (service.get_text(strip=True)).split("\n")[0]

# Find the sibling container holding details
service_details = service.find_next(
"dl", class_="govuk-summary-list"
)

if service_details:

# Extract next collection date
next_collection_row = service_details.find(
"dt", string="Next collection"
)
next_collection = (
next_collection_row.find_next_sibling("dd").get_text(
strip=True
)
if next_collection_row
else "Unknown"
)

# Parse dates into standard dd/mm/yyyy format
next_collection_date = datetime.strptime(
remove_ordinal_indicator_from_date_string(next_collection),
"%A, %d %B",
)

if (datetime.now().month == 12) and (
next_collection.month == 1
):
next_collection_date = next_collection_date.replace(
year=next_year
)
else:
next_collection_date = next_collection_date.replace(
year=current_year
)

dict_data = {
"type": collection_type.strip(),
"collectionDate": next_collection_date.strftime(
date_format
),
}
data["bins"].append(dict_data)

return data
13 changes: 13 additions & 0 deletions wiki/Councils.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This document is still a work in progress, don't worry if your council isn't lis
- [Bradford MDC](#bradford-mdc)
- [Braintree District Council](#braintree-district-council)
- [Breckland Council](#breckland-council)
- [Brent Council](#brent-council)
- [Brighton and Hove City Council](#brighton-and-hove-city-council)
- [Bristol City Council](#bristol-city-council)
- [Bromley Borough Council](#bromley-borough-council)
Expand Down Expand Up @@ -671,6 +672,18 @@ Note: You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/searc

---

### Brent Council
```commandline
python collect_data.py BrentCouncil https://recyclingservices.brent.gov.uk/waste -p "XXXX XXX" -n XX
```
Additional parameters:
- `-p` - postcode
- `-n` - house number

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

---

### Brighton and Hove City Council
```commandline
python collect_data.py BrightonandHoveCityCouncil https://cityclean.brighton-hove.gov.uk/link/collections -s -u XXXXXXXX -p "XXXX XXX" -n XX -w http://HOST:PORT/
Expand Down

0 comments on commit 5de7b0a

Please sign in to comment.