@@ -26,7 +26,9 @@ def parse_data(self, page: str, **kwargs) -> dict:
26
26
uprn = kwargs .get ("uprn" )
27
27
check_uprn (uprn )
28
28
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"
30
32
post_header_str = (
31
33
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,"
32
34
"image/apng,"
@@ -51,31 +53,60 @@ def parse_data(self, page: str, **kwargs) -> dict:
51
53
soup = BeautifulSoup (post_response .text , features = "html.parser" )
52
54
soup .prettify ()
53
55
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
+
54
63
data = {"bins" : []}
55
- collection_tuple = []
56
64
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 ))
79
110
80
111
ordered_data = sorted (collection_tuple , key = lambda x : x [1 ])
81
112
0 commit comments