Modification de l'URL des infos de passage
L'identifiant de ligne est maintenant requis à la fin de l'URL. Cette information n'étant pas utile auparavent, j'ai donc ajouté sa récupération et son stockage.
This commit is contained in:
		
							parent
							
								
									c2d3dab3ee
								
							
						
					
					
						commit
						e81da464e5
					
				
							
								
								
									
										72
									
								
								src/main.py
								
								
								
								
							
							
						
						
									
										72
									
								
								src/main.py
								
								
								
								
							|  | @ -5,39 +5,39 @@ from stop_route import StopRoute | |||
| from datetime import datetime | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     for word in sys.argv[1:]: | ||||
|         for area in get_stop_areas_by_name(word): | ||||
|             stop = get_stop_by_id(area.getId()) | ||||
|             for stopPoint in stop.getStopPoints(): | ||||
|                 for route in stopPoint.getRoutes(): | ||||
|                     if "Tram" in route.getLineName(): | ||||
|                         stopRoute = StopRoute(stopPoint.getId(), route.getId()) | ||||
|                         line = stopRoute.get_line() | ||||
|                         for vehicule in line.get_vehicles(): | ||||
|                             v = line.get_vehicle(vehicule) | ||||
|                             if v.getRealtime(): | ||||
|                                 print( | ||||
|                                     str(v.getWaitTimeText()) | ||||
|                                     + " (" | ||||
|                                     + datetime.fromtimestamp(v.getArrival()).strftime( | ||||
|                                         "%H:%M" | ||||
|                                     ) | ||||
|                                     + ") → " | ||||
|                                     + v.getDestination() | ||||
|                                     + ", Curr location: " | ||||
|                                     + str(v.getLocation()) | ||||
|                                 ) | ||||
|                             else: | ||||
|                                 print( | ||||
|                                     "~" | ||||
|                                     + str(v.getWaitTimeText()) | ||||
|                                     + " (" | ||||
|                                     + datetime.fromtimestamp(v.getArrival()).strftime( | ||||
|                                         "%H:%M" | ||||
|                                     ) | ||||
|                                     + ") → " | ||||
|                                     + v.getDestination() | ||||
|                                     + ", Curr location: " | ||||
|                                     + str(v.getLocation()) | ||||
|                                 ) | ||||
| if __name__ == '__main__': | ||||
| 	for word in sys.argv [1:]: | ||||
| 		for area in get_stop_areas_by_name (word): | ||||
| 			stop = get_stop_by_id (area.getId ()) | ||||
| 			for stopPoint in stop.getStopPoints (): | ||||
| 				for route in stopPoint.getRoutes (): | ||||
| 					if 'Tram' in route.getLineName (): | ||||
| 						stopRoute = StopRoute (stopPoint.getId (), route.getId (), route.getLineId ()) | ||||
| 						line = stopRoute.get_line () | ||||
| 						for vehicule in line.get_vehicles (): | ||||
| 							v = line.get_vehicle (vehicule) | ||||
| 							if v.getRealtime (): | ||||
| 								print ( | ||||
| 									str (v.getWaitTimeText ()) | ||||
| 									+ ' (' | ||||
| 									+ datetime.fromtimestamp (v.getArrival ()).strftime ( | ||||
| 										'%H:%M' | ||||
| 									) | ||||
| 									+ ') → ' | ||||
| 									+ v.getDestination () | ||||
| 									+ ', Curr location: ' | ||||
| 									+ str (v.getLocation ()) | ||||
| 								) | ||||
| 							else: | ||||
| 								print ( | ||||
| 									'~' | ||||
| 									+ str (v.getWaitTimeText ()) | ||||
| 									+ ' (' | ||||
| 									+ datetime.fromtimestamp (v.getArrival ()).strftime ( | ||||
| 										'%H:%M' | ||||
| 									) | ||||
| 									+ ') → ' | ||||
| 									+ v.getDestination () | ||||
| 									+ ', Curr location: ' | ||||
| 									+ str (v.getLocation ()) | ||||
| 								) | ||||
							
								
								
									
										36
									
								
								src/route.py
								
								
								
								
							
							
						
						
									
										36
									
								
								src/route.py
								
								
								
								
							|  | @ -1,23 +1,27 @@ | |||
