This repository was archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrmv_api.py
70 lines (52 loc) · 2.55 KB
/
rmv_api.py
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
#!/usr/bin/env python3
import urllib.request
import json
from typing import *
from dateutil import parser
from datetime import datetime
from time import sleep
API_TOKEN: str = "TODO" # like "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (hex)
ORIGIN_ID: str = "TODO" # like: "0000000" (dec)
DESTINATION_ID: str = "TODO" # like: "0000000" (dec)
API_REQUEST_TRIP: str = f"https://www.rmv.de/hapi/trip?originId={ORIGIN_ID}&destId={DESTINATION_ID}&accessId={API_TOKEN}&format=json"
API_REQUEST_DEP: str = f"https://www.rmv.de/hapi/departureBoard?id={ORIGIN_ID}&direction={DESTINATION_ID}&accessId={API_TOKEN}&format=json" #&time=23%3A59
def get_json_data(source_url: str):
with urllib.request.urlopen(source_url) as url:
data: str = url.read().decode()
return json.loads(data)
def filter_data_trip(data, exclude_destinations: Set[str] = []) -> Iterable[Tuple[float, str, str]]:
for element in data['Trip']:
option = element['LegList']['Leg'][0]
try:
time = time_converter(option['Origin']['date'], option['Origin']['time'])
except ValueError:
# ignore negative time deltas (departures in the past are irrelevant)
continue
line_name = option['Product']['line']
destination = option['direction']
if destination not in exclude_destinations:
yield time, line_name, destination
def filter_data_dep(data, exclude_destinations: Set[str] = []) -> Iterable[Tuple[float, str, str]]:
for element in data['Departure']:
try:
time = time_converter(element['date'], element['time'])
except ValueError:
# ignore negative time deltas (departures in the past are irrelevant)
continue
line_name = element['Product']['line']
destination = element['direction'].split(None, 1)[1]
if destination not in exclude_destinations:
yield time, line_name, destination
def time_converter(rmv_date: str, rmv_time: str) -> float:
rmv_date_time = f"{rmv_date} {rmv_time}"
time_offset = parser.parse(rmv_date_time) - datetime.now()
if time_offset.days < 0:
raise ValueError('Given time is in the past')
else:
return float(time_offset.seconds)
def print_to_console(data: List[Tuple[float, str, str]], num_entries: int = 5) -> None:
print("---------------")
for time, line, destination in data[:num_entries]:
minutes = time/60
print(f"{minutes:3.0f}\t\t{line}\t\t{destination}")
print("---------------")