-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1132 from m26dvd/master
- Loading branch information
Showing
14 changed files
with
735 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
uk_bin_collection/uk_bin_collection/councils/BrentCouncil.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
uk_bin_collection/uk_bin_collection/councils/CumberlandCouncil.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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_uprn = kwargs.get("uprn") | ||
check_uprn(user_uprn) | ||
bindata = {"bins": []} | ||
|
||
URI = "https://waste.cumberland.gov.uk/renderform?t=25&k=E43CEB1FB59F859833EF2D52B16F3F4EBE1CAB6A" | ||
|
||
s = requests.Session() | ||
|
||
# Make the GET request | ||
response = s.get(URI) | ||
|
||
# Make a BS4 object | ||
soup = BeautifulSoup(response.content, features="html.parser") | ||
|
||
# print(soup) | ||
|
||
token = (soup.find("input", {"name": "__RequestVerificationToken"})).get( | ||
"value" | ||
) | ||
|
||
formguid = (soup.find("input", {"name": "FormGuid"})).get("value") | ||
|
||
# print(token) | ||
# print(formguid) | ||
|
||
headers = { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Origin": "https://waste.cumberland.gov.uk", | ||
"Referer": "https://waste.cumberland.gov.uk/renderform?t=25&k=E43CEB1FB59F859833EF2D52B16F3F4EBE1CAB6A", | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 OPR/98.0.0.0", | ||
"X-Requested-With": "XMLHttpRequest", | ||
} | ||
|
||
payload = { | ||
"__RequestVerificationToken": token, | ||
"FormGuid": formguid, | ||
"ObjectTemplateID": "25", | ||
"Trigger": "submit", | ||
"CurrentSectionID": "33", | ||
"TriggerCtl": "", | ||
"FF265": f"U{user_uprn}", | ||
"FF265lbltxt": "Please select your address", | ||
} | ||
|
||
# print(payload) | ||
|
||
response = s.post( | ||
"https://waste.cumberland.gov.uk/renderform/Form", | ||
headers=headers, | ||
data=payload, | ||
) | ||
|
||
soup = BeautifulSoup(response.content, features="html.parser") | ||
for row in soup.find_all("div", class_="resirow"): | ||
# Extract the type of collection (e.g., Recycling, Refuse) | ||
collection_type_div = row.find("div", class_="col") | ||
collection_type = ( | ||
collection_type_div.get("class")[1] | ||
if collection_type_div | ||
else "Unknown" | ||
) | ||
|
||
# Extract the collection date | ||
date_div = row.find("div", style="width:360px;") | ||
collection_date = date_div.text.strip() if date_div else "Unknown" | ||
|
||
dict_data = { | ||
"type": collection_type, | ||
"collectionDate": datetime.strptime( | ||
collection_date, "%A %d %B %Y" | ||
).strftime(date_format), | ||
} | ||
bindata["bins"].append(dict_data) | ||
|
||
bindata["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), "%d/%m/%Y") | ||
) | ||
|
||
return bindata |
Oops, something went wrong.