-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsbb-cli.py
executable file
·62 lines (48 loc) · 2.45 KB
/
sbb-cli.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import urllib.parse
import urllib.request
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--from', help='Departure station', required=True)
parser.add_argument('-t', '--to', help='Arrival station', required=True)
parser.add_argument('-c', '--time', help='Departure time', required=False)
parser.add_argument('-d', '--detail', help='Verbose output', required=False)
args = vars(parser.parse_args())
if args['detail']:
url = 'http://transport.opendata.ch/v1/connections?from=' + str(args['from'].replace(' ','%20').encode('utf-8')) \
+ '&to=' + str(args['to'].replace(' ','%20').encode('utf-8')) + '&limit=6'
else:
url = 'http://transport.opendata.ch/v1/connections?from=' + str(args['from'].replace(' ','%20').encode('utf-8')) \
+ '&to=' + str(args['to'].replace(' ','%20').encode('utf-8')) \
+ '&fields[]=connections/from&fields[]=connections/to&fields[]=connections/duration&limit=6'
if args['time']:
url = url + '&time=' + urllib.parse.quote_plus(str(args['time'].replace(' ','%20')))
try: response = urllib.request.urlopen(url)
except urllib.error.URLError as e:
print("Network error! " + str(e))
else:
data = response.read()# a `bytes` object
text = data.decode('utf-8')
data = json.loads(text)
if data['connections']:
index = 0
for i in data['connections']:
index = index + 1
print("[" + str(index) + "] From: " + i['from']['station']['name']
+ " (" + (i['from']['platform'] or "-") + ")" + " At: " + i['from']['departure'][11:16]
+ " To: " + i['to']['station']['name'] + " At: " + i['to']['arrival'][11:16] + " Duration: " + i['duration'][4:12])
if args['detail']:
if data['connections']:
print("\nConnection [" + str(int(args['detail'])) + "]:")
for i in data['connections'][int(args['detail'])-1]['sections']:
if i['journey']:
print("Station: " + i['departure']['station']['name'] + " At: " + i['departure']['departure'][11:16]
+ " Platform: (" + (i['departure']['platform'] or "-") + ") \"" + str(i['journey']['name'])
+ "\" Heading to: " + i['journey']['to'] )
#i['arrival']
else:
print("Details not found!")
else:
print("Not found!")