-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdvs.py
55 lines (42 loc) · 1.39 KB
/
dvs.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
import requests
import urllib
import json
import datetime
class TimeError(Exception):
pass
def validate_date(date_in):
try:
datetime.datetime.strptime(date_in, '%Y-%m-%d')
return True
except ValueError:
return False
def station(addr, station_code):
headers = {
"User-Agent": "Trein.app website backend/1.0"
}
station_code_esc = urllib.quote(station_code, safe='')
r = requests.get(addr + "/v2/station/" + station_code_esc, headers=headers)
response = json.loads(r.text)
if response['result'] != "OK":
return False
return response['vertrektijden']
def train(addr, day, service_number, station = False):
headers = {
"User-Agent": "Trein.app website backend/1.0"
}
if validate_date(day) is False:
raise TimeError("Date input is incorrect")
day_esc = urllib.quote(day, safe='')
service_number_esc = urllib.quote(service_number, safe='')
if station is False:
requestaddr = addr + "/v2/trein/" + service_number_esc + "/" + day_esc
else:
requestaddr = addr + "/v2/trein/" + service_number_esc + "/" + day_esc + "/" + station
r = requests.get(requestaddr, headers=headers)
try:
response = json.loads(r.text)
except ValueError:
return False
if response['result'] != "OK":
return False
return response['trein']