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
|
# Copyright (C) 2022 Yuchen Pei.
#
# ptv.py is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# ptv.py is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public
# License along with ptv.py. If not, see
# <https://www.gnu.org/licenses/>.
from hashlib import sha1
from urllib import request, error, parse
import hmac
import json
import itertools
import os
_ROUTE_TYPE = ["Train", "Tram", "Bus", "Vline", "Night Bus"]
def get_url(req):
key = str.encode(os.environ.get('PTVKEY'))
dev_id = os.environ.get('PTVID')
req = parse.quote(req) + ('&' if ('?' in req) else '?')
raw = req + f'devid={dev_id}'
hashed = hmac.new(key, raw.encode('utf-8'), sha1)
signature = hashed.hexdigest().upper()
return f'http://timetableapi.ptv.vic.gov.au{raw}&signature={signature}'
def get_json(url):
try:
with request.urlopen(url) as opened:
document = opened.read().decode()
return json.loads(document)
except error.HTTPError:
print("http error!")
def get_stops(search_result):
return [{
'routes': stop['routes'],
'stop_name': stop['stop_name'],
'stop_id': stop['stop_id']
} for stop in search_result['stops']]
def get_stop_and_routes(search_result):
return itertools.chain.from_iterable([[(stop, route)
for route in stop['routes']]
for stop in search_result['stops']])
def search(keyword):
return get_json(get_url(f'/v3/search/{keyword}'))
def get_departures(route_type, stop_id, route_id):
return get_json(
get_url(f'/v3/departures/route_type/{route_type}/stop/{stop_id}'
f'/route/{route_id}'))
def get_departures_from_stop_and_route(stop, route):
return get_departures(route['route_type'], stop['stop_id'],
route['route_id'])
def get_directions(route_id):
return get_json(get_url(f'/v3/directions/route/{route_id}'))
def get_direction_names(route_id):
return {
dir['direction_id']: dir['direction_name']
for dir in get_directions(route_id)['directions']
}
def get_route_type(idx):
return _ROUTE_TYPE[idx]
|