| class Route: | ||||
|     def __init__(self, name, line_name): | ||||
|         self.id = None | ||||
|         self.name = name | ||||
|         self.line_name = line_name | ||||
| 	def __init__ (self, line_id, name, line_name): | ||||
| 		self.id = None | ||||
| 		self.name = name | ||||
| 		self.line_name = line_name | ||||
| 		self.line_id = line_id | ||||
| 
 | ||||
|     def getId(self): | ||||
|         return self.id | ||||
| 	def getId (self): | ||||
| 		return self.id | ||||
| 
 | ||||
|     def getName(self): | ||||
|         return self.name | ||||
| 	def getName (self): | ||||
| 		return self.name | ||||
| 
 | ||||
|     def getLineName(self): | ||||
|         return self.line_name | ||||
| 	def getLineName (self): | ||||
| 		return self.line_name | ||||
| 	 | ||||
| 	def getLineId (self): | ||||
| 		return self.line_id | ||||
| 
 | ||||
|     def setId(self, id): | ||||
|         self.id = id | ||||
| 	def setId (self, id): | ||||
| 		self.id = id | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return self.name + " (" + self.line_name + ")" | ||||
| 	def __repr__ (self): | ||||
| 		return self.name + ' (' + self.line_name + ')' | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         return self.name + " (" + self.line_name + ")" | ||||
| 	def __str__ (self): | ||||
| 		return self.name + ' (' + self.line_name + ')' | ||||
							
								
								
									
										152
									
								
								src/stop.py
								
								
								
								
							
							
						
						
									
										152
									
								
								src/stop.py
								
								
								
								
							|  | @ -5,100 +5,100 @@ from re import search | |||
| from route import Route | ||||
| 
 | ||||
| 
 | ||||
| INFO_URL = "https://ws.infotbm.com/ws/1.0/network/stoparea-informations/%s" | ||||
| INFO_URL = 'https://ws.infotbm.com/ws/1.0/network/stoparea-informations/%s' | ||||
| 
 | ||||
| LINE_TRANSLATE = { | ||||
|     "Tram A": "A", | ||||
|     "Tram B": "B", | ||||
|     "Tram C": "C", | ||||
|     "Tram D": "D", | ||||
|     "TBNight": "58", | ||||
|     "BAT3": "69", | ||||
| 	'Tram A': 'A', | ||||
| 	'Tram B': 'B', | ||||
| 	'Tram C': 'C', | ||||
| 	'Tram D': 'D', | ||||
| 	'TBNight': '58', | ||||
| 	'BAT3': '69', | ||||
| } | ||||
| 
 | ||||
| LINE_TYPES = ( | ||||
|     "Tram", | ||||
|     "Corol", | ||||
|     "Lianes", | ||||
|     "Ligne", | ||||
|     "Bus Relais", | ||||
|     "Citéis", | ||||
| 	'Tram', | ||||
| 	'Corol', | ||||
| 	'Lianes', | ||||
| 	'Ligne', | ||||
| 	'Bus Relais', | ||||
| 	'Citéis', | ||||
| ) | ||||
| 
 | ||||
| 
 | ||||
| class Stop: | ||||
|     def __init__(self, id, name, latitude, longitude, city): | ||||
|         self.id = id | ||||
|         self.name = name | ||||
|         self.latitude = latitude | ||||
|         self.longitude = longitude | ||||
|         self.city = city | ||||
|         self.stopPoints = [] | ||||
| 	def __init__ (self, id, name, latitude, longitude, city): | ||||
| 		self.id = id | ||||
| 		self.name = name | ||||
| 		self.latitude = latitude | ||||
| 		self.longitude = longitude | ||||
| 		self.city = city | ||||
| 		self.stopPoints = [] | ||||
| 
 | ||||
|     def getId(self): | ||||
|         return self.id | ||||
| 	def getId (self): | ||||
| 		return self.id | ||||
| 
 | ||||
|     def getName(self): | ||||
|         return self.name | ||||
| 	def getName (self): | ||||
| 		return self.name | ||||
| 
 | ||||
|     def getLatitude(self): | ||||
|         return self.latitude | ||||
| 	def getLatitude (self): | ||||
| 		return self.latitude | ||||
| 
 | ||||
