-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.py
245 lines (240 loc) · 12.7 KB
/
status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Import libraries
import requests
import datetime
from dateutil import tz
# keys: ['areas'], ['parkingLots']. sub keys: ['properties']: ['nameEn'], ['isOpen'], ['id'], ['parentFeatureId']
tz = tz.gettz('America/Vancouver')
class WRA:
def __init__(self, name, isOpen, wraID):
self.name = name
self.isOpen = isOpen
self.wraID = wraID
def get_status():
from app import db
from models import Info
date = datetime.datetime.now(tz).strftime('%Y-%m-%d')
data = requests.get(
'https://www.pc.gc.ca/apps/rogers-pass/data/publish-%s' % date).json()
# Not available:
if data == {'error': 'not_found', 'reason': 'missing'}:
output = 'Info for today is not available at this time. Updates usually come at 7AM, and until one has been posted all areas are CLOSED. Try again later by texting \"update\" to this number.'
status_date = datetime.datetime.utcnow().date()
# Append to dB
rpdata = Info(output, status_date)
db.session.add(rpdata)
db.session.commit()
else:
# Date
rawDate = data['validFrom']['PST'].split('T')[0]
rawTime = data['validFrom']['PST'].split('T')[1].split('-')[0][:-3]
statusDate = rawDate + '@' + rawTime
titleStr = 'Status for ' + statusDate + ':'
# Shaughnessy
shaughnessy = WRA(data['areas'][0]['properties']['nameEn'], data['areas']
[0]['properties']['isOpen'], data['areas'][0]['properties']['id'])
# East Rogers
eastRogersNorth = WRA(data['areas'][1]['properties']['nameEn'], data['areas']
[1]['properties']['isOpen'], data['areas'][1]['properties']['id'])
eastRogersSouth = WRA(data['areas'][2]['properties']['nameEn'], data['areas']
[2]['properties']['isOpen'], data['areas'][2]['properties']['id'])
eastRogers = [eastRogersNorth, eastRogersSouth]
# West Rogers
westRogersWest = WRA(data['areas'][3]['properties']['nameEn'], data['areas']
[3]['properties']['isOpen'], data['areas'][3]['properties']['id'])
westRogersSouth = WRA(data['areas'][4]['properties']['nameEn'], data['areas']
[4]['properties']['isOpen'], data['areas'][4]['properties']['id'])
westRogersNorthEast = WRA(data['areas'][5]['properties']['nameEn'], data['areas']
[5]['properties']['isOpen'], data['areas'][5]['properties']['id'])
westRogers = [westRogersWest, westRogersSouth, westRogersNorthEast]
# Fortitude
fortitude = WRA(data['areas'][6]['properties']['nameEn'], data['areas']
[6]['properties']['isOpen'], data['areas'][6]['properties']['id'])
closedWRA = 'Closed WRAs: '
openWRA = 'Open WRAs: '
ERbool = []
WRbool = []
for area in eastRogers:
ERbool.append(area.isOpen)
if all(ERbool) == True:
openWRA += 'East Rogers, '
else:
closedWRA += 'East Rogers, '
for area in westRogers:
WRbool.append(area.isOpen)
if all(WRbool) == True:
openWRA += 'West Rogers, '
else:
closedWRA += 'West Rogers, '
if fortitude.isOpen == True:
openWRA += (fortitude.name + ', ')
else:
closedWRA += (fortitude.name + ', ')
if shaughnessy.isOpen == True:
openWRA += (shaughnessy.name + ', ')
else:
closedWRA += (shaughnessy.name + ', ')
# Zones
closedZone = 'Closed Areas: '
openZone = 'Open Areas: '
for area in data['areas']:
try:
if (area['properties']['parentFeatureId'] == shaughnessy.wraID):
if shaughnessy.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == eastRogersNorth.wraID):
if eastRogersNorth.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == eastRogersSouth.wraID):
if eastRogersSouth.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == westRogersWest.wraID):
if westRogersWest.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == westRogersSouth.wraID):
if westRogersSouth.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == westRogersNorthEast.wraID):
if westRogersNorthEast.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == fortitude.wraID):
if fortitude.isOpen == True and area['properties']['isOpen'] == True:
openZone += (area['properties']['nameEn'] + ', ')
else:
closedZone += (area['properties']['nameEn'] + ', ')
except:
pass
# Parking
closedParking = 'Closed Parking: '
openParking = 'Open Parking: '
for parking in data['parkingLots']:
if parking['properties']['isOpen'] == True:
openParking += (parking['properties']['nameEn'] + ', ')
else:
closedParking += (parking['properties']['nameEn'] + ', ')
# Concat and save results for dB
status = (titleStr + '\n' + openWRA[:-2]
+ '\n' + closedWRA[:-2] + '\n' + openZone[:-2]
+ '\n' + closedZone[:-2] + '\n' + openParking[:-2]
+ '\n' + closedParking[:-2])
status_date = datetime.datetime.utcnow().date()
# Append to dB
rpdata = Info(status, status_date)
db.session.add(rpdata)
db.session.commit()
def get_status_now():
date = datetime.datetime.now(tz).strftime('%Y-%m-%d')
data = requests.get(
'https://www.pc.gc.ca/apps/rogers-pass/data/publish-%s' % date).json()
# Not available:
if data == {'error': 'not_found', 'reason': 'missing'}:
output = 'Info for today is not available at this time. Updates usually come at 7AM, and until one has been posted all areas are CLOSED. Try again later'
return output
else:
# Date
rawDate = data['validFrom']['PST'].split('T')[0]
rawTime = data['validFrom']['PST'].split('T')[1].split('-')[0][:-3]
statusDate = rawDate + '@' + rawTime
titleStr = 'Status for ' + statusDate + ':'
# Shaughnessy
shaughnessy = WRA(data['areas'][0]['properties']['nameEn'], data['areas']
[0]['properties']['isOpen'], data['areas'][0]['properties']['id'])
# East Rogers
eastRogersNorth = WRA(data['areas'][1]['properties']['nameEn'], data['areas']
[1]['properties']['isOpen'], data['areas'][1]['properties']['id'])
eastRogersSouth = WRA(data['areas'][2]['properties']['nameEn'], data['areas']
[2]['properties']['isOpen'], data['areas'][2]['properties']['id'])
eastRogers = [eastRogersNorth, eastRogersSouth]
# West Rogers
westRogersWest = WRA(data['areas'][3]['properties']['nameEn'], data['areas']
[3]['properties']['isOpen'], data['areas'][3]['properties']['id'])
westRogersSouth = WRA(data['areas'][4]['properties']['nameEn'], data['areas']
[4]['properties']['isOpen'], data['areas'][4]['properties']['id'])
westRogersNorthEast = WRA(data['areas'][5]['properties']['nameEn'], data['areas']
[5]['properties']['isOpen'], data['areas'][5]['properties']['id'])
westRogers = [westRogersWest, westRogersSouth, westRogersNorthEast]
# Fortitude
fortitude = WRA(data['areas'][6]['properties']['nameEn'], data['areas']
[6]['properties']['isOpen'], data['areas'][6]['properties']['id'])
closedWRA = 'Closed WRAs: '
openWRA = 'Open WRAs: '
ERbool = []
WRbool = []
for area in eastRogers:
ERbool.append(area.isOpen)
if all(ERbool) == True:
openWRA += 'East Rogers, '
else:
closedWRA += 'East Rogers, '
for area in westRogers:
WRbool.append(area.isOpen)
if all(WRbool) == True:
openWRA += 'West Rogers, '
else:
closedWRA += 'West Rogers, '
if fortitude.isOpen == True:
openWRA += (fortitude.name + ', ')
else:
closedWRA += (fortitude.name + ', ')
if shaughnessy.isOpen == True:
openWRA += (shaughnessy.name + ', ')
else:
closedWRA += (shaughnessy.name + ', ')
# Zones
closedZone = 'Special Closures: '
for area in data['areas']:
# Logic check: parent is open & area is closed = special closure
try:
if (area['properties']['parentFeatureId'] == shaughnessy.wraID):
if shaughnessy.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == eastRogersNorth.wraID):
if eastRogersNorth.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == eastRogersSouth.wraID):
if eastRogersSouth.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == westRogersWest.wraID):
if westRogersWest.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == westRogersSouth.wraID):
if westRogersSouth.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == westRogersNorthEast.wraID):
if westRogersNorthEast.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
if (area['properties']['parentFeatureId'] == fortitude.wraID):
if fortitude.isOpen == True and area['properties']['isOpen'] == False:
closedZone += (area['properties']['nameEn'] + ', ')
except:
pass
# Add special Closure to start of message
if closedZone != 'Special Closures: ':
closedZone = closedZone[:-2] + ' - Please check Parks Canada for more details!'
titleStr += + '\n' + closedZone
# Parking
closedParking = 'Closed Parking: '
openParking = 'Open Parking: '
for parking in data['parkingLots']:
if parking['properties']['isOpen'] == True:
openParking += (parking['properties']['nameEn'] + ', ')
else:
closedParking += (parking['properties']['nameEn'] + ', ')
# Concat and save results for dB
status = (titleStr
+ '\n' + '\n' + openWRA[:-2]
+ '\n' + '\n' + closedWRA[:-2]
+ '\n' + '\n' + openParking[:-2]
+ '\n' + '\n' + closedParking[:-2])
return status