1
+ import json
2
+
1
3
from bs4 import BeautifulSoup
4
+
2
5
from uk_bin_collection .uk_bin_collection .common import *
3
6
from uk_bin_collection .uk_bin_collection .get_bin_data import AbstractGetBinDataClass
4
7
@@ -16,46 +19,41 @@ def parse_data(self, page: str, **kwargs) -> dict:
16
19
uprn = kwargs .get ("uprn" )
17
20
check_uprn (uprn )
18
21
22
+ label_map = {
23
+ "domestic-waste-collection-service" : "Household Waste" ,
24
+ "recycling-collection-service" : "Recycling" ,
25
+ "garden-waste-collection-service" : "Garden Waste" ,
26
+ }
27
+
19
28
requests .packages .urllib3 .disable_warnings ()
20
29
response = requests .get (
21
- f"https://map .erewash.gov.uk/isharelive.web/myerewash.aspx?action=SetAddress&UniqueId ={ uprn } " ,
30
+ f"https://www .erewash.gov.uk/bbd-whitespace/one-year-collection-dates-without-christmas?uprn ={ uprn } " ,
22
31
headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; Win64; x64)" },
23
32
)
33
+ # Parse the JSON response
34
+ payload = response .json ()
35
+ bin_collection = json .loads (payload ) if isinstance (payload , str ) else payload
24
36
25
- soup = BeautifulSoup (response .text , features = "html.parser" )
26
- collections = soup .find ("div" , {"aria-label" : "Waste Collection" }).find_all (
27
- "div" , {"class" : "atPanelContent" }
37
+ cd = next (
38
+ i ["settings" ]["collection_dates" ]
39
+ for i in bin_collection
40
+ if i .get ("command" ) == "settings"
28
41
)
29
- for c in collections :
30
- bin_type = c .find ("h4" ).get_text (strip = True )
31
- if "my next" in bin_type .lower ():
32
- collection_info = c .find ("div" , {"class" : "atPanelData" }).get_text (
33
- strip = True
34
- )
35
- results = re .search (
36
- "([A-Za-z]+ \\ d+[A-Za-z]+ [A-Za-z]+ \\ d*)" , collection_info
42
+
43
+ for month in cd .values ():
44
+ for e in month :
45
+ d = e ["date" ] # "YYYY-MM-DD"
46
+ label = label_map .get (
47
+ e .get ("service-identifier" ),
48
+ e .get ("service" ) or e .get ("service-identifier" ),
37
49
)
38
- if results :
39
- collection_date = datetime .strptime (
40
- remove_ordinal_indicator_from_date_string (results [1 ]).strip (),
41
- "%A %d %B %Y" ,
42
- ).strftime (date_format )
43
- dict_data = {
44
- "type" : bin_type .replace ("My Next " , "" ).replace (
45
- " Collection" , ""
46
- ),
47
- "collectionDate" : collection_date ,
48
- }
49
- data ["bins" ].append (dict_data )
50
- if "garden waste" in collection_info .lower ():
51
- dict_data = {
52
- "type" : "Garden Waste" ,
53
- "collectionDate" : collection_date ,
54
- }
55
- data ["bins" ].append (dict_data )
56
-
57
- data ["bins" ].sort (
58
- key = lambda x : datetime .strptime (x .get ("collectionDate" ), date_format )
59
- )
50
+
51
+ dict_data = {
52
+ "type" : label ,
53
+ "collectionDate" : datetime .strptime (d , "%Y-%m-%d" ).strftime (
54
+ date_format
55
+ ),
56
+ }
57
+ data ["bins" ].append (dict_data )
60
58
61
59
return data
0 commit comments