|     def getLongitude(self): | ||||
| 	def getLongitude (self): | ||||
| 
 | ||||
|         return self.longitude | ||||
| 		return self.longitude | ||||
| 
 | ||||
|     def getCity(self): | ||||
|         return self.city | ||||
| 	def getCity (self): | ||||
| 		return self.city | ||||
| 
 | ||||
|     def getStopPoints(self): | ||||
|         return self.stopPoints | ||||
| 	def getStopPoints (self): | ||||
| 		return self.stopPoints | ||||
| 
 | ||||
|     def setStopPoints(self, stopPoints): | ||||
|         self.stopPoints = stopPoints | ||||
| 	def setStopPoints (self, stopPoints): | ||||
| 		self.stopPoints = stopPoints | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return self.name + " (" + self.city + ")" + " (id: " + self.id + ")" | ||||
| 	def __repr__ (self): | ||||
| 		return self.name + ' (' + self.city + ')' + ' (id: ' + self.id + ')' | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         return self.name + " (" + self.city + ")" + " (id: " + self.id + ")" | ||||
| 	def __str__ (self): | ||||
| 		return self.name + ' (' + self.city + ')' + ' (id: ' + self.id + ')' | ||||
| 
 | ||||
| 
 | ||||
| def get_stop_by_id(id): | ||||
|     data = get_data_from_json(INFO_URL % quote(id)) | ||||
|     stop = Stop( | ||||
|         data["id"], | ||||
|         data["name"], | ||||
|         float(data["latitude"]), | ||||
|         float(data["longitude"]), | ||||
|         data["city"], | ||||
|     ) | ||||
|     stopPoints = [] | ||||
|     for i in data["stopPoints"]: | ||||
|         stopPoint = StopPoint(i["name"]) | ||||
|         routes = [] | ||||
|         stopPoint.setId(int(search("[0-9]+$", i["id"]).group())) | ||||
|         for j in i["routes"]: | ||||
|             route = Route(j["name"], j["line"]["name"]) | ||||
|             add = False | ||||
|             if route.getLineName() in LINE_TRANSLATE: | ||||
|                 line_id = LINE_TRANSLATE[route.getLineName()] | ||||
|                 add = True | ||||
|             else: | ||||
|                 try: | ||||
|                     line_id = search("[0-9]+$", route.getLineName()).group() | ||||
|                 except AttributeError: | ||||
|                     continue | ||||
|                 line_id = "%02d" % int(line_id) | ||||
|                 for i in LINE_TYPES: | ||||
|                     if route.getLineName()[0 : len(i)] == i: | ||||
|                         add = True | ||||
|                         break | ||||
|             if add: | ||||
|                 route.setId(line_id) | ||||
|                 routes.append(route) | ||||
|                 stopPoint.setRoutes(routes) | ||||
|         if stopPoint.getRoutes() != []: | ||||
|             stopPoints.append(stopPoint) | ||||
|             stop.setStopPoints(stopPoints) | ||||
|     return stop | ||||
| def get_stop_by_id (id): | ||||
| 	data = get_data_from_json (INFO_URL % quote (id)) | ||||
| 	stop = Stop ( | ||||
| 		data ['id'], | ||||
| 		data ['name'], | ||||
| 		float (data ['latitude']), | ||||
| 		float (data ['longitude']), | ||||
| 		data ['city'], | ||||
| 	) | ||||
| 	stopPoints = [] | ||||
| 	for i in data ['stopPoints']: | ||||
| 		stopPoint = StopPoint (i ['name']) | ||||
| 		routes = [] | ||||
| 		stopPoint.setId (int (search ('[0-9]+$', i ['id']).group ())) | ||||
| 		for j in i ['routes']: | ||||
| 			route = Route (j ['id'], j ['name'], j ['line'] ['name']) | ||||
| 			add = False | ||||
| 			if route.getLineName () in LINE_TRANSLATE: | ||||
| 				line_id = LINE_TRANSLATE [route.getLineName ()] | ||||
| 				add = True | ||||
| 			else: | ||||
| 				try: | ||||
| 					line_id = search ('[0-9]+$', route.getLineName ()).group () | ||||
| 				except AttributeError: | ||||
| 					continue | ||||
| 				line_id = '%02d' % int (line_id) | ||||
| 				for i in LINE_TYPES: | ||||
| 					if route.getLineName () [0:len (i)] == i: | ||||
| 						add = True | ||||
| 						break | ||||
| 			if add: | ||||
| 				route.setId (line_id) | ||||
| 				routes.append (route) | ||||
| 				stopPoint.setRoutes (routes) | ||||
| 		if stopPoint.getRoutes () != []: | ||||
| 			stopPoints.append (stopPoint) | ||||
| 			stop.setStopPoints (stopPoints) | ||||
| 	return stop | ||||
|  | @ -2,36 +2,36 @@ from utils import get_data_from_json | |||
| from urllib.parse import quote | ||||
| 
 | ||||
