18
18
"gbfs_BA" : "fgb"
19
19
}
20
20
21
- def process_station_info (url ):
21
+ def process_motivate_stations (motivate_id ):
22
+ url = "https://layer.bicyclesharing.net/map/v1/%s/stations" % motivate_id
23
+ result = urlfetch .fetch (url , validate_certificate = True )
24
+ if result .status_code != 200 :
25
+ return []
26
+ response_json = json .loads (result .content )
27
+ out = []
28
+ for feature in response_json ['features' ]:
29
+ pt = feature ['geometry' ]['coordinates' ]
30
+ station = feature ['properties' ]
31
+ el = SystemInfoElement (
32
+ id = station ['station_id' ],
33
+ name = station ['name' ],
34
+ lat = pt [1 ],
35
+ lon = pt [0 ],
36
+ )
37
+ out .append (el )
38
+ return out
39
+
40
+ def process_station_info (url , system_id ):
41
+ if not url :
42
+ return []
43
+ if system_id in MOTIVATE_IDS :
44
+ return process_motivate_stations (MOTIVATE_IDS [system_id ])
22
45
result = urlfetch .fetch (url , validate_certificate = True )
23
46
if result .status_code != 200 :
24
47
return []
@@ -40,16 +63,20 @@ def process_station_info(url):
40
63
return out
41
64
42
65
def process_system_info (url ):
66
+ if not url :
67
+ return {}
43
68
result = urlfetch .fetch (url , validate_certificate = True )
44
69
if result .status_code != 200 :
45
- return None
70
+ return {}
46
71
response_json = json .loads (result .content )
47
72
return response_json ['data' ]
48
73
49
74
def process_regions (url ):
75
+ if not url :
76
+ return []
50
77
result = urlfetch .fetch (url , validate_certificate = True )
51
78
if result .status_code != 200 :
52
- return None
79
+ return []
53
80
response_json = json .loads (result .content )
54
81
out = []
55
82
for region in response_json ['data' ]['regions' ]:
@@ -62,30 +89,40 @@ def process_regions(url):
62
89
return out
63
90
64
91
@cache (ttl = STATION_STATUS_TTL )
65
- def process_free_bikes (url ):
92
+ def _process_free_bikes (url ):
66
93
result = urlfetch .fetch (url , validate_certificate = True )
67
94
if result .status_code != 200 :
68
- return None
95
+ return []
69
96
response_json = json .loads (result .content )
70
97
out = []
71
98
for bike in response_json ['data' ]['bikes' ]:
72
99
if bike ['is_reserved' ] == 0 and bike ['is_disabled' ] == 0 :
73
100
out .append (SystemInfoElement (id = bike ['bike_id' ],name = bike .get ('name' ,'Bike' ),lat = bike ['lat' ],lon = bike ['lon' ]))
74
101
return out
75
102
103
+ def process_free_bikes (url ):
104
+ if not url :
105
+ return []
106
+ return _process_free_bikes (url )
107
+
76
108
@cache (ttl = ALERTS_TTL )
77
- def process_alerts (url ):
109
+ def _process_alerts (url ):
78
110
result = urlfetch .fetch (url , validate_certificate = True )
79
111
if result .status_code != 200 :
80
- return None
112
+ return []
81
113
response_json = json .loads (result .content )
82
114
return response_json ['data' ]['alerts' ]
83
115
116
+ def process_alerts (url ):
117
+ if not url :
118
+ return []
119
+ return _process_alerts (url )
120
+
84
121
def process_motivate_status (motivate_id ):
85
122
url = "https://layer.bicyclesharing.net/map/v1/%s/stations" % motivate_id
86
123
result = urlfetch .fetch (url , validate_certificate = True )
87
124
if result .status_code != 200 :
88
- return {}
125
+ return []
89
126
response_json = json .loads (result .content )
90
127
out = []
91
128
for feature in response_json ['features' ]:
@@ -113,31 +150,13 @@ def process_motivate_status(motivate_id):
113
150
pts = pts ))
114
151
return out
115
152
116
- def process_motivate_stations (motivate_id ):
117
- url = "https://layer.bicyclesharing.net/map/v1/%s/stations" % motivate_id
118
- result = urlfetch .fetch (url , validate_certificate = True )
119
- if result .status_code != 200 :
120
- return {}
121
- response_json = json .loads (result .content )
122
- out = []
123
- for feature in response_json ['features' ]:
124
- pt = feature ['geometry' ]['coordinates' ]
125
- station = feature ['properties' ]
126
- el = SystemInfoElement (
127
- id = station ['station_id' ],
128
- name = station ['name' ],
129
- lat = pt [1 ],
130
- lon = pt [0 ],
131
- )
132
- out .append (el )
133
- return out
134
-
153
+ @cache (ttl = STATION_STATUS_TTL )
135
154
def _process_station_status (url , system_id ):
136
155
if system_id in MOTIVATE_IDS :
137
156
return process_motivate_status (MOTIVATE_IDS [system_id ])
138
157
result = urlfetch .fetch (url , validate_certificate = True )
139
158
if result .status_code != 200 :
140
- return None
159
+ return []
141
160
response_json = json .loads (result .content )
142
161
stations = response_json ['data' ]['stations' ]
143
162
out = []
@@ -155,8 +174,9 @@ def _process_station_status(url, system_id):
155
174
out .append (SystemStatusElement (id = station ['station_id' ],bikes = bikes ,docks = docks ,mod = mod ))
156
175
return out
157
176
158
- @cache (ttl = STATION_STATUS_TTL )
159
177
def process_station_status (url , system_id ):
178
+ if not url :
179
+ return []
160
180
return _process_station_status (url , system_id )
161
181
162
182
@@ -191,25 +211,25 @@ def load(self):
191
211
city = "%s, %s" % (line ['Location' ], line ['Country Code' ])
192
212
sys_info = process_system_info (config ['system_information' ])
193
213
system_id = "gbfs_%s" % sys_info .get ("system_id" , line ['System ID' ])
194
- if system_id in MOTIVATE_IDS :
195
- stations = process_motivate_stations (MOTIVATE_IDS [system_id ])
196
- else :
197
- stations = process_station_info (config ['station_information' ])
198
- regions = []
199
- if 'system_regions' in config :
200
- regions = process_regions (config ['system_regions' ])
214
+ stations = process_station_info (config .get ('station_information' ), system_id )
215
+ regions = process_regions (config .get ('system_regions' ))
216
+ bikes = process_free_bikes (config .get ('free_bike_status' ))
201
217
avg_lat = 0
202
218
avg_lon = 0
203
- station_count = 0
219
+ loc_count = 0
204
220
for station in stations :
205
221
avg_lat += station .lat
206
222
avg_lon += station .lon
207
- station_count += 1
208
- if station_count > 0 :
209
- avg_lat = avg_lat / station_count
210
- avg_lon = avg_lon / station_count
223
+ loc_count += 1
224
+ for bike in bikes :
225
+ avg_lat += bike .lat
226
+ avg_lon += bike .lon
227
+ loc_count += 1
228
+ if loc_count > 0 :
229
+ avg_lat = avg_lat / loc_count
230
+ avg_lon = avg_lon / loc_count
211
231
recent_ts = 0
212
- station_statuses = _process_station_status (config [ 'station_status' ] , system_id )
232
+ station_statuses = process_station_status (config . get ( 'station_status' ) , system_id )
213
233
for station in station_statuses :
214
234
ts = station .mod
215
235
if ts > recent_ts :
@@ -242,11 +262,7 @@ def get_info(self, system):
242
262
243
263
def get_status (self , system ):
244
264
status_header = [["id" ,"bikes" ,"docks" ,"mod" ,"pts" ]]
245
- station_statuses = process_station_status (system .config ['station_status' ], system .key .id ())
246
- alerts = []
247
- if 'system_alerts' in system .config :
248
- alerts = process_alerts (system .config ['system_alerts' ])
249
- bikes = []
250
- if 'free_bike_status' in system .config :
251
- bikes = process_free_bikes (system .config ['free_bike_status' ])
265
+ station_statuses = process_station_status (system .config .get ('station_status' ), system .key .id ())
266
+ alerts = process_alerts (system .config .get ('system_alerts' ))
267
+ bikes = process_free_bikes (system .config .get ('free_bike_status' ))
252
268
return {"statuses" : CompactElement .of (station_statuses ), "alerts" : alerts , 'bikes' : CompactElement .of (bikes )}
0 commit comments