65 lines
1.9 KiB
Python
Executable File
65 lines
1.9 KiB
Python
Executable File
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from subprocess import check_output
|
|
from requests import get
|
|
import json
|
|
import sys
|
|
from datetime import datetime
|
|
import pytz
|
|
|
|
timezone = pytz.timezone('Europe/Berlin')
|
|
statusData = None
|
|
tripData = None
|
|
|
|
def get_api_db():
|
|
global statusData
|
|
global tripData
|
|
statusData_response = get("https://iceportal.de/api1/rs/status", timeout=5)
|
|
statusData = statusData_response.json()
|
|
tripData_response = get("https://iceportal.de/api1/rs/tripInfo/trip", timeout=5)
|
|
tripData = tripData_response.json()
|
|
|
|
def get_ssid():
|
|
|
|
scanoutput = check_output(["/usr/sbin/iwlist", "wlp2s0", "scan"])
|
|
|
|
for line in scanoutput.split():
|
|
line = line.decode("utf-8")
|
|
if line[:5] == "ESSID":
|
|
ssid = line.split('"')[1]
|
|
return ssid
|
|
|
|
# Get Speed from ICE-Portal
|
|
def get_speed_db():
|
|
speed= str(statusData["speed"])
|
|
return speed
|
|
|
|
def get_next_db():
|
|
next_stop=tripData["trip"]["stopInfo"]["actualNext"]
|
|
for i in tripData['trip']['stops']:
|
|
if i['station']['evaNr'] == next_stop:
|
|
nextStationName = i['station']['name']
|
|
arrivalDelay = i['timetable']['arrivalDelay']
|
|
utime=i['timetable']['actualArrivalTime']/1000
|
|
arrivalTime = datetime.fromtimestamp(utime, tz=timezone).strftime('%H:%M')
|
|
break
|
|
tripinfo = nextStationName+" "+ arrivalTime+" " + arrivalDelay
|
|
return tripinfo
|
|
|
|
def get_trainInfo_db():
|
|
trainType = tripData["trip"]["trainType"]
|
|
trainNumber = tripData["trip"]["vzn"]
|
|
finalStationName = tripData["trip"]["stopInfo"]["finalStationName"]
|
|
return trainType+" "+trainNumber+" "+ finalStationName
|
|
|
|
def main():
|
|
if get_ssid() == "WIFIonICE":
|
|
get_api_db()
|
|
print(get_trainInfo_db()+" | "+"SPEED: "+get_speed_db()+" km/h | "+"NEXT: "+get_next_db())
|
|
return 0
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main()) # next section explains the use of sys.exit
|
|
|