Skip to content

Commit a977d55

Browse files
committed
fix: #1571 - Castle Point District Council
fix: #1571 - Castle Point District Council
1 parent 2246571 commit a977d55

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

uk_bin_collection/uk_bin_collection/councils/CastlepointDistrictCouncil.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def parse_data(self, page: str, **kwargs) -> dict:
2626
uprn = kwargs.get("uprn")
2727
check_uprn(uprn)
2828

29-
post_url = "https://apps.castlepoint.gov.uk/cpapps/index.cfm?fa=myStreet.displayDetails"
29+
base_url = "https://apps.castlepoint.gov.uk/cpapps/"
30+
31+
post_url = f"{base_url}index.cfm?fa=myStreet.displayDetails"
3032
post_header_str = (
3133
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,"
3234
"image/apng,"
@@ -51,31 +53,60 @@ def parse_data(self, page: str, **kwargs) -> dict:
5153
soup = BeautifulSoup(post_response.text, features="html.parser")
5254
soup.prettify()
5355

56+
calMonthNext = f"{base_url}{soup.select_one("div.calMonthNext a")["href"]}"
57+
nextmonth_response = requests.post(
58+
calMonthNext, headers=post_headers, data=form_data, verify=False
59+
)
60+
soup_nextmonth = BeautifulSoup(nextmonth_response.text, features="html.parser")
61+
soup_nextmonth.prettify()
62+
5463
data = {"bins": []}
55-
collection_tuple = []
5664

57-
calendar = soup.find("table", class_="calendar")
58-
month = datetime.strptime(
59-
soup.find("div", class_="calMonthCurrent").get_text(), "[%b]"
60-
).strftime("%m")
61-
year = datetime.strptime(
62-
soup.find("h1").get_text(), "About my Street - %B %Y"
63-
).strftime("%Y")
64-
65-
pink_days = [
66-
day.get_text().strip() for day in calendar.find_all("td", class_="pink")
67-
]
68-
black_days = [
69-
day.get_text().strip() for day in calendar.find_all("td", class_="normal")
70-
]
71-
72-
for day in pink_days:
73-
collection_date = datetime(year=int(year), month=int(month), day=int(day))
74-
collection_tuple.append(("Pink collection", collection_date))
75-
76-
for day in black_days:
77-
collection_date = datetime(year=int(year), month=int(month), day=int(day))
78-
collection_tuple.append(("Normal collection", collection_date))
65+
def parse_calendar_month(soup_one_month):
66+
out = []
67+
68+
calendar = soup_one_month.find("table", class_="calendar")
69+
if not calendar:
70+
return out # be robust
71+
72+
# e.g. "[Aug]"
73+
month_txt = soup_one_month.find("div", class_="calMonthCurrent").get_text(
74+
strip=True
75+
)
76+
month = datetime.strptime(month_txt, "[%b]").strftime("%m")
77+
78+
# e.g. "About my Street - August 2025"
79+
year_txt = soup_one_month.find("h1").get_text(strip=True)
80+
year = datetime.strptime(year_txt, "About my Street - %B %Y").strftime("%Y")
81+
82+
pink_days = [
83+
td.get_text(strip=True) for td in calendar.find_all("td", class_="pink")
84+
]
85+
black_days = [
86+
td.get_text(strip=True)
87+
for td in calendar.find_all("td", class_="normal")
88+
]
89+
90+
for day in pink_days:
91+
out.append(
92+
(
93+
"Pink collection",
94+
datetime(year=int(year), month=int(month), day=int(day)),
95+
)
96+
)
97+
for day in black_days:
98+
out.append(
99+
(
100+
"Normal collection",
101+
datetime(year=int(year), month=int(month), day=int(day)),
102+
)
103+
)
104+
105+
return out
106+
107+
collection_tuple = []
108+
for s in (soup, soup_nextmonth):
109+
collection_tuple.extend(parse_calendar_month(s))
79110

80111
ordered_data = sorted(collection_tuple, key=lambda x: x[1])
81112

0 commit comments

Comments
 (0)