Skip to content

Commit

Permalink
Merge pull request #1010 from m26dvd/master
Browse files Browse the repository at this point in the history
feat: Council Pack 16
  • Loading branch information
robbrad authored Nov 15, 2024
2 parents 5ddcff1 + db3c6be commit f44748c
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 46 deletions.
43 changes: 41 additions & 2 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@
"BaberghDistrictCouncil": {
"skip_get_url": true,
"house_number": "Monday",
"postcode": "Week 1",
"url": "https://www.babergh.gov.uk",
"wiki_name": "Babergh District Council",
"wiki_note": "Use the House Number field to pass the DAY of the week for your collections. Monday/Tuesday/Wednesday/Thursday/Friday"
"wiki_note": "Use the House Number field to pass the DAY of the week for your collections. Monday/Tuesday/Wednesday/Thursday/Friday. [OPTIONAL] Use the 'postcode' field to pass the WEEK for your garden collection. [Week 1/Week 2]"
},
"BCPCouncil": {
"skip_get_url": true,
Expand Down Expand Up @@ -397,6 +398,12 @@
"wiki_name": "Conwy County Borough Council",
"wiki_note": "Conwy County Borough Council uses a straight UPRN in the URL, e.g., `&uprn=XXXXXXXXXXXXX`."
},
"CopelandBoroughCouncil": {
"uprn": "100110734613",
"url": "https://www.copeland.gov.uk",
"wiki_name": "Copeland Borough Council",
"wiki_note": "Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"CornwallCouncil": {
"skip_get_url": true,
"uprn": "100040128734",
Expand Down Expand Up @@ -1005,9 +1012,10 @@
"MidSuffolkDistrictCouncil": {
"skip_get_url": true,
"house_number": "Monday",
"postcode": "Week 2",
"url": "https://www.midsuffolk.gov.uk",
"wiki_name": "Mid Suffolk District Council",
"wiki_note": "Use the House Number field to pass the DAY of the week for your collections. Monday/Tuesday/Wednesday/Thursday/Friday"
"wiki_note": "Use the House Number field to pass the DAY of the week for your collections. Monday/Tuesday/Wednesday/Thursday/Friday. [OPTIONAL] Use the 'postcode' field to pass the WEEK for your garden collection. [Week 1/Week 2]"
},
"MidSussexDistrictCouncil": {
"house_number": "OAKLANDS, OAKLANDS ROAD RH16 1SS",
Expand Down Expand Up @@ -1303,6 +1311,12 @@
"wiki_name": "Rochford Council",
"wiki_note": "No extra parameters are required. Dates presented should be read as 'week commencing'."
},
"RotherDistrictCouncil": {
"uprn": "100061937338",
"url": "https://www.rother.gov.uk",
"wiki_name": "Rother District Council",
"wiki_note": "Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"RotherhamCouncil": {
"url": "https://www.rotherham.gov.uk/bin-collections?address=100050866000&submit=Submit",
"uprn": "100050866000",
Expand Down Expand Up @@ -1412,6 +1426,12 @@
"wiki_name": "South Gloucestershire Council",
"wiki_note": "Provide your UPRN. You can find it using [FindMyAddress](https://www.findmyaddress.co.uk/search)."
},
"SouthHamsDistrictCouncil": {
"uprn": "10004742851",
"url": "https://www.southhams.gov.uk",
"wiki_name": "South Hams District Council",
"wiki_note": "Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"SouthKestevenDistrictCouncil": {
"house_number": "2 Althorpe Close, Market Deeping, PE6 8BL",
"postcode": "PE68BL",
Expand Down Expand Up @@ -1476,6 +1496,12 @@
"wiki_name": "St Albans City and District Council",
"wiki_note": "Provide your UPRN. You can find it using [FindMyAddress](https://www.findmyaddress.co.uk/search)."
},
"StevenageBoroughCouncil": {
"uprn": "100080878852",
"url": "https://www.stevenage.gov.uk",
"wiki_name": "Stevenage Borough Council",
"wiki_note": "Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"StHelensBC": {
"house_number": "15",
"postcode": "L34 2GA",
Expand Down Expand Up @@ -1630,6 +1656,12 @@
"wiki_name": "Test Valley Borough Council",
"wiki_note": "Provide your UPRN and postcode. Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"ThanetDistrictCouncil": {
"uprn": "100061111858",
"url": "https://www.thanet.gov.uk",
"wiki_name": "Thanet District Council",
"wiki_note": "Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"ThreeRiversDistrictCouncil": {
"postcode": "WD3 7AZ",
"skip_get_url": true,
Expand Down Expand Up @@ -1897,6 +1929,13 @@
"wiki_name": "Worcester City Council",
"wiki_note": "Provide your UPRN. You can find it using [FindMyAddress](https://www.findmyaddress.co.uk/search)."
},
"WolverhamptonCityCouncil": {
"uprn": "100071205205",
"postcode": "WV3 9NZ",
"url": "https://www.wolverhampton.gov.uk",
"wiki_name": "Wolverhampton City Council",
"wiki_note": "Use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"WorcesterCityCouncil": {
"url": "https://www.Worcester.gov.uk",
"wiki_command_url_override": "https://www.Worcester.gov.uk",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CouncilClass(AbstractGetBinDataClass):
def parse_data(self, page: str, **kwargs) -> dict:

collection_day = kwargs.get("paon")
garden_collection_week = kwargs.get("postcode")
bindata = {"bins": []}

days_of_week = [
Expand All @@ -35,10 +36,14 @@ def parse_data(self, page: str, **kwargs) -> dict:
"Sunday",
]

garden_week = ["Week 1", "Week 2"]

refusestartDate = datetime(2024, 11, 4)
recyclingstartDate = datetime(2024, 11, 11)

offset_days = days_of_week.index(collection_day)
if garden_collection_week:
garden_collection = garden_week.index(garden_collection_week)

refuse_dates = get_dates_every_x_days(refusestartDate, 14, 28)
recycling_dates = get_dates_every_x_days(recyclingstartDate, 14, 28)
Expand Down Expand Up @@ -125,6 +130,63 @@ def parse_data(self, page: str, **kwargs) -> dict:
}
bindata["bins"].append(dict_data)

if garden_collection_week:
if garden_collection == 0:
gardenstartDate = datetime(2024, 11, 11)
elif garden_collection == 1:
gardenstartDate = datetime(2024, 11, 4)

garden_dates = get_dates_every_x_days(gardenstartDate, 14, 28)

garden_bank_holidays = [
("23/12/2024", 1),
("24/12/2024", 1),
("25/12/2024", 1),
("26/12/2024", 1),
("27/12/2024", 1),
("30/12/2024", 1),
("31/12/2024", 1),
("01/01/2025", 1),
("02/01/2025", 1),
("03/01/2025", 1),
]

for gardenDate in garden_dates:

collection_date = (
datetime.strptime(gardenDate, "%d/%m/%Y")
+ timedelta(days=offset_days)
).strftime("%d/%m/%Y")

garden_holiday = next(
(
value
for date, value in garden_bank_holidays
if date == collection_date
),
0,
)

if garden_holiday > 0:
continue

holiday_offset = next(
(value for date, value in bank_holidays if date == collection_date),
0,
)

if holiday_offset > 0:
collection_date = (
datetime.strptime(collection_date, "%d/%m/%Y")
+ timedelta(days=holiday_offset)
).strftime("%d/%m/%Y")

dict_data = {
"type": "Garden Bin",
"collectionDate": collection_date,
}
bindata["bins"].append(dict_data)

bindata["bins"].sort(
key=lambda x: datetime.strptime(x.get("collectionDate"), "%d/%m/%Y")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from xml.etree import ElementTree

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


class CouncilClass(AbstractGetBinDataClass):
"""
Concrete classes have to implement all abstract operations of the
baseclass. They can also override some
operations with a default implementation.
"""

def parse_data(self, page: str, **kwargs) -> dict:
uprn = kwargs.get("uprn")
check_uprn(uprn)
council = "CPL"

# Make SOAP request
headers = {
"Content-Type": "text/xml; charset=UTF-8",
"Referer": "https://collections-copeland.azurewebsites.net/calendar.html",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
}
requests.packages.urllib3.disable_warnings()
post_data = (
'<?xml version="1.0" encoding="utf-8"?>'
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
'<soap:Body><getRoundCalendarForUPRN xmlns="http://webaspx-collections.azurewebsites.net/">'
"<council>" + council + "</council><UPRN>" + uprn + "</UPRN>"
"<from>Chtml</from></getRoundCalendarForUPRN></soap:Body></soap:Envelope>"
)
response = requests.post(
"https://collections-copeland.azurewebsites.net/WSCollExternal.asmx",
headers=headers,
data=post_data,
)

if response.status_code != 200:
raise ValueError("No bin data found for provided UPRN.")

# Get HTML from SOAP response
xmltree = ElementTree.fromstring(response.text)
html = xmltree.find(
".//{http://webaspx-collections.azurewebsites.net/}getRoundCalendarForUPRNResult"
).text
# Parse with BS4
soup = BeautifulSoup(html, features="html.parser")
soup.prettify()

data = {"bins": []}
for bin_type in ["Refuse", "Recycling", "Garden"]:
bin_el = soup.find("b", string=bin_type)
if bin_el:
bin_info = bin_el.next_sibling.split(": ")[1]
collection_date = ""
results = re.search("([A-Za-z]+ \\d\\d? [A-Za-z]+) then", bin_info)
if results:
if results[1] == "Today":
date = datetime.now()
elif results[1] == "Tomorrow":
date = datetime.now() + timedelta(days=1)
else:
date = get_next_occurrence_from_day_month(
datetime.strptime(
results[1] + " " + datetime.now().strftime("%Y"),
"%a %d %b %Y",
)
)
if date:
collection_date = date.strftime(date_format)
else:
results2 = re.search("([A-Za-z]+) then", bin_info)
if results2:
if results2[1] == "Today":
collection_date = datetime.now().strftime(date_format)
elif results2[1] == "Tomorrow":
collection_date = (
datetime.now() + timedelta(days=1)
).strftime(date_format)
else:
collection_date = results2[1]

if collection_date != "":
dict_data = {
"type": bin_type,
"collectionDate": collection_date,
}
data["bins"].append(dict_data)

return data
Loading

0 comments on commit f44748c

Please sign in to comment.