diff --git a/uk_bin_collection/tests/input.json b/uk_bin_collection/tests/input.json index 604ae27f30..eb16129582 100755 --- a/uk_bin_collection/tests/input.json +++ b/uk_bin_collection/tests/input.json @@ -636,6 +636,14 @@ "wiki_name": "East Lindsey District Council", "wiki_note": "Pass the house name/number and postcode in their respective parameters. This parser requires a Selenium webdriver." }, + "EastLothianCouncil": { + "house_number": "Flat 1", + "postcode": "EH21 6QA", + "skip_get_url": true, + "url": "https://eastlothian.gov.uk", + "wiki_name": "East Lothian Council", + "wiki_note": "Pass the house number and postcode in their respective parameters" + }, "EastRenfrewshireCouncil": { "house_number": "23", "postcode": "G46 6RG", diff --git a/uk_bin_collection/uk_bin_collection/councils/EastLothianCouncil.py b/uk_bin_collection/uk_bin_collection/councils/EastLothianCouncil.py new file mode 100644 index 0000000000..d34e99c507 --- /dev/null +++ b/uk_bin_collection/uk_bin_collection/councils/EastLothianCouncil.py @@ -0,0 +1,83 @@ +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 = "http://collectiondates.eastlothian.gov.uk/ajax/your-calendar/load-streets-spring-2024.asp" + + payload = { + "postcode": user_postcode, + } + + headers = { + "Referer": "http://collectiondates.eastlothian.gov.uk/your-calendar", + "User-Agent": "Mozilla/5.0", + } + + # Make the GET request + response = requests.get(URI, headers=headers, params=payload) + + # Parse the HTML with BeautifulSoup + soup = BeautifulSoup(response.text, "html.parser") + + # Find the select dropdown + select = soup.find("select", id="SelectStreet") + + # Find the option that contains "Flat 1" + address = select.find("option", string=lambda text: text and user_paon in text) + + URI = "http://collectiondates.eastlothian.gov.uk/ajax/your-calendar/load-recycling-summer-2024.asp" + + payload = { + "id": address["value"], + } + + # Make the GET request + response = requests.get(URI, headers=headers, params=payload) + + # Parse the HTML with BeautifulSoup + soup = BeautifulSoup(response.text, "html.parser") + + # Extract collection details + calendar_items = soup.find_all("div", class_="calendar-item") + for item in calendar_items: + waste_label = item.find("div", class_="waste-label").text.strip() + waste_value = item.find("div", class_="waste-value").find("h4").text.strip() + + try: + collection_date = datetime.strptime( + remove_ordinal_indicator_from_date_string(waste_value), + "%A %d %B %Y", + ) + except ValueError: + continue + + dict_data = { + "type": waste_label.replace(" is:", ""), + "collectionDate": collection_date.strftime(date_format), + } + bindata["bins"].append(dict_data) + + bindata["bins"].sort( + key=lambda x: datetime.strptime(x.get("collectionDate"), date_format) + ) + + return bindata diff --git a/wiki/Councils.md b/wiki/Councils.md index e873360ec4..33967b0712 100644 --- a/wiki/Councils.md +++ b/wiki/Councils.md @@ -93,6 +93,7 @@ This document is still a work in progress, don't worry if your council isn't lis - [East Devon District Council](#east-devon-district-council) - [East Herts Council](#east-herts-council) - [East Lindsey District Council](#east-lindsey-district-council) +- [East Lothian Council](#east-lothian-council) - [East Renfrewshire Council](#east-renfrewshire-council) - [East Riding Council](#east-riding-council) - [East Staffordshire Borough Council](#east-staffordshire-borough-council) @@ -1308,6 +1309,19 @@ Note: Pass the house name/number and postcode in their respective parameters. Th --- +### East Lothian Council +```commandline +python collect_data.py EastLothianCouncil https://eastlothian.gov.uk -s -p "XXXX XXX" -n XX +``` +Additional parameters: +- `-s` - skip get URL +- `-p` - postcode +- `-n` - house number + +Note: Pass the house number and postcode in their respective parameters + +--- + ### East Renfrewshire Council ```commandline python collect_data.py EastRenfrewshireCouncil https://eastrenfrewshire.gov.uk/ -s -p "XXXX XXX" -n XX -w http://HOST:PORT/