-
Notifications
You must be signed in to change notification settings - Fork 7
/
find3d.py
executable file
·99 lines (78 loc) · 2.77 KB
/
find3d.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Martin Herweg [email protected]
#
# 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.
#
# api documentation:
# http://gateway.x-plane.com/api#get-scenery
# download the airports file (10MB) before running this script:
# wget -N https://gateway.x-plane.com/apiv1/airports
# this tool tries to download all "3D" airport sceneries from gateway.x-plane.com
# the ICAO.txt for each airport is saved into the current folder.
# you can use those files as input for xplane2fg or dsf2stg
from io import StringIO
import os, sys, io
import json, base64, getopt
import http.client
import zipfile
inputfilename="airports"
htconn = http.client.HTTPSConnection("gateway.x-plane.com")
def download_stgtxt(sid,icao):
htconn.request("GET", "/apiv1/scenery/%s" % str(sid))
r1 = htconn.getresponse()
r2 = r1.read()
try:
result = json.loads(r2)
except:
print ("Cannot download",icao, "recommended Scenery ID:", sid, "- no json?")
return
zip_base64 = result["scenery"]["masterZipBlob"]
zip_blob = base64.b64decode(zip_base64)
zip_bytearray = io.BytesIO(zip_blob)
zip_fhandle = zipfile.ZipFile(zip_bytearray)
icao=icao.upper()
print("reading", icao, sid)
# myZip = zipfile.ZipFile("%s.zip" % icao, "r")
#datstring = zip_fhandle.read("%s.dat" % icao)
zip_fhandle.extract(icao + ".dat")
try:
txtstring = zip_fhandle.read("%s.txt" % icao)
except:
print ("(2D)")
else:
print (" 3D :-)")
zip_fhandle.extract(icao + ".txt")
# main
try:
infile = open(inputfilename, 'r')
except:
print ("input file: " + inputfilename + " not found")
sys.exit()
r2 = infile.read()
result = json.loads(r2)
airports = result["airports"]
for a in airports:
if a['SceneryType'] == 0:
pass
else:
icao = a['AirportCode']
sid = a['RecommendedSceneryId']
# only download if we do not have it
if os.path.isfile(icao + ".txt"):
print ("not downloading" + icao)
else:
download_stgtxt(sid,icao)