| 
 | ||||
| STOP_URL = "https://ws.infotbm.com/ws/1.0/get-schedule/%s" | ||||
| STOP_URL = 'https://ws.infotbm.com/ws/1.0/get-schedule/%s' | ||||
| 
 | ||||
| 
 | ||||
| class StopArea: | ||||
|     def __init__(self, id, name, city): | ||||
|         self.id = id | ||||
|         self.name = name | ||||
|         self.city = city | ||||
| 	def __init__ (self, id, name, city): | ||||
| 		self.id = id | ||||
| 		self.name = name | ||||
| 		self.city = city | ||||
| 
 | ||||
|     def getId(self): | ||||
|         return self.id | ||||
| 	def getId (self): | ||||
| 		return self.id | ||||
| 
 | ||||
|     def getName(self): | ||||
|         return self.name | ||||
| 	def getName (self): | ||||
| 		return self.name | ||||
| 
 | ||||
|     def getCity(self): | ||||
|         return self.city | ||||
| 	def getCity (self): | ||||
| 		return self.city | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return self.name + " (" + self.city + ")" + " (id: " + self.id + ")" | ||||
| 	def __repr__ (self): | ||||
| 		return self.name + ' (' + self.city + ')' + ' (id: ' + self.id + ')' | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         return self.name + " (" + self.city + ")" + " (id: " + self.id + ")" | ||||
| 	def __str__ (self): | ||||
| 		return self.name + ' (' + self.city + ')' + ' (id: ' + self.id + ')' | ||||
| 
 | ||||
| 
 | ||||
| # we on only treat stops of type "stop_area" | ||||
| def get_stop_areas_by_name(keyword): | ||||
|     data = get_data_from_json(STOP_URL % quote(keyword)) | ||||
|     stopAreas = [] | ||||
|     for s in data: | ||||
|         if s["type"] == "stop_area": | ||||
|             stopAreas.append(StopArea(s["id"], s["name"], s["city"])) | ||||
|     return stopAreas | ||||
| def get_stop_areas_by_name (keyword): | ||||
| 	data = get_data_from_json (STOP_URL % quote (keyword)) | ||||
| 	stopAreas = [] | ||||
| 	for s in data: | ||||
| 		if s ['type'] == 'stop_area': | ||||
| 			stopAreas.append (StopArea (s ['id'], s ['name'], s ['city'])) | ||||
| 	return stopAreas | ||||
|  | @ -1,26 +1,26 @@ | |||
| class StopPoint: | ||||
|     def __init__(self, name): | ||||
|         self.id = None | ||||
|         self.name = name | ||||
|         self.routes = [] | ||||
| 	def __init__ (self, name): | ||||
| 		self.id = None | ||||
| 		self.name = name | ||||
| 		self.routes = [] | ||||
| 
 | ||||
|     def getId(self): | ||||
|         return self.id | ||||
| 	def getId (self): | ||||
| 		return self.id | ||||
| 
 | ||||
|     def getName(self): | ||||
|         return self.name | ||||
| 	def getName (self): | ||||
| 		return self.name | ||||
| 
 | ||||
|     def getRoutes(self): | ||||
|         return self.routes | ||||
| 	def getRoutes (self): | ||||
| 		return self.routes | ||||
| 
 | ||||
|     def setId(self, id): | ||||
|         self.id = id | ||||
| 	def setId (self, id): | ||||
| 		self.id = id | ||||
| 
 | ||||
