44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
|
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()
|