Initial commit: demo scripts for realtime and timetable API
This commit is contained in:
commit
3722b951c1
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
requests
|
||||||
|
gtfs-realtime-bindings
|
9
scripts/realtime.py
Executable file
9
scripts/realtime.py
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
from google.transit import gtfs_realtime_pb2
|
||||||
|
import requests
|
||||||
|
|
||||||
|
feed = gtfs_realtime_pb2.FeedMessage()
|
||||||
|
response = requests.get('https://www.ztm.poznan.pl/pl/dla-deweloperow/getGtfsRtFile?file=trip_updates.pb')
|
||||||
|
feed.ParseFromString(response.content)
|
||||||
|
for entity in feed.entity:
|
||||||
|
if entity.HasField('trip_update'):
|
||||||
|
print(entity.trip_update)
|
43
scripts/timetable.py
Normal file
43
scripts/timetable.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
def get_tram_departures():
|
||||||
|
url = "https://www.poznan.pl/mim/komunikacja/service.html"
|
||||||
|
|
||||||
|
params = {
|
||||||
|
"stop_id": "POKA42"
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.get(url, params=params)
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
for route in data.get('routes', []):
|
||||||
|
if route['name'] == '16':
|
||||||
|
for variant in route.get('variants', []):
|
||||||
|
if variant['headsign'] == 'Os. Sobieskiego':
|
||||||
|
for service in variant.get('services', []):
|
||||||
|
# Get first 3 departures
|
||||||
|
departures = service.get('departures', [])[0:3]
|
||||||
|
return departures
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
print(f"Error making request: {e}")
|
||||||
|
return []
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print(f"Error parsing JSON: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
def main():
|
||||||
|
departures = get_tram_departures()
|
||||||
|
if departures:
|
||||||
|
print("Next 3 departures for tram 16 to Os. Sobieskiego:")
|
||||||
|
for departure in departures:
|
||||||
|
print(departure)
|
||||||
|
else:
|
||||||
|
print("No departures found or error occurred")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user