1
- import json
1
+ import time
2
+
2
3
import requests
3
- from datetime import datetime
4
+ from dateutil . relativedelta import relativedelta
4
5
5
6
from uk_bin_collection .uk_bin_collection .common import *
6
7
from uk_bin_collection .uk_bin_collection .get_bin_data import AbstractGetBinDataClass
7
8
8
9
10
+ # import the wonderful Beautiful Soup and the URL grabber
9
11
class CouncilClass (AbstractGetBinDataClass ):
10
12
"""
11
13
Concrete classes have to implement all abstract operations of the
@@ -14,28 +16,84 @@ class CouncilClass(AbstractGetBinDataClass):
14
16
"""
15
17
16
18
def parse_data (self , page : str , ** kwargs ) -> dict :
17
- user_uprn = kwargs .get ("uprn" )
18
- check_uprn (user_uprn )
19
+ # Make a BS4 object
20
+ uprn = kwargs .get ("uprn" )
21
+ # usrn = kwargs.get("paon")
22
+ check_uprn (uprn )
23
+ # check_usrn(usrn)
19
24
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" ),
25
+
26
+ # uprn = uprn.zfill(12)
27
+
28
+ SESSION_URL = "https://eastherts-self.achieveservice.com/authapi/isauthenticated?uri=https%253A%252F%252Feastherts-self.achieveservice.com%252FAchieveForms%252F%253Fmode%253Dfill%2526consentMessage%253Dyes%2526form_uri%253Dsandbox-publish%253A%252F%252FAF-Process-98782935-6101-4962-9a55-5923e76057b6%252FAF-Stage-dcd0ec18-dfb4-496a-a266-bd8fadaa28a7%252Fdefinition.json%2526process%253D1%2526process_uri%253Dsandbox-processes%253A%252F%252FAF-Process-98782935-6101-4962-9a55-5923e76057b6%2526process_id%253DAF-Process-98782935-6101-4962-9a55-5923e76057b6&hostname=eastherts-self.achieveservice.com&withCredentials=true"
29
+
30
+ API_URL = "https://eastherts-self.achieveservice.com/apibroker/runLookup"
31
+
32
+ headers = {
33
+ "Content-Type" : "application/json" ,
34
+ "Accept" : "*/*" ,
35
+ "User-Agent" : "Mozilla/5.0" ,
36
+ "X-Requested-With" : "XMLHttpRequest" ,
37
+ "Referer" : "https://eastherts-self.achieveservice.com/fillform/?iframe_id=fillform-frame-1&db_id=" ,
38
+ }
39
+ s = requests .session ()
40
+ r = s .get (SESSION_URL )
41
+ r .raise_for_status ()
42
+ session_data = r .json ()
43
+ sid = session_data ["auth-session" ]
44
+ params = {
45
+ # unix_timestamp
46
+ "_" : str (int (time .time () * 1000 )),
47
+ "sid" : sid ,
48
+ }
49
+
50
+ params = {
51
+ "id" : "683d9ff0e299d" ,
52
+ "repeat_against" : "" ,
53
+ "noRetry" : "true" ,
54
+ "getOnlyTokens" : "undefined" ,
55
+ "log_id" : "" ,
56
+ "app_name" : "AF-Renderer::Self" ,
57
+ # unix_timestamp
58
+ "_" : str (int (time .time () * 1000 )),
59
+ "sid" : sid ,
60
+ }
61
+
62
+ data = {
63
+ "formValues" : {
64
+ "Collection Days" : {
65
+ "inputUPRN" : {
66
+ "value" : uprn ,
38
67
}
39
- bindata ["bins" ].append (dict_data )
40
-
68
+ },
69
+ }
70
+ }
71
+
72
+ r = s .post (API_URL , json = data , headers = headers , params = params )
73
+ r .raise_for_status ()
74
+
75
+ data = r .json ()
76
+ rows_data = data ["integration" ]["transformed" ]["rows_data" ]["0" ]
77
+ if not isinstance (rows_data , dict ):
78
+ raise ValueError ("Invalid data returned from API" )
79
+
80
+ # Extract each service's relevant details for the bin schedule
81
+ for key , value in rows_data .items ():
82
+ if key .endswith ("NextDate" ):
83
+ BinType = key .replace ("NextDate" , "ServiceName" )
84
+ for key2 , value2 in rows_data .items ():
85
+ if key2 == BinType :
86
+ BinType = value2
87
+ next_collection = datetime .strptime (
88
+ remove_ordinal_indicator_from_date_string (value ), "%A %d %B"
89
+ ).replace (year = datetime .now ().year )
90
+ if datetime .now ().month == 12 and next_collection .month == 1 :
91
+ next_collection = next_collection + relativedelta (years = 1 )
92
+
93
+ dict_data = {
94
+ "type" : BinType ,
95
+ "collectionDate" : next_collection .strftime (date_format ),
96
+ }
97
+ bindata ["bins" ].append (dict_data )
98
+
41
99
return bindata
0 commit comments