1
- from bs4 import BeautifulSoup
2
- from selenium .webdriver .common .by import By
3
- from selenium .webdriver .support import expected_conditions as EC
4
- from selenium .webdriver .support .wait import WebDriverWait
1
+ import json
2
+ import requests
3
+ from datetime import datetime
5
4
6
5
from uk_bin_collection .uk_bin_collection .common import *
7
6
from uk_bin_collection .uk_bin_collection .get_bin_data import AbstractGetBinDataClass
@@ -15,116 +14,28 @@ class CouncilClass(AbstractGetBinDataClass):
15
14
"""
16
15
17
16
def parse_data (self , page : str , ** kwargs ) -> dict :
18
- # Get and check UPRN
19
- driver = None
20
- try :
21
- user_postcode = kwargs .get ("postcode" )
22
- user_paon = kwargs .get ("paon" )
23
- check_paon (user_paon )
24
- check_postcode (user_postcode )
25
- web_driver = kwargs .get ("web_driver" )
26
- headless = kwargs .get ("headless" )
27
- bindata = {"bins" : []}
28
-
29
- API_URL = "https://uhte-wrp.whitespacews.com"
30
-
31
- # Create Selenium webdriver
32
- driver = create_webdriver (web_driver , headless , None , __name__ )
33
- driver .get (API_URL )
34
-
35
- # Click Find my bin collection day button
36
- collectionButton = WebDriverWait (driver , 10 ).until (
37
- EC .element_to_be_clickable ((By .LINK_TEXT , "Find my bin collection day" ))
38
- )
39
- collectionButton .click ()
40
-
41
- main_content = WebDriverWait (driver , 10 ).until (
42
- EC .presence_of_element_located ((By .ID , "main-content" ))
43
- )
44
-
45
- # Wait for the property number field to appear then populate it
46
- inputElement_number = WebDriverWait (driver , 10 ).until (
47
- EC .element_to_be_clickable (
48
- (
49
- By .ID ,
50
- "address_name_number" ,
51
- )
52
- )
53
- )
54
- inputElement_number .send_keys (user_paon )
55
-
56
- # Wait for the postcode field to appear then populate it
57
- inputElement_postcode = WebDriverWait (driver , 10 ).until (
58
- EC .element_to_be_clickable (
59
- (
60
- By .ID ,
61
- "address_postcode" ,
62
- )
63
- )
64
- )
65
- inputElement_postcode .send_keys (user_postcode )
66
-
67
- # Click search button
68
- continueButton = WebDriverWait (driver , 10 ).until (
69
- EC .element_to_be_clickable (
70
- (
71
- By .ID ,
72
- "Submit" ,
73
- )
74
- )
75
- )
76
- continueButton .click ()
77
-
78
- # Wait for the 'Search Results' to appear and select the first result
79
- property = WebDriverWait (driver , 10 ).until (
80
- EC .element_to_be_clickable (
81
- (
82
- By .CSS_SELECTOR ,
83
- "li.app-subnav__section-item a" ,
84
- # "app-subnav__link govuk-link clicker colordarkblue fontfamilyArial fontsize12rem",
85
- # "//a[starts-with(@aria-label, '{user_paon}')]",
86
- )
87
- )
88
- )
89
- property .click ()
90
-
91
- upcoming_scheduled_collections = WebDriverWait (driver , 10 ).until (
92
- EC .presence_of_element_located (
93
- (By .ID , "upcoming-scheduled-collections" )
94
- )
95
- )
96
-
97
- soup = BeautifulSoup (driver .page_source , features = "html.parser" )
98
-
99
- collections = []
100
- for collection in soup .find_all (
101
- "u1" ,
102
- class_ = "displayinlineblock justifycontentleft alignitemscenter margin0 padding0" ,
103
- ):
104
- date = collection .find (
105
- "p" , string = lambda text : text and "/" in text
106
- ).text .strip () # Extract date
107
- service = collection .find (
108
- "p" , string = lambda text : text and "Collection Service" in text
109
- ).text .strip () # Extract service type
110
- collections .append ({"date" : date , "service" : service })
111
-
112
- # Print the parsed data
113
- for item in collections :
114
-
115
- dict_data = {
116
- "type" : item ["service" ],
117
- "collectionDate" : item ["date" ],
118
- }
119
- bindata ["bins" ].append (dict_data )
120
-
121
- except Exception as e :
122
- # Here you can log the exception if needed
123
- print (f"An error occurred: { e } " )
124
- # Optionally, re-raise the exception if you want it to propagate
125
- raise
126
- finally :
127
- # This block ensures that the driver is closed regardless of an exception
128
- if driver :
129
- driver .quit ()
17
+ user_uprn = kwargs .get ("uprn" )
18
+ check_uprn (user_uprn )
19
+ bindata = {"bins" : []}
20
+
21
+ # Make API request
22
+ api_url = f"https://east-herts.co.uk/api/services/{ user_uprn } "
23
+ response = requests .get (api_url )
24
+ response .raise_for_status ()
25
+
26
+ data = response .json ()
27
+ today = datetime .now ().date ()
28
+
29
+ for service in data .get ("services" , []):
30
+ collection_date_str = service .get ("collectionDate" )
31
+ if collection_date_str :
32
+ collection_date = datetime .strptime (collection_date_str , "%Y-%m-%d" ).date ()
33
+ # Only include future dates
34
+ if collection_date >= today :
35
+ dict_data = {
36
+ "type" : service .get ("binType" , "" ),
37
+ "collectionDate" : collection_date .strftime ("%d/%m/%Y" ),
38
+ }
39
+ bindata ["bins" ].append (dict_data )
40
+
130
41
return bindata
0 commit comments