-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgateway_pull.py
executable file
·123 lines (102 loc) · 3.55 KB
/
gateway_pull.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Martin Herweg m.herweg@gmx.de
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#import socket
import json, base64, os, getopt, sys
import http.client
import zipfile
helptext = 'gateway_pull.py -i <ICAO> \ngateway_pull.py -s <sceneryId>'
def main(argv):
icao="XXXX"
sid = ""
sids=[]
author =""
try:
opts, args = getopt.getopt(argv,"hi:s:")
except getopt.GetoptError:
print (helptext)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print (helptext)
sys.exit()
elif opt == "-i":
icao = arg
elif opt == "-s":
sid = arg
conn = http.client.HTTPSConnection('gateway.x-plane.com')
if sid == "":
conn.request("GET", "/apiv1/airport/" + icao)
r1 = conn.getresponse()
#print (r1)
r2 = r1.read()
#print (r2)
result = json.loads(r2)
sid=result["airport"]["recommendedSceneryId"]
sceneries = result["airport"]["scenery"]
for s in sceneries:
for k2, v2 in s.items():
if k2 in ["dateDeclined","dateAccepted","DateAccepted","DateApproved","DateDeclined","userId","type"]:
pass
else:
print (k2, v2)
if s['Status']=="Approved":
sids.append(s['sceneryId'])
if s["sceneryId"] == sid:
author = s["userName"]
print ("-----------------")
print ("approved scenery ids for " + icao + ":" + str(sids))
#print ("highest approved id:", max(sids))
print ("recommended SceneryId:" + str(sid) + "by author: " + author)
conn.request("GET", "/apiv1/scenery/" + str(sid))
r1 = conn.getresponse()
r2 = r1.read()
result = json.loads(r2)
#print (result)
encoded_zip = result["scenery"]["masterZipBlob"]
blob = base64.b64decode(encoded_zip)
#print ("writing " + icao + ".zip"_
file = open(icao + ".zip","wb")
file.write(blob)
file.close()
#print ("reading " + icao + ".zip")
myZip = zipfile.ZipFile(icao + ".zip","r")
print ("writing " + icao + ".dat")
myZip.extract(icao + ".dat")
try:
myZip.extract(icao + ".txt")
except:
print ("(2D)")
else:
print ("writing " + icao + ".txt")
print (" 3D :-)")
#print ("deleting " + icao + ".zip")
os.remove(icao + ".zip")
#newFileByteArray = bytearray(zipfile)
#newFile.write(blob)
#for zipContentFile in myZip.namelist():
# data = myZip.read(zipContentFile)
# #print (zipContentFile)
# file = open(zipContentFile, 'w+b')
# file.write(data)
# file.close()
#myZip = zipfile.ZipFile(icao + "_Scenery_Pack.zip","r")
#myZip.extract(icao + "_Scenery_Pack/Earth nav data/apt.dat")
if __name__ == "__main__":
main(sys.argv[1:])