Skip to content

Commit

Permalink
Merge pull request #777 from dp247/775-cornwall-council
Browse files Browse the repository at this point in the history
  • Loading branch information
robbrad authored Jul 31, 2024
2 parents c9743c1 + 306a01c commit 039cd04
Show file tree
Hide file tree
Showing 2 changed files with 77 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 @@ -247,6 +247,13 @@
"wiki_name": "Conwy County Borough Council",
"wiki_note": "Conwy County Borough Council is a straight up uprn in the url eg &uprn=XXXXXXXXXXXXX ."
},
"CornwallCouncil": {
"skip_get_url": true,
"uprn": "100040128734",
"url": "https://www.cornwall.gov.uk/my-area/",
"wiki_name": "Cornwall Council",
"wiki_note": "Use https://uprn.uk/ to find your UPRN."
},
"CrawleyBoroughCouncil": {
"house_number": "9701076",
"skip_get_url": true,
Expand Down
70 changes: 70 additions & 0 deletions uk_bin_collection/uk_bin_collection/councils/CornwallCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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
from dateutil.relativedelta import relativedelta



# 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": []}
collections = []

curr_date = datetime.today()

user_uprn = kwargs.get("uprn")
check_uprn(user_uprn)

headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language': 'en-GB,en;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.143 Safari/537.36',
'sec-ch-ua': '"Opera GX";v="111", "Chromium";v="125", "Not.A/Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
params = {
'uprn': f'{user_uprn}',
# 'uprn': f'100040128734',
}
response = requests.get(
'https://www.cornwall.gov.uk/umbraco/surface/waste/MyCollectionDays',
params=params,
headers=headers
)

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

for item in soup.find_all('div', class_='collection text-center service'):
bin_type = item.contents[1].text + " bin"
collection_date = datetime.strptime(item.contents[5].text, "%d %b").replace(year=curr_date.year)
if curr_date.month == 12 and collection_date.month == 1:
collection_date = collection_date + relativedelta(years=1)
collections.append((bin_type, collection_date))

ordered_data = sorted(collections, key=lambda x: x[1])
data = {"bins": []}
for bin in ordered_data:
dict_data = {
"type": bin[0].capitalize().strip(),
"collectionDate": bin[1].strftime(date_format),
}
data["bins"].append(dict_data)

return data

0 comments on commit 039cd04

Please sign in to comment.