infotbm/stop.py

135 lines
3.4 KiB
Python
Raw Normal View History

2017-11-18 16:38:59 +01:00
'''
2018-07-20 15:43:31 +02:00
Fourni les informations sur les arrêts
2017-11-18 16:38:59 +01:00
'''
2017-11-21 21:07:08 +01:00
from libs import get_data_from_json, hms2seconds
2017-11-18 16:38:59 +01:00
from time import time
from urllib.parse import quote_plus
2017-11-18 16:38:59 +01:00
search_stop_url = 'https://ws.infotbm.com/ws/1.0/get-schedule/%s'
stop_info_url = 'https://ws.infotbm.com/ws/1.0/network/stoparea-informations/%s'
2018-07-20 15:43:31 +02:00
stop_schedule_url = 'https://ws.infotbm.com/ws/1.0/get-realtime-pass/%d/%s'
2017-11-18 16:38:59 +01:00
def search_stop_name (keyword):
'''
Recherche la référence d'un nom d'arrêt
'''
d = get_data_from_json (search_stop_url % quote_plus (keyword))
r = []
for i in d:
r.append ({
'name': i ['name'],
'city': i ['city'],
'ref': i ['id'],
})
return (r)
2017-11-18 16:38:59 +01:00
class Stop ():
'''
2018-07-20 15:43:31 +02:00
Récupère les informations sur un arrêt
2017-11-18 16:38:59 +01:00
data format as returned by infotbm:
2018-07-20 15:43:31 +02:00
Format des données retournées pas le site
{
destinations: {
<destination_stop_id>: [
2017-11-18 16:38:59 +01:00
{
2018-07-20 15:43:31 +02:00
destination_name: str
realtime: 1 si suivi, 0 sinon
vehicle_id: str
vehicle_lattitude: float
vehicle_longitude: float
waittime: HH:MM:SS
2017-11-18 16:38:59 +01:00
},
2018-07-20 15:43:31 +02:00
]
2017-11-18 16:38:59 +01:00
}
2018-07-20 15:43:31 +02:00
}
2017-11-18 16:38:59 +01:00
'''
2018-07-20 15:43:31 +02:00
def __init__ (self, number, line, autoupdate_at_creation = True, autoupdate = False, autoupdate_delay = -1):
2017-11-18 16:38:59 +01:00
self.number = number
2018-07-20 15:43:31 +02:00
self.line = line
2017-11-18 16:38:59 +01:00
self.last_update = 0
self.data = None
if autoupdate_at_creation:
self.update ()
def update (self, auto = False):
'''
2018-07-20 15:43:31 +02:00
Met à jour les données
2017-11-18 16:38:59 +01:00
'''
2018-07-20 15:43:31 +02:00
d = get_data_from_json (stop_schedule_url % (self.number, self.line)) ['destinations']
2017-11-21 21:07:08 +01:00
self.last_update = time ()
2018-07-20 15:43:31 +02:00
if type (d) == dict:
2017-11-21 21:07:08 +01:00
self.data = []
# let's simplify the data
for i in d:
2018-07-20 15:43:31 +02:00
for j in d [i]:
2017-11-21 21:07:08 +01:00
loc = None
try:
loc = (float (j ['vehicle_lattitude']), float (j ['vehicle_longitude']))
except TypeError:
pass
2018-07-20 15:43:31 +02:00
vehicle = {
2017-11-21 21:07:08 +01:00
'id': j ['vehicle_id'],
'destination': j ['destination_name'],
'realtime': j ['realtime'] == '1',
'location': loc,
'wait_time': hms2seconds (j ['waittime']),
'arrival': int (self.last_update + hms2seconds (j ['waittime'])),
2018-07-20 15:43:31 +02:00
}
self.data.append (vehicle)
2017-11-18 16:38:59 +01:00
else:
2017-11-21 21:07:08 +01:00
self.last_update = 0
2017-11-18 16:38:59 +01:00
def data_age (self):
'''
2018-07-20 15:43:31 +02:00
Retourne l'âge des données
2017-11-18 16:38:59 +01:00
'''
return (time () - self.last_update)
2018-07-20 15:43:31 +02:00
def get_line (self):
2017-11-18 16:38:59 +01:00
class Line ():
'''
2018-07-20 15:43:31 +02:00
Information sur la ligne déservie à un arrêt
2017-11-18 16:38:59 +01:00
'''
def __init__ (self, data):
2018-07-20 15:43:31 +02:00
self.ve = data
2017-11-18 16:38:59 +01:00
def vehicles (self):
2017-11-21 21:07:08 +01:00
return (list (range (0, len (self.ve))))
2017-11-18 16:38:59 +01:00
def get_vehicle (self, vehicle):
class Vehicle ():
'''
2018-07-20 15:43:31 +02:00
Information sur un passage de véhicule
2017-11-18 16:38:59 +01:00
'''
def __init__ (self, data):
2017-11-21 21:07:08 +01:00
self.id = data ['id']
self.location = data ['location']
self.destination = data ['destination']
self.is_realtime = data ['realtime']
self.wait_time = data ['wait_time']
self.arrival = data ['arrival']
2017-11-18 16:38:59 +01:00
2017-11-21 21:07:08 +01:00
return (Vehicle (self.ve [vehicle]))
2017-11-18 16:38:59 +01:00
2018-07-20 15:43:31 +02:00
return (Line (self.data))
2017-11-18 16:38:59 +01:00
if __name__ == '__main__':
from datetime import datetime
print (search_stop_name ('Réinson'))
print (search_stop_name ('Gravière'))
for i in ((3687, 'A'), (5459, '32')):
2018-07-20 15:43:31 +02:00
s = Stop (i [0], i [1])
line = s.get_line ()
print ('\t' + i [1])
for k in line.vehicles ():
v = line.get_vehicle (k)
if v.is_realtime:
print ('\t\t' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)
else:
print ('\t\t~' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)