|     def setRoutes(self, routes): | ||||
|         self.routes = routes | ||||
| 	def setRoutes (self, routes): | ||||
| 		self.routes = routes | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return self.name | ||||
| 	def __repr__ (self): | ||||
| 		return self.name | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         return self.name | ||||
| 	def __str__ (self): | ||||
| 		return self.name | ||||
|  | @ -3,86 +3,87 @@ from time import time | |||
| from vehicle import Vehicle | ||||
| 
 | ||||
| 
 | ||||
| SCHEDULE_URL = "https://ws.infotbm.com/ws/1.0/get-realtime-pass/%d/%s" | ||||
| SCHEDULE_URL = 'https://ws.infotbm.com/ws/1.0/get-realtime-pass/%d/%s/%s' | ||||
| 
 | ||||
| 
 | ||||
| class Line: | ||||
|     """ | ||||
|     Information on the line served at a stop. | ||||
|     """ | ||||
| 	''' | ||||
| 	Information on the line served at a stop. | ||||
| 	''' | ||||
| 
 | ||||
|     def __init__(self, vehicles): | ||||
|         self.vehicles = vehicles | ||||
| 	def __init__ (self, vehicles): | ||||
| 		self.vehicles = vehicles | ||||
| 
 | ||||
|     def get_vehicles(self): | ||||
|         if self.vehicles is not None: | ||||
|             return list(range(0, len(self.vehicles))) | ||||
|         return [] | ||||
| 	def get_vehicles (self): | ||||
| 		if self.vehicles is not None: | ||||
| 			return list (range (0, len (self.vehicles))) | ||||
| 		return [] | ||||
| 
 | ||||
|     def get_vehicle(self, vehicle_id): | ||||
|         return self.vehicles[vehicle_id] | ||||
| 	def get_vehicle (self, vehicle_id): | ||||
| 		return self.vehicles [vehicle_id] | ||||
| 
 | ||||
| 
 | ||||
| class StopRoute: | ||||
|     def __init__( | ||||
|         self, | ||||
|         number, | ||||
|         line, | ||||
|         auto_update_at_creation=True, | ||||
|         auto_update=False, | ||||
|         auto_update_delay=-1, | ||||
|     ): | ||||
|         self.number = number | ||||
|         self.line = line | ||||
|         self.last_update = 0 | ||||
|         self.data = None | ||||
|         if auto_update_at_creation: | ||||
|             self.update() | ||||
| 	def __init__ ( | ||||
| 		self, | ||||
| 		number, | ||||
| 		line, | ||||
| 		line_id, | ||||
| 		auto_update_at_creation = True, | ||||
| 		auto_update = False, | ||||
| 		auto_update_delay = -1, | ||||
| 	): | ||||
| 		self.number = number | ||||
| 		self.line = line | ||||
| 		self.line_id = line_id | ||||
| 		self.last_update = 0 | ||||
| 		self.data = None | ||||
| 		if auto_update_at_creation: | ||||
| 			self.update () | ||||
| 
 | ||||
|     def update(self, auto=False): | ||||
|         """ | ||||
|         Update data. | ||||
|         """ | ||||
| 	def update (self, auto = False): | ||||
| 		''' | ||||
| 		Update data. | ||||
| 		''' | ||||
| 
 | ||||
