Mise à jour pour les arrêts

This commit is contained in:
Sasha MOREL 2018-07-20 15:43:31 +02:00
parent 3675d786e4
commit 58447d04fc
1 changed files with 40 additions and 111 deletions

151
stop.py
View File

@ -1,89 +1,38 @@
''' '''
Provides all info from a stop Fourni les informations sur les arrêts
''' '''
from libs import get_data_from_json, hms2seconds from libs import get_data_from_json, hms2seconds
from time import time from time import time
stop_schedule_url = 'http://plandynamique.infotbm.com/api/schedules?stop=%d' stop_schedule_url = 'https://ws.infotbm.com/ws/1.0/get-realtime-pass/%d/%s'
def get_stop_list ():
'''
Gets stop name after its number
'''
stop_list = {}
# Trams
data = get_data_from_json ('http://plandynamique.infotbm.com/api/file/trams.xml')
for i in data ['arret']:
num = []
loc = None
try:
loc = (float (i ['x']), float (i ['y']))
except (KeyError, ValueError, TypeError):
pass
if type (i ['ligne']) == dict:
num.append (int (i ['ligne'] ['StopPointExternalCode'].replace ('TBT', '')))
else:
for j in i ['ligne']:
num.append (int (j ['StopPointExternalCode'].replace ('TBT', '')))
for j in num:
stop_list [j] = {'name': i ['nom'], 'location': loc}
# Bus
data = get_data_from_json ('http://plandynamique.infotbm.com/api/file/bus.xml')
for i in data ['arret']:
n = int (i ['StopPointExternalCode'].replace ('TBC', ''))
loc = None
try:
loc = (float (i ['x']), float (i ['y']))
except (KeyError, ValueError, TypeError):
pass
stop_list [n] = {'name': i ['StopName'], 'location': loc}
return (stop_list)
class Stop (): class Stop ():
''' '''
Gather stop informations Récupère les informations sur un arrêt
data format as returned by infotbm: data format as returned by infotbm:
[ Format des données retournées pas le site
{ {
origin: str, # source de l'information, BDSI tout le temps semble-t-il destinations: {
type: str, # type de ligne (Bus, Tramway, Ferry) <destination_stop_id>: [
message: str,
code: str, # nom de la ligne
name: str, # nom complet de la ligne
schedules: [
{ {
arrival: str, destination_name: str
arrival_commande: str, realtime: 1 si suivi, 0 sinon
arrival_theorique: str, vehicle_id: str
comment: str, vehicle_lattitude: float
departure: str, vehicle_longitude: float
departure_commande: str, waittime: HH:MM:SS
departure_theorique: str,
destination_id: int,
destination_name: str,
origin: str,
realtime: int,
schedule_id: int,
trip_id: int,
updated_at: str, # last data update
vehicle_id: int,
vehicle_lattitude: float,
vehicle_longitude: float,
vehicle_position_updated_at: str, # last update from vehicle
waittime: str, # estimated wait time in %H:%M:%S format
waittime_text: str, # human readable estimated wait time written in French
}, },
], ]
} }
] }
''' '''
def __init__ (self, number, autoupdate_at_creation = True, autoupdate = False, autoupdate_delay = -1): def __init__ (self, number, line, autoupdate_at_creation = True, autoupdate = False, autoupdate_delay = -1):
self.number = number self.number = number
self.line = line
self.last_update = 0 self.last_update = 0
self.data = None self.data = None
if autoupdate_at_creation: if autoupdate_at_creation:
@ -91,62 +40,45 @@ class Stop ():
def update (self, auto = False): def update (self, auto = False):
''' '''
Updates data Met à jour les données
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 (stop_schedule_url % self.number) d = get_data_from_json (stop_schedule_url % (self.number, self.line)) ['destinations']
self.last_update = time () self.last_update = time ()
if type (d) == list: if type (d) == dict:
self.data = [] self.data = []
# let's simplify the data # let's simplify the data
for i in d: for i in d:
line = { for j in d [i]:
'id': i ['code'],
'name': i ['name'],
'vehicle_type': i ['type'],
'vehicles': [],
}
for j in i ['schedules']:
loc = None loc = None
try: try:
loc = (float (j ['vehicle_lattitude']), float (j ['vehicle_longitude'])) loc = (float (j ['vehicle_lattitude']), float (j ['vehicle_longitude']))
except TypeError: except TypeError:
pass pass
line ['vehicles'].append ({ vehicle = {
'id': j ['vehicle_id'], 'id': j ['vehicle_id'],
'destination': j ['destination_name'], 'destination': j ['destination_name'],
'realtime': j ['realtime'] == '1', 'realtime': j ['realtime'] == '1',
'location': loc, 'location': loc,
'wait_time': hms2seconds (j ['waittime']), 'wait_time': hms2seconds (j ['waittime']),
'arrival': int (self.last_update + hms2seconds (j ['waittime'])), 'arrival': int (self.last_update + hms2seconds (j ['waittime'])),
}) }
self.data.append (line) self.data.append (vehicle)
else: else:
self.last_update = 0 self.last_update = 0
def data_age (self): def data_age (self):
''' '''
Computes the data's age Retourne l'âge des données
''' '''
return (time () - self.last_update) return (time () - self.last_update)
def lines (self): def get_line (self):
'''
List lines on the stop
Return unique indexes as known by the class
'''
return (list (range (0, len (self.data))))
def get_line (self, line):
class Line (): class Line ():
''' '''
Information about a line on a stop Information sur la ligne déservie à un arrêt
''' '''
def __init__ (self, data): def __init__ (self, data):
self.id = data ['id'] self.ve = data
self.name = data ['name']
self.vehicle_type = data ['vehicle_type']
self.ve = data ['vehicles']
def vehicles (self): def vehicles (self):
return (list (range (0, len (self.ve)))) return (list (range (0, len (self.ve))))
@ -154,7 +86,7 @@ class Stop ():
def get_vehicle (self, vehicle): def get_vehicle (self, vehicle):
class Vehicle (): class Vehicle ():
''' '''
Information about a vehicle Information sur un passage de véhicule
''' '''
def __init__ (self, data): def __init__ (self, data):
self.id = data ['id'] self.id = data ['id']
@ -166,21 +98,18 @@ class Stop ():
return (Vehicle (self.ve [vehicle])) return (Vehicle (self.ve [vehicle]))
return (Line (self.data [line])) return (Line (self.data))
if __name__ == '__main__': if __name__ == '__main__':
from datetime import datetime from datetime import datetime
stops = get_stop_list () for i in ((3687, 'A'), (1922, '32')):
for i in (3687, 1922, 5832, 3443, 3648): s = Stop (i [0], i [1])
print (str (i) + ' (' + stops [i] ['name'] + ') ' + str (stops [i] ['location'])) line = s.get_line ()
s = Stop (i) print ('\t' + i [1])
for j in s.lines (): for k in line.vehicles ():
line = s.get_line (j) v = line.get_vehicle (k)
print ('\t' + line.vehicle_type + ' ' + line.id + ' (' + line.name + ')') if v.is_realtime:
for k in line.vehicles (): print ('\t\t' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)
v = line.get_vehicle (k) else:
if v.is_realtime: print ('\t\t~' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)
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)