-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetable.py
54 lines (45 loc) · 1.6 KB
/
timetable.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
import urllib.parse
import urllib.request
import json
def url_builder(location,transport):
app_key = 'bd5f386fb6fbac9344fc1c4e41f48002'
app_id = 'd1a1cdb4'
api = 'http://transportapi.com/v3/uk/places.json?query='
full_url = api + str(location)+'&type=' + str(transport)+'&app_id=' + app_id + '&app_key='+ app_key
return full_url
# example URL: http://transportapi.com/v3/uk/places.json?query=euston&type=train_station&app_id=d1a1cdb4&app_key=bd5f386fb6fbac9344fc1c4e41f48002
def data_fetch(full_url):
url = urllib.request.urlopen(full_url)
output = url.read().decode('utf-8')
raw_api_dict = json.loads(output)
url.close()
return raw_api_dict
def data_organiser(raw_data):
main = raw_data.get('main')
sys = raw_data.get('sys')
data = dict(
type=raw_data.get('type'),
name=sys.get('name'),
latitude=main.get('latitude'),
longitude=main.get('longitude'),
accuracy=main.get('accuracy'),
source=main.get('source'),
request_time=main.get('request_time'),
station_code=main.get('station_code')
)
def data_output(data):
e = '''-----------------------------------
station name: {name} {type}:
station code: {station_code}
Latitude:{latitude}
Longitude:{longitude}
accuracy: {accuracy}
Source: {source}
Requested {request_time}
-----------------------------------'''
print(e.format(**data))
if __name__ == '__main__':
try:
data_output(data_organiser(data_fetch(url_builder(2172797))))
except IOError:
print("Sorry, I don't know what you mean")