|         data = get_data_from_json(SCHEDULE_URL % (self.number, self.line)) | ||||
|         if "destinations" in data: | ||||
|             data = data["destinations"] | ||||
|         else: | ||||
|             return | ||||
|         self.last_update = time() | ||||
|         if type(data) == dict: | ||||
|             self.data = [] | ||||
|             # let's simplify the data | ||||
|             for i in data: | ||||
|                 for j in data[i]: | ||||
|                     location = None | ||||
|                     try: | ||||
|                         location = ( | ||||
|                             float(j["vehicle_lattitude"]), | ||||
|                             float(j["vehicle_longitude"]), | ||||
|                         ) | ||||
|                     except TypeError: | ||||
|                         pass | ||||
|                     vehicle = Vehicle( | ||||
|                         j["vehicle_id"], | ||||
|                         j["destination_name"], | ||||
|                         j["realtime"] == "1", | ||||
|                         location, | ||||
|                         hms2seconds(j["waittime"]), | ||||
|                         j["waittime_text"], | ||||
|                         int(self.last_update + hms2seconds(j["waittime"])), | ||||
|                     ) | ||||
|                     self.data.append(vehicle) | ||||
|             self.data = sorted(self.data, key=lambda vehicle: vehicle.getArrival()) | ||||
|         else: | ||||
|             self.last_update = 0 | ||||
| 		data = get_data_from_json (SCHEDULE_URL % (self.number, self.line, self.line_id)) | ||||
| 		if 'destinations' in data: | ||||
| 			data = data ['destinations'] | ||||
| 		else: | ||||
| 			return | ||||
| 		self.last_update = time () | ||||
| 		if type (data) == dict: | ||||
| 			self.data = [] | ||||
| 			for i in data: | ||||
| 				for j in data [i]: | ||||
| 					location = None | ||||
| 					try: | ||||
| 						location = ( | ||||
| 							float (j ['vehicle_lattitude']), | ||||
| 							float (j ['vehicle_longitude']), | ||||
| 						) | ||||
| 					except TypeError: | ||||
| 						pass | ||||
| 					vehicle = Vehicle ( | ||||
| 						j ['vehicle_id'], | ||||
| 						j ['destination_name'], | ||||
| 						j ['realtime'] == '1', | ||||
| 						location, | ||||
| 						hms2seconds (j ['waittime']), | ||||
| 						j ['waittime_text'], | ||||
| 						int (self.last_update + hms2seconds (j ['waittime'])), | ||||
| 					) | ||||
| 					self.data.append (vehicle) | ||||
| 			self.data = sorted (self.data, key = lambda vehicle: vehicle.getArrival ()) | ||||
| 		else: | ||||
| 			self.last_update = 0 | ||||
| 
 | ||||
|     def data_age(self): | ||||
|         """ | ||||
|         Returns the age of the data. | ||||
|         """ | ||||
| 	def data_age (self): | ||||
| 		''' | ||||
| 		Returns the age of the data. | ||||
| 		''' | ||||
| 
 | ||||
|         return time() - self.last_update | ||||
| 		return time () - self.last_update | ||||
| 
 | ||||
|     def get_line(self): | ||||
|         return Line(self.data) | ||||
| 	def get_line (self): | ||||
| 		return Line (self.data) | ||||
							
								
								
									
										38
									
								
								src/utils.py
								
								
								
								
							
							
						
						
									
										38
									
								
								src/utils.py
								
								
								
								
							|  | @ -3,26 +3,26 @@ from urllib import request | |||
| from urllib.error import HTTPError | ||||
| 
 | ||||
| 
 | ||||
| def get_data_from_json(url): | ||||
|     """ | ||||
|     Gets data from json at url. | ||||
|     """ | ||||
| def get_data_from_json (url): | ||||
| 	''' | ||||
| 	Gets data from json at url. | ||||
| 	''' | ||||
| 
 | ||||
|     opener = request.build_opener() | ||||
|     try: | ||||
|         return read_json(opener.open(url).read().decode("utf8")) | ||||
|     except HTTPError: | ||||
|         return None | ||||
| 	opener = request.build_opener () | ||||
| 	try: | ||||
| 		return read_json (opener.open (url).read ().decode ('utf8')) | ||||
| 	except HTTPError: | ||||
| 		return None | ||||
| 
 | ||||
| 
 | ||||
| def hms2seconds(hhmmss): | ||||
|     """ | ||||
|     Convert H:M:S string to time in seconds. | ||||
|     """ | ||||
| def hms2seconds (hhmmss): | ||||
| 	''' | ||||
| 	Convert H:M:S string to time in seconds. | ||||
| 	''' | ||||
| 
 | ||||
|     try: | ||||
|         cut_string = hhmmss.split(":") | ||||
|         cut_time = (int(cut_string[0]), int(cut_string[1]), int(cut_string[2])) | ||||
|         return 3600 * cut_time[0] + 60 * cut_time[1] + cut_time[2] | ||||
|     except (IndexError, ValueError, TypeError): | ||||
|         return None | ||||
| 	try: | ||||
| 		cut_string = hhmmss.split (':') | ||||
| 		cut_time = (int (cut_string [0]), int (cut_string [1]), int (cut_string [2])) | ||||
| 		return 3600 * cut_time [0] + 60 * cut_time [1] + cut_time [2] | ||||
| 	except (IndexError, ValueError, TypeError): | ||||
| 		return None | ||||
							
								
								
									
										175
									
								
								src/vcub.py
								
								
								
								
							
							
						
						
									
										175
									
								
								src/vcub.py
								
								
								
								
							|  | @ -6,109 +6,108 @@ from utils import get_data_from_json | |||
