1818 "gbfs_BA" : "fgb"
1919}
2020
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 ])
2245 result = urlfetch .fetch (url , validate_certificate = True )
2346 if result .status_code != 200 :
2447 return []
@@ -40,16 +63,20 @@ def process_station_info(url):
4063 return out
4164
4265def process_system_info (url ):
66+ if not url :
67+ return {}
4368 result = urlfetch .fetch (url , validate_certificate = True )
4469 if result .status_code != 200 :
45- return None
70+ return {}
4671 response_json = json .loads (result .content )
4772 return response_json ['data' ]
4873
4974def process_regions (url ):
75+ if not url :
76+ return []
5077 result = urlfetch .fetch (url , validate_certificate = True )
5178 if result .status_code != 200 :
52- return None
79+ return []
5380 response_json = json .loads (result .content )
5481 out = []
5582 for region in response_json ['data' ]['regions' ]:
@@ -62,30 +89,40 @@ def process_regions(url):
6289 return out
6390
6491@cache (ttl = STATION_STATUS_TTL )
65- def process_free_bikes (url ):
92+ def _process_free_bikes (url ):
6693 result = urlfetch .fetch (url , validate_certificate = True )
6794 if result .status_code != 200 :
68- return None
95+ return []
6996 response_json = json .loads (result .content )
7097 out = []
7198 for bike in response_json ['data' ]['bikes' ]:
7299 if bike ['is_reserved' ] == 0 and bike ['is_disabled' ] == 0 :
73100 out .append (SystemInfoElement (id = bike ['bike_id' ],name = bike .get ('name' ,'Bike' ),lat = bike ['lat' ],lon = bike ['lon' ]))
74101 return out
75102
103+ def process_free_bikes (url ):
104+ if not url :
105+ return []
106+ return _process_free_bikes (url )
107+
76108@cache (ttl = ALERTS_TTL )
77- def process_alerts (url ):
109+ def _process_alerts (url ):
78110 result = urlfetch .fetch (url , validate_certificate = True )
79111 if result .status_code != 200 :
80- return None
112+ return []
81113 response_json = json .loads (result .content )
82114 return response_json ['data' ]['alerts' ]
83115
116+ def process_alerts (url ):
117+ if not url :
118+ return []
119+ return _process_alerts (url )
120+
84121def process_motivate_status (motivate_id ):
85122 url = "https://layer.bicyclesharing.net/map/v1/%s/stations" % motivate_id
86123 result = urlfetch .fetch (url , validate_certificate = True )
87124 if result .status_code != 200 :
88- return {}
125+ return []
89126 response_json = json .loads (result .content )
90127 out = []
91128 for feature in response_json ['features' ]:
@@ -113,31 +150,13 @@ def process_motivate_status(motivate_id):
113150 pts = pts ))
114151 return out
115152
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 )
135154def _process_station_status (url , system_id ):
136155 if system_id in MOTIVATE_IDS :
137156 return process_motivate_status (MOTIVATE_IDS [system_id ])
138157 result = urlfetch .fetch (url , validate_certificate = True )
139158 if result .status_code != 200 :
140- return None
159+ return []
141160 response_json = json .loads (result .content )
142161 stations = response_json ['data' ]['stations' ]
143162 out = []
@@ -155,8 +174,9 @@ def _process_station_status(url, system_id):
155174 out .append (SystemStatusElement (id = station ['station_id' ],bikes = bikes ,docks = docks ,mod = mod ))
156175 return out
157176
158- @cache (ttl = STATION_STATUS_TTL )
159177def process_station_status (url , system_id ):
178+ if not url :
179+ return []
160180 return _process_station_status (url , system_id )
161181
162182
@@ -191,25 +211,25 @@ def load(self):
191211 city = "%s, %s" % (line ['Location' ], line ['Country Code' ])
192212 sys_info = process_system_info (config ['system_information' ])
193213 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' ))
201217 avg_lat = 0
202218 avg_lon = 0
203- station_count = 0
219+ loc_count = 0
204220 for station in stations :
205221 avg_lat += station .lat
206222 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
211231 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 )
213233 for station in station_statuses :
214234 ts = station .mod
215235 if ts > recent_ts :
@@ -242,11 +262,7 @@ def get_info(self, system):
242262
243263 def get_status (self , system ):
244264 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' ))
252268 return {"statuses" : CompactElement .of (station_statuses ), "alerts" : alerts , 'bikes' : CompactElement .of (bikes )}
0 commit comments