-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-zrh.py
141 lines (112 loc) · 5.53 KB
/
get-zrh.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 27 10:33:29 2019
@file get-zrh.py
@brief download arrivals and departures from ZRH (Airport Zurich)
@author Simon Burkhardt - simonmartin.ch - github.com/mnemocron
@date 2019-06
@description
@bug It seems like the last few flights appear twice in all the files.
The problem is most likely at the determination of lastFlightFetched = True
"""
"""
JSON format kept from the old ZRH website's api
{
"timetable":[{
"airportinformation": {
"airport_city": "DUBLIN"
},
"flightcode": "EI349",
"masterflight": {
"registration": "EIDEN"
},
"scheduled": "20:05",
"expected" : "20:20"
},{
[...]
}
}
"""
#%%
from ZRHGrabber import ZRHGrabber
import json
import os
import optparse
parser = optparse.OptionParser('get-zrh')
parser.add_option('--today', dest='today', action='store_true', default=False, help='only fetch flights for today')
parser.add_option('--tomorroy', dest='tomorrow', action='store_true', default=False, help='only fetch flights for tomorrow')
parser.add_option('--standard', dest='std', action='store_true', default=False, help='only fetch standard table')
parser.add_option('--spotter', dest='spt', action='store_true', default=False, help='only fetch spotter table')
parser.add_option('--arrivals', dest='arr', action='store_true', default=False, help='only fetch arrivals')
parser.add_option('--departures', dest='dep', action='store_true', default=False, help='only fetch departures')
parser.add_option('-d', '--out-dir', dest='out_dir', type='string', default='./timetables/', help='output directory for json files')
(opts, args) = parser.parse_args()
# if none is selected, default to all
if(opts.today == False and opts.tomorrow == False):
opts.today = True
opts.tomorrow = True
if(opts.std == False and opts.spt == False):
opts.std = True
opts.spt = True
if(opts.arr == False and opts.dep == False):
opts.arr = True
opts.dep = True
# Writes a dict in json format to a file
def dump_to_json_file(data, filename):
with open(filename, 'w+', encoding='utf-8') as fh:
json.dump(data, fh, indent=4, sort_keys=True)
def main():
try:
if(os.path.exists(opts.out_dir)):
outdir = opts.out_dir
else:
raise FileNotFoundError('Output directory not found')
if(outdir[-1] not in ['/']):
outdir = outdir + '/'
zrh = ZRHGrabber()
if(opts.today):
if(opts.std):
if(opts.arr):
print('[+] downloading (standard) (arrivals) (today) ...')
arrival_standard = zrh.fetch('Arrival', spotter=False, tomorrow=False)
dump_to_json_file(arrival_standard, outdir + 'timetable.arrival.standard.json')
if(opts.dep):
print('[+] downloading (standard) (depatures) (today) ...')
departures_standard = zrh.fetch('Departure', spotter=False, tomorrow=False)
dump_to_json_file(departures_standard, outdir + 'timetable.departure.standard.json')
if(opts.spt):
if(opts.arr):
print('[+] downloading (spotter) (arrivals) (today) ...')
arrival_spotter = zrh.fetch('Arrival', spotter=True, tomorrow=False)
dump_to_json_file(arrival_spotter, outdir + 'timetable.arrival.spotter.json')
if(opts.dep):
print('[+] downloading (spotter) (depatures) (today) ...')
departures_spotter = zrh.fetch('Departure', spotter=True, tomorrow=False)
dump_to_json_file(departures_spotter, outdir + 'timetable.departure.spotter.json')
if(opts.tomorrow):
if(opts.std):
if(opts.arr):
print('[+] downloading (standard) (arrivals) (tomorrow) ...')
arrival_standard_tomorrow = zrh.fetch('Arrival', spotter=False, tomorrow=True)
dump_to_json_file(arrival_standard_tomorrow, outdir + 'timetable.arrival.tom.standard.json')
if(opts.dep):
print('[+] downloading (standard) (departures) (tomorrow) ...')
departures_standard_tomorrow = zrh.fetch('Departure', spotter=False, tomorrow=True)
dump_to_json_file(departures_standard_tomorrow, outdir + 'timetable.departure.tom.standard.json')
if(opts.spt):
if(opts.arr):
print('[+] downloading (spotter) (arrivals) (tomorrow) ...')
arrival_spotter_tomorrow = zrh.fetch('Arrival', spotter=True, tomorrow=True)
dump_to_json_file(arrival_spotter_tomorrow, outdir + 'timetable.arrival.tom.spotter.json')
if(opts.dep):
print('[+] downloading (spotter) (departures) (tomorrow) ...')
departures_spotter_tomorrow = zrh.fetch('Departure', spotter=True, tomorrow=True)
dump_to_json_file(departures_spotter_tomorrow, outdir + 'timetable.departure.tom.spotter.json')
print('[+] fetching done!')
# enables abortion of the program through CTRL + C
except KeyboardInterrupt:
print('')
exit(0)
if __name__ == '__main__':
main()