| from time import time | ||||
| 
 | ||||
| 
 | ||||
| vcub_url = "https://ws.infotbm.com/ws/1.0/vcubs" | ||||
| vcub_url = 'https://ws.infotbm.com/ws/1.0/vcubs' | ||||
| 
 | ||||
| 
 | ||||
| class Vcub: | ||||
|     """ | ||||
|     Retrieves information from V³ stations. | ||||
|     """ | ||||
| 	''' | ||||
| 	Retrieves information from V³ stations. | ||||
| 	''' | ||||
| 
 | ||||
|     def __init__( | ||||
|         self, | ||||
|         autoupdate_at_creation=None, | ||||
|         autoupdate=False, | ||||
|         autoupdate_delay=-1, | ||||
|         data=None, | ||||
|     ): | ||||
|         self.last_update = 0 | ||||
|         if type(data) == dict: | ||||
|             self.data = self.update(data=data) | ||||
|         else: | ||||
|             self.data = None | ||||
|         if autoupdate_at_creation or ( | ||||
|             autoupdate_at_creation is None and self.data is None | ||||
|         ): | ||||
|             self.update() | ||||
| 	def __init__ ( | ||||
| 		self, | ||||
| 		autoupdate_at_creation = None, | ||||
| 		autoupdate = False, | ||||
| 		autoupdate_delay = -1, | ||||
| 		data = None, | ||||
| 	): | ||||
| 		self.last_update = 0 | ||||
| 		if type (data) == dict: | ||||
| 			self.data = self.update (data = data) | ||||
| 		else: | ||||
| 			self.data = None | ||||
| 		if autoupdate_at_creation or ( | ||||
| 			autoupdate_at_creation is None and self.data is None | ||||
| 		): | ||||
| 			self.update () | ||||
| 
 | ||||
|     def update(self, auto=False, data=None): | ||||
|         """ | ||||
|         Updates data. | ||||
|         """ | ||||
| 	def update (self, auto = False, data = None): | ||||
| 		''' | ||||
| 		Updates data. | ||||
| 		''' | ||||
| 
 | ||||
|         if data is None or type(data) != dict: | ||||
|             d = get_data_from_json(vcub_url) | ||||
|         else: | ||||
|             d = data | ||||
|         # the original format is awfull, so I change it a little | ||||
|         if type(d) == dict: | ||||
|             self.data = {} | ||||
|             d = d["lists"] | ||||
|             for i in d: | ||||
|                 e = { | ||||
|                     "name": i["name"], | ||||
|                     "online": i["connexionState"] == "CONNECTEE", | ||||
|                     "plus": i["typeVlsPlus"] == "VLS_PLUS", | ||||
|                     "empty": int(i["nbPlaceAvailable"]), | ||||
|                     "bikes": int(i["nbBikeAvailable"]), | ||||
|                     "ebikes": int(i["nbElectricBikeAvailable"]), | ||||
|                     "location": (float(i["latitude"]), float(i["longitude"])), | ||||
|                 } | ||||
|                 self.data[int(i["id"])] = e | ||||
|             self.last_update = time() | ||||
| 		if data is None or type (data) != dict: | ||||
| 			d = get_data_from_json (vcub_url) | ||||
| 		else: | ||||
| 			d = data | ||||
| 		if type (d) == dict: | ||||
| 			self.data = {} | ||||
| 			d = d ['lists'] | ||||
| 			for i in d: | ||||
| 				e = { | ||||
| 					'name': i ['name'], | ||||
| 					'online': i ['connexionState'] == 'CONNECTEE', | ||||
| 					'plus': i ['typeVlsPlus'] == 'VLS_PLUS', | ||||
| 					'empty': int (i ['nbPlaceAvailable']), | ||||
| 					'bikes': int (i ['nbBikeAvailable']), | ||||
| 					'ebikes': int (i ['nbElectricBikeAvailable']), | ||||
| 					'location': (float (i ['latitude']), float (i ['longitude'])), | ||||
| 				} | ||||
| 				self.data [int (i ['id'])] = e | ||||
| 			self.last_update = time () | ||||
| 
 | ||||
