2017-11-19 16:39:25 +01:00
|
|
|
|
'''
|
|
|
|
|
Provides all info about V³ stations
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
from libs import get_data_from_json
|
|
|
|
|
from time import time
|
|
|
|
|
|
2018-07-17 23:02:25 +02:00
|
|
|
|
vcub_url = 'https://ws.infotbm.com/ws/1.0/vcubs'
|
2017-11-19 16:39:25 +01:00
|
|
|
|
|
|
|
|
|
class Vcub ():
|
|
|
|
|
'''
|
2018-07-17 23:02:25 +02:00
|
|
|
|
Récupère les informations des stations V³
|
2017-11-19 16:39:25 +01:00
|
|
|
|
|
2018-07-17 23:02:25 +02:00
|
|
|
|
Format de données, tel que retourné par le site infotbm :
|
2017-11-19 16:39:25 +01:00
|
|
|
|
{
|
2018-07-17 23:02:25 +02:00
|
|
|
|
lists: [
|
|
|
|
|
{
|
|
|
|
|
'id': numéro de la station,
|
|
|
|
|
'name': str,
|
|
|
|
|
'connexionState': 'CONNECTEE' si en service 'DECONNECTEE' sinon,
|
|
|
|
|
'typeVlsPlus': 'VLS_PLUS' si V³+ 'PAS_VLS_PLUS' sinon,
|
|
|
|
|
'nbPlaceAvailable': 33,
|
|
|
|
|
'nbBikeAvailable': 0
|
|
|
|
|
'latitude': float,
|
|
|
|
|
'longitude': float,
|
|
|
|
|
},
|
|
|
|
|
]
|
2017-11-19 16:39:25 +01:00
|
|
|
|
}
|
|
|
|
|
'''
|
|
|
|
|
def __init__ (self, autoupdate_at_creation = True, autoupdate = False, autoupdate_delay = -1):
|
2017-11-19 16:50:20 +01:00
|
|
|
|
self.last_update = 0
|
2017-11-19 16:39:25 +01:00
|
|
|
|
self.data = None
|
|
|
|
|
if autoupdate_at_creation:
|
|
|
|
|
self.update ()
|
|
|
|
|
|
|
|
|
|
def update (self, auto = False):
|
|
|
|
|
'''
|
|
|
|
|
Updates data
|
|
|
|
|
auto optionnal param is to set if a update is a automatic one, and must be performed as defined in autoupdate_delay variable
|
|
|
|
|
'''
|
|
|
|
|
d = get_data_from_json (vcub_url)
|
|
|
|
|
# the original format is awfull, so I change it a little
|
2018-02-22 21:53:02 +01:00
|
|
|
|
if type (d) == dict:
|
2017-11-19 16:39:25 +01:00
|
|
|
|
self.data = {}
|
2018-07-17 23:02:25 +02:00
|
|
|
|
d = d ['lists']
|
2017-11-19 16:39:25 +01:00
|
|
|
|
for i in d:
|
|
|
|
|
e = {
|
2018-07-17 23:02:25 +02:00
|
|
|
|
'name': i ['name'],
|
|
|
|
|
'online': i ['connexionState'] == 'CONNECTEE',
|
|
|
|
|
'plus': i ['typeVlsPlus'] == 'VLS_PLUS',
|
|
|
|
|
'empty': int (i ['nbPlaceAvailable']),
|
|
|
|
|
'bikes': int (i ['nbBikeAvailable']),
|
|
|
|
|
'location': (float (i ['latitude']), float (i ['longitude']))
|
2018-02-22 21:53:02 +01:00
|
|
|
|
}
|
2018-07-17 23:02:25 +02:00
|
|
|
|
self.data [int (i ['id'])] = e
|
2017-11-19 16:50:20 +01:00
|
|
|
|
self.last_update = time ()
|
|
|
|
|
|
|
|
|
|
def data_age (self):
|
|
|
|
|
'''
|
|
|
|
|
Computes the data's age
|
|
|
|
|
'''
|
|
|
|
|
return (time () - self.last_update)
|
2017-11-19 16:39:25 +01:00
|
|
|
|
|
|
|
|
|
def get_names (self):
|
|
|
|
|
'''
|
|
|
|
|
Returns all names in a dict with id as data
|
|
|
|
|
'''
|
|
|
|
|
r = {}
|
|
|
|
|
for i in self.data:
|
|
|
|
|
r [self.data [i] ['name']] = i
|
|
|
|
|
return (r)
|
|
|
|
|
|
|
|
|
|
def get_locations (self):
|
|
|
|
|
'''
|
|
|
|
|
Returns all locations in a dict with id as data
|
|
|
|
|
'''
|
|
|
|
|
r = {}
|
|
|
|
|
for i in self.data:
|
|
|
|
|
r [self.data [i] ['location']] = i
|
|
|
|
|
return (r)
|
|
|
|
|
|
|
|
|
|
def get_by_id (self, id):
|
|
|
|
|
'''
|
|
|
|
|
Returns a station by its id
|
|
|
|
|
'''
|
|
|
|
|
class Station ():
|
|
|
|
|
'''
|
|
|
|
|
A V³ station
|
|
|
|
|
'''
|
|
|
|
|
def __init__ (self, data, id):
|
|
|
|
|
self.data = data
|
|
|
|
|
self.id = id
|
|
|
|
|
self.name = self.data ['name']
|
|
|
|
|
self.location = self.data ['location']
|
|
|
|
|
self.online = self.data ['online']
|
|
|
|
|
self.isplus = self.data ['plus']
|
|
|
|
|
self.bikes = self.data ['bikes']
|
|
|
|
|
self.empty = self.data ['empty']
|
|
|
|
|
|
|
|
|
|
def __int__ (self):
|
|
|
|
|
return (self.id)
|
|
|
|
|
|
|
|
|
|
return (Station (self.data [id], id))
|
|
|
|
|
|
2018-02-22 21:53:02 +01:00
|
|
|
|
|
2017-11-19 16:39:25 +01:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
v = Vcub ()
|
|
|
|
|
for i in (v.get_by_id (149), v.get_by_id (v.get_names ()['Buttiniere']), v.get_by_id (v.get_locations () [(44.8875, -0.51763)])):
|
|
|
|
|
print ('%s (%d) (%f, %f)%s%s\n\tbikes: %d\n\tfree: %d\n\t' % (i.name, i, i.location [0], i.location [1], i.isplus and ' (VCUB+)' or '', i.online and ' ' or ' OFFLINE', i.bikes, i.empty))
|