Repository navigation
Expand file tree
/
Copy pathvvo_data.py
More file actions
105 lines (87 loc) · 3.03 KB
/
Copy pathvvo_data.py
File metadata and controls
105 lines (87 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import requests
from datetime import datetime, timezone, timedelta
import re
import pytz
TIMEZONE = 'Europe/Berlin'
class VVOData():
def __init__(self, stopid : str):
self.timezone = pytz.timezone('Europe/Berlin')
self.stopid = stopid
self.data = {}
def retrieve_stop_data(self):
url = 'https://webapi.vvo-online.de/dm'
attributes = {
"stopid": self.stopid,
"limit": 10,
"mot": [
"Tram",
"CityBus",
"IntercityBus",
"SuburbanRailway",
"Train",
"Cableway",
"Ferry",
"HailedSharedTaxi"
]
}
try:
response = requests.post(url,json=attributes,timeout=5)
response.raise_for_status()
self.data = response.json()
except requests.exceptions.ConnectionError:
print("No internet connection.")
except requests.exceptions.Timeout:
print("Request timed out.")
except requests.exceptions.HTTPError as e:
print(f"HTTP error: {e}")
except requests.exceptions.RequestException as e:
# catches all other requests-related errors
print(f"Request failed: {e}")
def get_data_value(self, i : int, key : str) -> str:
try:
return self.data['Departures'][i][key]
except:
return 'N/A'
def get_data_entry(self, i: int) -> tuple():
try:
line = self.data['Departures'][i]['LineName']
dir = self.data['Departures'][i]['Direction']
real_time = self.data['Departures'][i]['RealTime']
scheduled_time = self.data['Departures'][i]['ScheduledTime']
state = self.data['Departures'][i]['State']
return(line, dir, real_time, scheduled_time, state)
except:
return ('line N/A', 'dir N/A', 'real_time N/A', 'scheduled_time N/A','state N/A')
def get_data(self) -> dict:
return self.data
def convert_utc_to_timezone(date_string):
try:
match = re.match(r'/Date\((\d+)[+-]\d{4}\)/', date_string)
timestamp_ms = int(match.group(1))
utc_time = datetime.fromtimestamp(timestamp_ms / 1000.0, tz=timezone.utc)
time = utc_time.astimezone(pytz.timezone(TIMEZONE))
return time
except:
return datetime.now()
def format_datetime(time) -> str:
hours = time.hour
minutes = time.minute
return f"{hours:02d}:{minutes:02d}"
def get_time_delta(time) -> str:
tz = pytz.timezone(TIMEZONE)
if time.tzinfo is None:
time = tz.localize(time)
now = datetime.now(tz)
delta = time - now
minutes = int(delta.total_seconds() / 60)
if minutes < 0:
minutes = 0
return f"in {minutes} Min"
if __name__ == "__main__":
vvo = VVOData('33000742')
vvo.retrieve_stop_data()
print(vvo.get_data())
data = vvo.get_data_entry(0)
time = convert_utc_to_timezone(data[3])
print(get_time_delta(time))
#print(time_b, time_a, get_time_delta(time_a, time_b))