|     def data_age(self): | ||||
|         """ | ||||
|         Computes the data's age. | ||||
|         """ | ||||
| 	def data_age (self): | ||||
| 		''' | ||||
| 		Computes the data's age. | ||||
| 		''' | ||||
| 
 | ||||
|         return time() - self.last_update | ||||
| 		return time () - self.last_update | ||||
| 
 | ||||
|     def get_names(self): | ||||
|         """ | ||||
|         Returns all names in a dict with id as data. | ||||
|         """ | ||||
| 	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 | ||||
| 		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. | ||||
|         """ | ||||
| 	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 | ||||
| 		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. | ||||
|         """ | ||||
| 	def get_by_id (self, id): | ||||
| 		''' | ||||
| 		Returns a station by its id. | ||||
| 		''' | ||||
| 
 | ||||
|         class Station: | ||||
|             """ | ||||
|             A V³ station | ||||
|             """ | ||||
| 		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.ebikes = self.data["ebikes"] | ||||
|                 self.empty = self.data["empty"] | ||||
| 			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.ebikes = self.data ['ebikes'] | ||||
| 				self.empty = self.data ['empty'] | ||||
| 
 | ||||
|             def __int__(self): | ||||
|                 return self.id | ||||
| 			def __int__ (self): | ||||
| 				return self.id | ||||
| 
 | ||||
|         return Station(self.data[id], id) | ||||
| 		return Station (self.data [id], id) | ||||
| 
 | ||||
|     def get_all_ids(self): | ||||
|         return tuple(self.data.keys()) | ||||
| 	def get_all_ids (self): | ||||
| 		return tuple (self.data.keys ()) | ||||
|  | @ -1,32 +1,32 @@ | |||
| class Vehicle: | ||||
|     def __init__( | ||||
|         self, id, destination, realtime, location, wait_time, wait_time_text, arrival | ||||
|     ): | ||||
|         self.id = id | ||||
|         self.destination = destination | ||||
|         self.realtime = realtime | ||||
|         self.location = location | ||||
|         self.wait_time = wait_time | ||||
|         self.wait_time_text = wait_time_text | ||||
|         self.arrival = arrival | ||||
| 	def __init__ ( | ||||
| 		self, id, destination, realtime, location, wait_time, wait_time_text, arrival | ||||
| 	): | ||||
| 		self.id = id | ||||
| 		self.destination = destination | ||||
| 		self.realtime = realtime | ||||
| 		self.location = location | ||||
| 		self.wait_time = wait_time | ||||
| 		self.wait_time_text = wait_time_text | ||||
| 		self.arrival = arrival | ||||
| 
 | ||||
|     def getId(self): | ||||
|         return self.id | ||||
| 	def getId (self): | ||||
| 		return self.id | ||||
| 
 | ||||
|     def getDestination(self): | ||||
|         return self.destination | ||||
| 	def getDestination (self): | ||||
| 		return self.destination | ||||
| 
 | ||||
|     def getRealtime(self): | ||||
|         return self.realtime | ||||
| 	def getRealtime (self): | ||||
| 		return self.realtime | ||||
| 
 | ||||
|     def getLocation(self): | ||||
|         return self.location | ||||
| 	def getLocation (self): | ||||
| 		return self.location | ||||
| 
 | ||||
|     def getWaitTime(self): | ||||
|         return self.wait_time | ||||
| 	def getWaitTime (self): | ||||
| 		return self.wait_time | ||||
| 
 | ||||
|     def getWaitTimeText(self): | ||||
|         return self.wait_time_text | ||||
| 	def getWaitTimeText (self): | ||||
| 		return self.wait_time_text | ||||
| 
 | ||||
|     def getArrival(self): | ||||
|         return self.arrival | ||||
| 	def getArrival (self): | ||||
| 		return self.arrival | ||||
		Loading…
	
		Reference in New Issue