Skip to content

Commit 7eaaa0b

Browse files
authored
Merge pull request #1453 from davida72/Council-Fixes-(05-21)
Council fixes (05 21)
2 parents 6492b51 + 8329b2e commit 7eaaa0b

File tree

5 files changed

+332
-96
lines changed

5 files changed

+332
-96
lines changed

uk_bin_collection/tests/input.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,9 +1693,10 @@
16931693
"LAD24CD": "E06000012"
16941694
},
16951695
"NorthHertfordshireDistrictCouncil": {
1696-
"house_number": "2",
1696+
"house_number": "22",
16971697
"postcode": "SG6 4BJ",
16981698
"url": "https://www.north-herts.gov.uk",
1699+
"web_driver": "http://selenium:4444",
16991700
"wiki_name": "North Hertfordshire",
17001701
"wiki_note": "Pass the house number and postcode in their respective parameters.",
17011702
"LAD24CD": "E07000099"
@@ -2180,6 +2181,7 @@
21802181
},
21812182
"SouthRibbleCouncil": {
21822183
"uprn": "010013246384",
2184+
"postcode": "PR5 6DT",
21832185
"url": "https://www.southribble.gov.uk",
21842186
"wiki_command_url_override": "https://www.southribble.gov.uk",
21852187
"wiki_name": "South Ribble",

uk_bin_collection/uk_bin_collection/councils/AdurAndWorthingCouncils.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,55 @@ def parse_data(self, page: str, **kwargs) -> dict:
3737
paragraphs = bin_row.find_all("p")
3838

3939
for p in paragraphs:
40-
if p.get_text() and "Next collection:" in p.get_text():
41-
date_str = p.get_text().replace("Next collection:", "").strip()
42-
# Extract day number from date string (e.g. "2" from "Friday 2nd May")
43-
day_number = int("".join(filter(str.isdigit, date_str)))
44-
# Replace ordinal in date string with plain number
45-
date_str = date_str.replace(
46-
get_date_with_ordinal(day_number), str(day_number)
40+
# Check for both singular and plural "Next collection(s):"
41+
if p.get_text() and (
42+
"Next collection:" in p.get_text()
43+
or "Next collections:" in p.get_text()
44+
):
45+
# Extract collection dates
46+
date_text = (
47+
p.get_text()
48+
.replace("Next collection:", "")
49+
.replace("Next collections:", "")
50+
.strip()
4751
)
4852

49-
try:
50-
# Parse date with full format
51-
bin_date = datetime.strptime(date_str, "%A %d %B")
52-
53-
# Add current year since it's not in the date string
54-
current_year = datetime.now().year
55-
bin_date = bin_date.replace(year=current_year)
56-
57-
# If the date is in the past, it's probably for next year
58-
if bin_date < datetime.now():
59-
bin_date = bin_date.replace(year=current_year + 1)
60-
61-
collections.append((bin_type, bin_date))
62-
print(
63-
f"Successfully parsed date for {bin_type}: {bin_date}"
64-
)
65-
break
66-
67-
except ValueError as e:
68-
print(
69-
f"Failed to parse date '{date_str}' for {bin_type}: {e}"
70-
)
71-
continue
53+
# Split multiple dates if comma-separated
54+
date_strings = [date.strip() for date in date_text.split(",")]
55+
56+
for date_str in date_strings:
57+
try:
58+
# Extract day number from date string (e.g. "2" from "Tuesday 27th May")
59+
day_number = int("".join(filter(str.isdigit, date_str)))
60+
# Replace ordinal in date string with plain number
61+
date_str = date_str.replace(
62+
get_date_with_ordinal(day_number), str(day_number)
63+
)
64+
65+
# Parse date with full format
66+
bin_date = datetime.strptime(date_str, "%A %d %B")
67+
68+
# Add current year since it's not in the date string
69+
current_year = datetime.now().year
70+
bin_date = bin_date.replace(year=current_year)
71+
72+
# If the date is in the past, it's probably for next year
73+
if bin_date < datetime.now():
74+
bin_date = bin_date.replace(year=current_year + 1)
75+
76+
collections.append((bin_type, bin_date))
77+
print(
78+
f"Successfully parsed date for {bin_type}: {bin_date}"
79+
)
80+
81+
except ValueError as e:
82+
print(
83+
f"Failed to parse date '{date_str}' for {bin_type}: {e}"
84+
)
85+
continue
86+
87+
# Found and processed the collection dates, so break the loop
88+
break
7289

7390
except Exception as e:
7491
print(f"Error processing bin row: {e}")

uk_bin_collection/uk_bin_collection/councils/BradfordMDC.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,22 @@ def parse_data(self, page: str, **kwargs) -> dict:
130130
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format)
131131
)
132132

133+
data["bins"].sort(
134+
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format)
135+
)
136+
137+
# Deduplicate the bins based on type and collection date
138+
# Feels a bit hacky, but fixes
139+
# https://github.com/robbrad/UKBinCollectionData/issues/1436
140+
unique_bins = []
141+
seen = set()
142+
for bin_item in data["bins"]:
143+
# Create a unique identifier for each bin entry
144+
bin_key = (bin_item["type"], bin_item["collectionDate"])
145+
if bin_key not in seen:
146+
seen.add(bin_key)
147+
unique_bins.append(bin_item)
148+
149+
data["bins"] = unique_bins
150+
133151
return data

uk_bin_collection/uk_bin_collection/councils/EastleighBoroughCouncil.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ def parse_data(self, page: str, **kwargs) -> dict:
7777
return data
7878

7979
except Exception as e:
80-
print(f"Error fetching/parsing data: {str(e)}")
81-
return {"bins": [{"type": "Error", "collectionDate": "2024-01-01"}]}
80+
import traceback
81+
82+
error_message = f"Error fetching/parsing data for Eastleigh: {str(e)}\n{traceback.format_exc()}"
83+
print(error_message)
84+
# Use the correct date format for the error fallback
85+
today = datetime.now().strftime("%d/%m/%Y")
86+
return {"bins": [{"type": "Error", "collectionDate": today}]}
8287
finally:
8388
if "driver" in locals():
8489
driver.quit()

0 commit comments

Comments
 (0)