inflight-status - inflight-status - Inflight Internet Stauts Helper Scripts
(HTM) git clone git://r-36.net/inflight-status
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
inflight-status (2827B)
---
1 #!/usr/bin/env python
2 # coding=utf-8
3 #
4 # Copy me if you can.
5 # by 20h
6 #
7
8 import os
9 import sys
10 import getopt
11 from lxml import etree
12 import io
13 import requests
14 from terminaltables import SingleTable as Table
15
16 def usage(app):
17 app = os.path.basename(app)
18 print("usage: %s [-hpt] [-u uri]" % (app), file=sys.stderr)
19 sys.exit(1)
20
21 def main(args):
22 try:
23 opts, largs = getopt.getopt(args[1:], "hpu:t")
24 except getopt.GetoptError as err:
25 print(str(err))
26 usage(args[0])
27
28 flightdatauris = {
29 "panasonic": "http://services.inflightpanasonic.aero/inflight/services/flightdata/v1/flightdata",
30 "panasonic": "http://services.inflightpanasonic.aero/inflight/services/flightdata/v2/flightdata",
31 "airfrance": "https://connect.airfrance.com/ach/api/flightdata"
32 }
33
34 useproxy = False
35 dotable = True
36 nnfd = {}
37 for o, a in opts:
38 if o == "-h":
39 usage(args[0])
40 elif o == "-p":
41 useproxy = True
42 elif o == "-t":
43 dotable = False
44 elif o == "-u":
45 flightdatauri = a
46 else:
47 assert False, "unhandled option"
48
49 for var in ("http_proxy", "HTTP_PROXY", "https_proxy", \
50 "HTTPS_PROXY"):
51 if useproxy == False and var in os.environ:
52 os.environ[var] = ""
53
54 statustype = None
55 for k in flightdatauris.keys():
56 print("Trying %s flight status ... " % (k))
57 try:
58 fd = requests.get(flightdatauris[k])
59 statustype = k
60 except:
61 continue
62 if statustype == None:
63 return 1
64
65 if statustype == "panasonic":
66 # TODO: Decoding of v2 data?
67 flightdata = fd.json()
68 tablehdr = "flight %s from %s to %s" % (
69 flightdata["td_id_fltdata_flight_number"],
70 flightdata["td_id_fltdata_departure_baggage_id"],
71 flightdata["td_id_fltdata_destination_baggage_id"])
72
73 nfd = flightdata
74 nnfd = {}
75 for k in nfd.keys():
76 if "_fltdata_" in k:
77 keyname = k.split("_fltdata_")[1]
78 elif "disclaimer" in k:
79 continue
80 else:
81 keyname = k[6:]
82 nnfd[keyname] = nfd[k]
83 del nfd
84
85 elif statustype == "airfrance":
86 flightdata = fd.json()
87 tablehdr = "flight %s from %s to %s" % (
88 flightdata["flight"]["number"],
89 flightdata["flight"]["originIATA"],
90 flightdata["flight"]["destinationIATA"])
91
92 def lineardict(d):
93 rarr = {}
94 for k in d.keys():
95 if isinstance(d[k], dict):
96 srarr = lineardict(d[k])
97 for sk in srarr.keys():
98 rarr["%s_%s" % (k, \
99 sk)] = srarr[sk]
100 else:
101 rarr[k] = d[k]
102 return rarr
103
104 nnfd = lineardict(flightdata)
105
106 if nnfd != None:
107 nnfdkeys = list(nnfd.keys())
108 nnfdkeys.sort()
109 if dotable == True:
110 print(tablehdr)
111 tabledata = [["key", "value"]]
112 for k in nnfdkeys:
113 tabledata.append([k, nnfd[k]])
114
115 table = Table(tabledata, tablehdr)
116 table.outer_border = False
117 print(table.table)
118 else:
119 print(tablehdr)
120 for k in nnfdkeys:
121 print("%s: %s" % (k, nnfd[k]))
122
123 return 0
124
125 if __name__ == "__main__":
126 sys.exit(main(sys.argv))
127