1
1
import requests
2
2
import json
3
3
4
- from datetime import datetime
4
+ from datetime import datetime , timedelta
5
5
from waste_collection_schedule import Collection # type: ignore[attr-defined]
6
6
7
7
TITLE = "Republic Services"
12
12
"Scott Country Clerk" : {"street_address" : "101 E Main St, Georgetown, KY 40324" },
13
13
"Branch County Clerk" : {"street_address" : "31 Division St. Coldwater, MI 49036" },
14
14
"Contract Collection" : {"street_address" : "8957 Park Meadows Dr, Elk Grove, CA 95624" },
15
+ "Residential Collection" : {"street_address" : "117 Roxie Ln, Georgetown, KY 40324" },
16
+
17
+ }
18
+ DELAYS = {
19
+ " one " : 1 ,
20
+ " two " : 2 ,
21
+ " three " : 3 ,
22
+ " four " : 4 ,
23
+ " five " : 5 ,
24
+ " six " : 6 ,
25
+ " seven " : 7 ,
15
26
}
16
27
17
28
@@ -20,36 +31,102 @@ def __init__(self, street_address):
20
31
self ._street_address = street_address
21
32
22
33
def fetch (self ):
23
- response1 = requests .get (
34
+ s = requests .Session ()
35
+
36
+ # Get address data
37
+ r0 = requests .get (
24
38
"https://www.republicservices.com/api/v1/addresses" ,
25
39
params = {"addressLine1" : self ._street_address },
26
40
)
41
+ r0_json = json .loads (r0 .text )["data" ][0 ]
42
+ address_hash = r0_json ["addressHash" ]
43
+ longitude = r0_json ["longitude" ]
44
+ latitude = r0_json ["latitude" ]
27
45
28
- address_hash = json .loads (response1 .text )["data" ][0 ]["addressHash" ]
29
-
30
- response2 = requests .get (
46
+ # Get raw schedule
47
+ r1 = requests .get (
31
48
"https://www.republicservices.com/api/v1/publicPickup" ,
32
49
params = {"siteAddressHash" : address_hash },
33
50
)
51
+ r1_json = json .loads (r1 .text )["data" ]
52
+ service = ""
53
+ schedule = {}
54
+ for service_type in r1_json :
55
+ if hasattr (service_type , "__iter__" ) and service_type != "isColaAccount" :
56
+ i = 0
57
+ for item in r1_json [service_type ]:
58
+ for day in item ["nextServiceDays" ]:
59
+ dt = datetime .strptime (day , "%Y-%m-%d" ).date ()
60
+ service = item ["containerCategory" ]
61
+ schedule .update (
62
+ {i : {
63
+ "date" : dt ,
64
+ "waste_type" : item ["wasteTypeDescription" ],
65
+ "waste_description" : item ["productDescription" ],
66
+ "service" : service ,
67
+ }
68
+ }
69
+ )
70
+ i += 1
34
71
35
- r_json = json .loads (response2 .text )["data" ]
36
-
37
- entries = []
72
+ # Compile holidays that impact collections
73
+ r2 = s .get (f"https://www.republicservices.com/api/v3/holidaySchedules/schedules" , params = {"latitude" : latitude , "longitude" : longitude })
74
+ r2_json = json .loads (r2 .text )["data" ]
75
+ day_offset = 0
76
+ i = 0
77
+ holidays = {}
78
+ for item in r2_json :
79
+ if item ["serviceImpacted" ] == True and item ["LOB" ] == service :
80
+ for delay in DELAYS :
81
+ if delay in item ["description" ]:
82
+ day_offset = DELAYS [delay ]
83
+ dt = datetime .strptime (item ["date" ], "%Y-%m-%dT00:00:00.0000000Z" ).date ()
84
+ holidays .update (
85
+ {i : {
86
+ "date" : dt ,
87
+ "name" : item ["name" ],
88
+ "description" : item ["description" ],
89
+ "delay" : day_offset ,
90
+ "incorporated" : False
91
+ }
92
+ }
93
+ )
94
+ i += 1
38
95
39
- for x in r_json :
40
- if hasattr (r_json [x ], "__iter__" ):
41
- for item in r_json [x ]:
42
- waste_type = item ["wasteTypeDescription" ]
43
- container_type = item ["containerType" ]
44
- icon = "mdi:trash-can"
45
- if waste_type == "Recycle" :
46
- icon = "mdi:recycle"
47
- if container_type == "YC" :
48
- waste_type = "Yard Waste"
49
- icon = "mdi:leaf"
50
- for day in item ["nextServiceDays" ]:
51
- next_pickup = day
52
- next_pickup_date = datetime .fromisoformat (next_pickup ).date ()
53
- entries .append (Collection (date = next_pickup_date , t = waste_type , icon = icon ))
96
+ # Cycle through schedule and holidays incorporating delays
97
+ while True :
98
+ changes = 0
99
+ for holiday in holidays :
100
+ if not holidays [holiday ]["incorporated" ]:
101
+ h = holidays [holiday ]["date" ]
102
+ d = holidays [holiday ]["delay" ]
103
+ for pickup in schedule :
104
+ p = schedule [pickup ]["date" ]
105
+ date_difference = (p - h ).days
106
+ if date_difference <= 5 and date_difference >= 0 :
107
+ revised_date = p + timedelta (days = d )
108
+ schedule [pickup ]["date" ] = revised_date
109
+ holidays [holiday ]["incorporated" ] = True
110
+ # print(p, h, d, date_difference, revised_date)
111
+ changes += 1
112
+ if changes == 0 :
113
+ break
54
114
115
+ # Build final schedule (implements original logic for assigning icon)
116
+ entries = []
117
+ for item in schedule :
118
+ if "RECYCLE" in schedule [item ]["waste_description" ]:
119
+ icon = "mdi:recycle"
120
+ elif "YARD" in schedule [item ]["waste_description" ]:
121
+ icon = "mdi:leaf"
122
+ else :
123
+ icon = "mdi:trash-can"
124
+ entries .append (
125
+ Collection (
126
+ date = schedule [item ]["date" ],
127
+ t = schedule [item ]["waste_type" ],
128
+ icon = icon ,
129
+ ),
130
+ )
131
+
55
132
return entries
0 commit comments