infotbm/stop.py

186 lines
5.0 KiB
Python
Raw Normal View History

2017-11-18 16:38:59 +01:00
'''
Provides all info from a stop
'''
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
stop_schedule_url = 'http://plandynamique.infotbm.com/api/schedules?stop=%d'
def get_stop_list ():
'''
Gets stop name after its number
'''
2018-02-22 21:53:02 +01:00
stop_list = {}
2017-11-18 16:38:59 +01:00
# Trams
data = get_data_from_json ('http://plandynamique.infotbm.com/api/file/trams.xml')
for i in data ['arret']:
2017-11-21 21:07:08 +01:00
num = []
loc = None
try:
loc = (float (i ['x']), float (i ['y']))
2018-02-22 21:53:02 +01:00
except (KeyError, ValueError, TypeError):
2017-11-21 21:07:08 +01:00
pass
2017-11-18 16:38:59 +01:00
if type (i ['ligne']) == dict:
2017-11-21 21:07:08 +01:00
num.append (int (i ['ligne'] ['StopPointExternalCode'].replace ('TBT', '')))
2017-11-18 16:38:59 +01:00
else:
for j in i ['ligne']:
2017-11-21 21:07:08 +01:00
num.append (int (j ['StopPointExternalCode'].replace ('TBT', '')))
for j in num:
2018-02-22 21:53:02 +01:00
stop_list [j] = {'name': i ['nom'], 'location': loc}
# Bus
2017-11-18 16:38:59 +01:00
data = get_data_from_json ('http://plandynamique.infotbm.com/api/file/bus.xml')
for i in data ['arret']:
n = int (i ['StopPointExternalCode'].replace ('TBC', ''))
2017-11-21 21:07:08 +01:00
loc = None
try:
loc = (float (i ['x']), float (i ['y']))
2018-02-22 21:53:02 +01:00
except (KeyError, ValueError, TypeError):
2017-11-21 21:07:08 +01:00
pass
2018-02-22 21:53:02 +01:00
stop_list [n] = {'name': i ['StopName'], 'location': loc}
2017-11-18 16:38:59 +01:00
2018-02-22 21:53:02 +01:00
return (stop_list)
2017-11-18 16:38:59 +01:00
class Stop ():
'''
Gather stop informations
data format as returned by infotbm:
[
{
origin: str, # source de l'information, BDSI tout le temps semble-t-il
type: str, # type de ligne (Bus, Tramway, Ferry)
message: str,
code: str, # nom de la ligne
name: str, # nom complet de la ligne
schedules: [
{
arrival: str,
arrival_commande: str,
arrival_theorique: str,
comment: str,
departure: str,
departure_commande: str,
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
2017-11-18 16:38:59 +01:00
},
],
}
]
'''
def __init__ (self, number, autoupdate_at_creation = True, autoupdate = False, autoupdate_delay = -1):
self.number = number
self.last_update = 0
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
'''
2017-11-21 21:07:08 +01:00
d = get_data_from_json (stop_schedule_url % self.number)
self.last_update = time ()
2018-02-22 21:53:02 +01:00
if type (d) == list:
2017-11-21 21:07:08 +01:00
self.data = []
# let's simplify the data
for i in d:
2018-02-22 21:53:02 +01:00
line = {
2017-11-21 21:07:08 +01:00
'id': i ['code'],
'name': i ['name'],
'vehicle_type': i ['type'],
'vehicles': [],
}
for j in i ['schedules']:
loc = None
try:
loc = (float (j ['vehicle_lattitude']), float (j ['vehicle_longitude']))
except TypeError:
pass
2018-02-22 21:53:02 +01:00
line ['vehicles'].append ({
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-02-22 21:53:02 +01:00
self.data.append (line)
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):
'''
Computes the data's age
'''
return (time () - self.last_update)
def lines (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 ():
'''
Information about a line on a stop
'''
def __init__ (self, data):
2017-11-21 21:07:08 +01:00
self.id = data ['id']
self.name = data ['name']
self.vehicle_type = data ['vehicle_type']
self.ve = data ['vehicles']
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 ():
'''
Information about a vehicle
'''
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
return (Line (self.data [line]))
if __name__ == '__main__':
from datetime import datetime
2017-11-21 21:07:08 +01:00
stops = get_stop_list ()
for i in (3687, 1922, 5832, 3443, 3648):
print (str (i) + ' (' + stops [i] ['name'] + ') ' + str (stops [i] ['location']))
2017-11-18 16:38:59 +01:00
s = Stop (i)
for j in s.lines ():
2018-02-22 21:53:02 +01:00
line = s.get_line (j)
print ('\t' + line.vehicle_type + ' ' + line.id + ' (' + line.name + ')')
for k in line.vehicles ():
v = line.get_vehicle (k)
2017-11-21 21:07:08 +01:00
if v.is_realtime:
print ('\t\t' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)
2017-11-18 16:38:59 +01:00
else:
print ('\t\t~' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)