Ajout de fonctions de recherche

On a maintenant la liaison entre les zones d'arrêts et un ensemble (ligne-arrêt) via les arrêts de chaque zone et les lignes de chaque arrêt.
This commit is contained in:
Sasha MOREL 2021-08-27 11:53:29 +02:00
parent 63fe328116
commit d83fc60afd
1 changed files with 101 additions and 23 deletions

108
stop.py
View File

@ -4,20 +4,32 @@ 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
from urllib.parse import quote_plus from urllib.parse import quote
from re import search
search_stop_url = 'https://ws.infotbm.com/ws/1.0/get-schedule/%s' 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' stop_info_url = 'https://ws.infotbm.com/ws/1.0/network/stoparea-informations/%s'
stop_schedule_url = 'https://ws.infotbm.com/ws/1.0/get-realtime-pass/%d/%s' stop_schedule_url = 'https://ws.infotbm.com/ws/1.0/get-realtime-pass/%d/%s'
def search_stop_name (keyword): def search_stop_by_name (keyword):
''' '''
Recherche la référence d'un nom d'arrêt Recherche la référence d'un nom d'arrêt
Format des données retournées par le site
[
{
id: str, nommé ref par la suite
name: str
type: str, mais je ne gère que "stop_area"
city: str
},
]
''' '''
d = get_data_from_json (search_stop_url % quote_plus (keyword)) d = get_data_from_json (search_stop_url % quote (keyword))
r = [] r = []
for i in d: for i in d:
if i ['type'] == 'stop_area':
r.append ({ r.append ({
'name': i ['name'], 'name': i ['name'],
'city': i ['city'], 'city': i ['city'],
@ -26,11 +38,68 @@ def search_stop_name (keyword):
return (r) return (r)
class Stop (): def show_stops_from_ref (ref):
'''
Affiche la liste des arrêts d'une référence donnée par search_stop_name
Format des données retournées par le site
{
id: str, contenu de la variable ref donnée
name: str
latitude: str, convertible en float
longitude: str, convertible en float
city: str
hasWheelchairBoarding: bool, accessibilité en fauteuil roulant
stopPoints: [
id: str
name: str, encore le nom
routes: [
{
id: str
name: str, nom du terminus
line: {
name: str, nom pour les humains
}
}
]
]
}
'''
d = get_data_from_json (stop_info_url % quote (ref))
r = {
'ref': d ['id'],
'name': d ['name'],
'latitude': float (d ['latitude']),
'longitude': float (d ['longitude']),
'city': d ['city'],
'stop_points': [],
}
for i in d ['stopPoints']:
s = {
'name': i ['name'],
'routes': [],
}
s ['id'] = int (search ('[0-9]+$', i ['id']).group ())
for j in i ['routes']:
rte = {
'terminus': j ['name'],
'line_human': j ['line'] ['name'],
}
line_id = search ('[0-9A-D]+$', rte ['line_human']).group ()
try:
line_id = '%02d' % int (line_id)
except ValueError:
pass
rte ['line_id'] = line_id
s ['routes'].append (rte)
r ['stop_points'].append (s)
return (r)
class StopRoute ():
''' '''
Récupère les informations sur un arrêt Récupère les informations sur un arrêt
data format as returned by infotbm:
Format des données retournées pas le site Format des données retournées pas le site
{ {
destinations: { destinations: {
@ -42,6 +111,7 @@ class Stop ():
vehicle_lattitude: float vehicle_lattitude: float
vehicle_longitude: float vehicle_longitude: float
waittime: HH:MM:SS waittime: HH:MM:SS
waittime_text: str, lisible pas un humain
}, },
] ]
} }
@ -78,6 +148,7 @@ class Stop ():
'realtime': j ['realtime'] == '1', 'realtime': j ['realtime'] == '1',
'location': loc, 'location': loc,
'wait_time': hms2seconds (j ['waittime']), 'wait_time': hms2seconds (j ['waittime']),
'wait_time_human': j ['waittime_text'],
'arrival': int (self.last_update + hms2seconds (j ['waittime'])), 'arrival': int (self.last_update + hms2seconds (j ['waittime'])),
} }
self.data.append (vehicle) self.data.append (vehicle)
@ -99,7 +170,9 @@ class Stop ():
self.ve = data self.ve = data
def vehicles (self): def vehicles (self):
if self.ve is not None:
return (list (range (0, len (self.ve)))) return (list (range (0, len (self.ve))))
return ([])
def get_vehicle (self, vehicle): def get_vehicle (self, vehicle):
class Vehicle (): class Vehicle ():
@ -112,6 +185,7 @@ class Stop ():
self.destination = data ['destination'] self.destination = data ['destination']
self.is_realtime = data ['realtime'] self.is_realtime = data ['realtime']
self.wait_time = data ['wait_time'] self.wait_time = data ['wait_time']
self.wait_time_text = data ['wait_time_human']
self.arrival = data ['arrival'] self.arrival = data ['arrival']
return (Vehicle (self.ve [vehicle])) return (Vehicle (self.ve [vehicle]))
@ -121,15 +195,19 @@ class Stop ():
if __name__ == '__main__': if __name__ == '__main__':
from datetime import datetime from datetime import datetime
print (search_stop_name ('Réinson')) for word in ('Gravière', 'Stade Chaban Delmas', 'Cassagne', 'Zorbut'):
print (search_stop_name ('Gravière')) print (word + ':')
for i in ((3687, 'A'), (5459, '32')): for area in search_stop_by_name (word):
s = Stop (i [0], i [1]) print ('\t' + area ['name'] + ' (' + area ['city'] + '):')
line = s.get_line () for stop in show_stops_from_ref (area ['ref']) ['stop_points']:
print ('\t' + i [1]) print ('\t\t' + stop ['name'] + ' (' + str (stop ['id']) + '):')
for k in line.vehicles (): for route in stop ['routes']:
v = line.get_vehicle (k) print ('\t\t\t' + route ['line_human'] + ' terminus ' + route ['terminus'] + ':')
sr = StopRoute (stop ['id'], route ['line_id'])
line = sr.get_line ()
for vehicle in line.vehicles ():
v = line.get_vehicle (vehicle)
if v.is_realtime: if v.is_realtime:
print ('\t\t' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination) print ('\t\t\t\t' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)
else: else:
print ('\t\t~' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination) print ('\t\t\t\t~' + str (v.wait_time) + ' (' + datetime.fromtimestamp (v.arrival).strftime ('%H:%M') + ') → ' + v.destination)