2016-05-04 21:08:22 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Torsten Dreyer
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# terrasync.py - synchronize terrascenery data to your local disk
|
2016-05-11 12:57:38 +00:00
|
|
|
# needs dnspython (pip install dnspython)
|
2016-05-04 21:08:22 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
import urllib, os, hashlib
|
|
|
|
from urllib.parse import urlparse
|
2016-12-28 02:46:59 +00:00
|
|
|
from http.client import HTTPConnection, _CS_IDLE, HTTPException
|
2016-05-11 20:59:59 +00:00
|
|
|
from os import listdir
|
2017-09-01 08:33:35 +00:00
|
|
|
from os.path import isfile, isdir, join
|
2016-12-28 03:17:46 +00:00
|
|
|
import re
|
2016-12-28 14:32:55 +00:00
|
|
|
import argparse
|
2017-09-01 08:33:35 +00:00
|
|
|
import shutil
|
2016-05-04 21:08:22 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
#################################################################################################################################
|
|
|
|
class HTTPGetCallback:
|
|
|
|
def __init__(self, src, callback):
|
|
|
|
self.callback = callback
|
|
|
|
self.src = src
|
|
|
|
self.result = None
|
|
|
|
|
|
|
|
class HTTPGetter:
|
|
|
|
def __init__(self, baseUrl, maxPending=10):
|
|
|
|
self.baseUrl = baseUrl
|
|
|
|
self.parsedBaseUrl = urlparse(baseUrl)
|
|
|
|
self.maxPending = maxPending
|
|
|
|
self.requests = []
|
|
|
|
self.pendingRequests = []
|
2016-06-06 10:17:12 +00:00
|
|
|
self.httpConnection = HTTPConnection(self.parsedBaseUrl.netloc)
|
2016-05-18 13:14:59 +00:00
|
|
|
self.httpRequestHeaders = headers = {'Host':self.parsedBaseUrl.netloc,'Content-Length':0,'Connection':'Keep-Alive','User-Agent':'FlightGear terrasync.py'}
|
2016-05-18 10:51:29 +00:00
|
|
|
|
2016-05-25 14:19:36 +00:00
|
|
|
def doGet(self, httpGetCallback):
|
2016-05-18 10:51:29 +00:00
|
|
|
conn = self.httpConnection
|
|
|
|
request = httpGetCallback
|
2016-05-25 14:19:36 +00:00
|
|
|
self.httpConnection.request("GET", self.parsedBaseUrl.path + request.src, None, self.httpRequestHeaders)
|
|
|
|
httpGetCallback.result = self.httpConnection.getresponse()
|
|
|
|
httpGetCallback.callback()
|
|
|
|
|
|
|
|
def get(self, httpGetCallback):
|
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
try:
|
2016-05-25 14:19:36 +00:00
|
|
|
self.doGet(httpGetCallback)
|
2016-12-28 02:46:59 +00:00
|
|
|
except HTTPException:
|
2016-05-18 10:51:29 +00:00
|
|
|
# try to reconnect once
|
|
|
|
#print("reconnect")
|
2016-05-25 14:19:36 +00:00
|
|
|
self.httpConnection.close()
|
|
|
|
self.httpConnection.connect()
|
|
|
|
self.doGet(httpGetCallback)
|
2016-05-18 10:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
#################################################################################################################################
|
|
|
|
class DirIndex:
|
|
|
|
|
|
|
|
def __init__(self, dirIndexFile):
|
|
|
|
self.d = []
|
|
|
|
self.f = []
|
|
|
|
self.version = 0
|
|
|
|
self.path = ""
|
|
|
|
|
|
|
|
with open(dirIndexFile) as f:
|
|
|
|
self.readFrom(f)
|
|
|
|
|
|
|
|
def readFrom(self, readable):
|
|
|
|
for line in readable:
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('#'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
tokens = line.split(':')
|
|
|
|
if len(tokens) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] == "version":
|
|
|
|
self.version = int(tokens[1])
|
|
|
|
|
|
|
|
elif tokens[0] == "path":
|
|
|
|
self.path = tokens[1]
|
|
|
|
|
|
|
|
elif tokens[0] == "d":
|
|
|
|
self.d.append({ 'name': tokens[1], 'hash': tokens[2] })
|
|
|
|
|
|
|
|
elif tokens[0] == "f":
|
|
|
|
self.f.append({ 'name': tokens[1], 'hash': tokens[2], 'size': tokens[3] })
|
|
|
|
|
|
|
|
def getVersion(self):
|
|
|
|
return self.version
|
|
|
|
|
|
|
|
def getPath(self):
|
|
|
|
return self.path
|
|
|
|
|
|
|
|
def getDirectories(self):
|
|
|
|
return self.d
|
|
|
|
|
|
|
|
def getFiles(self):
|
|
|
|
return self.f
|
|
|
|
|
|
|
|
#################################################################################################################################
|
|
|
|
class HTTPDownloadRequest(HTTPGetCallback):
|
|
|
|
def __init__(self, terrasync, src, dst, callback = None ):
|
|
|
|
super().__init__(src, self.callback)
|
|
|
|
self.terrasync = terrasync
|
|
|
|
self.dst = dst
|
|
|
|
self.mycallback = callback
|
|
|
|
|
|
|
|
def callback(self):
|
2016-12-28 02:46:59 +00:00
|
|
|
if self.result.status != 200:
|
|
|
|
return
|
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
with open(self.dst, 'wb') as f:
|
|
|
|
f.write(self.result.read())
|
|
|
|
|
|
|
|
if self.mycallback != None:
|
|
|
|
self.mycallback(self)
|
|
|
|
|
|
|
|
#################################################################################################################################
|
|
|
|
|
|
|
|
def hash_of_file(fname):
|
2016-05-11 20:59:59 +00:00
|
|
|
if not os.path.exists( fname ):
|
|
|
|
return None
|
|
|
|
|
2016-05-04 21:08:22 +00:00
|
|
|
hash = hashlib.sha1()
|
|
|
|
try:
|
|
|
|
with open(fname, "rb") as f:
|
|
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
|
|
hash.update(chunk)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return hash.hexdigest()
|
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
#################################################################################################################################
|
2016-12-28 03:17:46 +00:00
|
|
|
|
|
|
|
class Coordinate:
|
|
|
|
def __init__(self, lat, lon):
|
|
|
|
self.lat = lat
|
|
|
|
self.lon = lon
|
|
|
|
|
|
|
|
class DownloadBoundaries:
|
|
|
|
def __init__(self, top, left, bottom, right):
|
|
|
|
if top < bottom:
|
|
|
|
raise ValueError("top cannot be less than bottom")
|
|
|
|
if right < left:
|
2016-12-28 14:32:55 +00:00
|
|
|
raise ValueError("right cannot be less than left")
|
2016-12-28 03:17:46 +00:00
|
|
|
|
|
|
|
if top > 90 or bottom < -90:
|
|
|
|
raise ValueError("top and bottom must be a valid latitude")
|
|
|
|
if left < -180 or right > 180:
|
|
|
|
raise ValueError("left and right must be a valid longitude")
|
|
|
|
self.top = top
|
|
|
|
self.left = left
|
|
|
|
self.bottom = bottom
|
|
|
|
self.right = right
|
|
|
|
|
|
|
|
def is_coordinate_inside_boundaries(self, coordinate):
|
|
|
|
if coordinate.lat < self.bottom or coordinate.lat > self.top:
|
|
|
|
return False
|
|
|
|
if coordinate.lon < self.left or coordinate.lon > self.right:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def parse_terrasync_coordinate(coordinate):
|
|
|
|
matches = re.match("(w|e)(\d{3})(n|s)(\d{2})", coordinate)
|
|
|
|
if not matches:
|
|
|
|
return None
|
|
|
|
lon = int(matches.group(2))
|
|
|
|
if matches.group(1) == "w":
|
|
|
|
lon *= -1
|
|
|
|
lat = int(matches.group(4))
|
|
|
|
if matches.group(3) == "s":
|
|
|
|
lat *= -1
|
|
|
|
return Coordinate(lat, lon)
|
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
class TerraSync:
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-12-28 04:41:59 +00:00
|
|
|
def __init__(self, url, target, quick, removeOrphan, downloadBoundaries):
|
2016-05-18 10:51:29 +00:00
|
|
|
self.setUrl(url).setTarget(target)
|
|
|
|
self.quick = quick
|
|
|
|
self.removeOrphan = removeOrphan
|
|
|
|
self.httpGetter = None
|
2016-12-28 03:17:46 +00:00
|
|
|
self.downloadBoundaries = downloadBoundaries
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def setUrl(self, url):
|
|
|
|
self.url = url.rstrip('/').strip()
|
|
|
|
return self
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def setTarget(self, target):
|
|
|
|
self.target = target.rstrip('/').strip()
|
|
|
|
return self
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def start(self):
|
|
|
|
self.httpGetter = HTTPGetter(self.url)
|
|
|
|
self.updateDirectory("", "", None )
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def updateFile(self, serverPath, localPath, fileHash ):
|
|
|
|
localFullPath = join(self.target, localPath)
|
|
|
|
if fileHash != None and hash_of_file(localFullPath) == fileHash:
|
|
|
|
#print("hash of file matches, not downloading")
|
|
|
|
return
|
|
|
|
|
2018-01-26 06:38:51 +00:00
|
|
|
print("Downloading '{}'".format(serverPath))
|
2016-05-18 10:51:29 +00:00
|
|
|
|
|
|
|
request = HTTPDownloadRequest(self, serverPath, localFullPath )
|
|
|
|
self.httpGetter.get(request)
|
2016-05-11 20:59:59 +00:00
|
|
|
|
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def updateDirectory(self, serverPath, localPath, dirIndexHash):
|
2018-01-26 06:38:51 +00:00
|
|
|
print("Processing '{}'...".format(serverPath))
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2018-01-26 06:38:51 +00:00
|
|
|
if serverPath:
|
|
|
|
serverFolderName = os.path.basename(serverPath)
|
2016-12-28 03:17:46 +00:00
|
|
|
coordinate = parse_terrasync_coordinate(serverFolderName)
|
|
|
|
if coordinate and not self.downloadBoundaries.is_coordinate_inside_boundaries(coordinate):
|
|
|
|
return
|
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
localFullPath = join(self.target, localPath)
|
|
|
|
if not os.path.exists( localFullPath ):
|
|
|
|
os.makedirs( localFullPath )
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
localDirIndex = join(localFullPath, ".dirindex")
|
|
|
|
if dirIndexHash != None and hash_of_file(localDirIndex) == dirIndexHash:
|
|
|
|
# print("hash of dirindex matches, not downloading")
|
|
|
|
if not self.quick:
|
|
|
|
self.handleDirindexFile( localDirIndex )
|
|
|
|
else:
|
2018-01-26 06:38:51 +00:00
|
|
|
request = HTTPDownloadRequest(self,
|
|
|
|
serverPath + "/.dirindex",
|
|
|
|
localDirIndex,
|
|
|
|
self.handleDirindexRequest)
|
2016-05-18 10:51:29 +00:00
|
|
|
self.httpGetter.get(request)
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def handleDirindexRequest(self, dirindexRequest):
|
|
|
|
self.handleDirindexFile(dirindexRequest.dst)
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
def handleDirindexFile(self, dirindexFile):
|
|
|
|
dirIndex = DirIndex(dirindexFile)
|
|
|
|
serverFiles = []
|
2017-09-01 08:33:35 +00:00
|
|
|
serverDirs = []
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
for file in dirIndex.getFiles():
|
|
|
|
f = file['name']
|
|
|
|
h = file['hash']
|
2018-01-26 06:38:51 +00:00
|
|
|
self.updateFile("/" + dirIndex.getPath() + "/" + f,
|
|
|
|
join(dirIndex.getPath(), f),
|
|
|
|
h)
|
2016-05-18 10:51:29 +00:00
|
|
|
serverFiles.append(f)
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
for subdir in dirIndex.getDirectories():
|
|
|
|
d = subdir['name']
|
|
|
|
h = subdir['hash']
|
2018-01-26 06:38:51 +00:00
|
|
|
self.updateDirectory("/" + dirIndex.getPath() + "/" + d,
|
|
|
|
join(dirIndex.getPath(), d),
|
|
|
|
h)
|
2017-09-01 08:33:35 +00:00
|
|
|
serverDirs.append(d)
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
if self.removeOrphan:
|
|
|
|
localFullPath = join(self.target, dirIndex.getPath())
|
2018-01-26 06:38:51 +00:00
|
|
|
localFiles = [ f for f in listdir(localFullPath)
|
|
|
|
if isfile(join(localFullPath, f)) ]
|
2016-05-18 10:51:29 +00:00
|
|
|
for f in localFiles:
|
|
|
|
if f != ".dirindex" and not f in serverFiles:
|
2017-09-01 08:33:35 +00:00
|
|
|
#print("removing orphan file", join(localFullPath,f) )
|
2016-05-18 10:51:29 +00:00
|
|
|
os.remove( join(localFullPath,f) )
|
2018-01-26 06:38:51 +00:00
|
|
|
localDirs = [ f for f in listdir(localFullPath)
|
|
|
|
if isdir(join(localFullPath, f)) ]
|
2017-09-01 08:33:35 +00:00
|
|
|
for f in localDirs:
|
|
|
|
if not f in serverDirs:
|
|
|
|
#print ("removing orphan dir",f)
|
|
|
|
shutil.rmtree( join(localFullPath,f) )
|
2016-05-11 12:57:38 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
#################################################################################################################################
|
|
|
|
|
|
|
|
|
2016-12-28 14:32:55 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-u", "--url", dest="url", metavar="URL",
|
|
|
|
default="http://flightgear.sourceforge.net/scenery", help="Server URL [default: %(default)s]")
|
|
|
|
parser.add_argument("-t", "--target", dest="target", metavar="DIR",
|
2016-12-28 04:41:59 +00:00
|
|
|
default=".", help="Directory to store the files [default: current directory]")
|
2016-12-28 14:32:55 +00:00
|
|
|
parser.add_argument("-q", "--quick", dest="quick", action="store_true",
|
2016-12-28 04:41:59 +00:00
|
|
|
default=False, help="Quick")
|
2016-12-28 14:32:55 +00:00
|
|
|
parser.add_argument("-r", "--remove-orphan", dest="removeOrphan", action="store_true",
|
2016-12-28 04:41:59 +00:00
|
|
|
default=False, help="Remove old scenery files")
|
|
|
|
|
2016-12-28 14:32:55 +00:00
|
|
|
parser.add_argument("--top", dest="top", type=int,
|
|
|
|
default=90, help="Maximum latitude to include in download [default: %(default)d]")
|
|
|
|
parser.add_argument("--bottom", dest="bottom", type=int,
|
|
|
|
default=-90, help="Minimum latitude to include in download [default: %(default)d]")
|
|
|
|
parser.add_argument("--left", dest="left", type=int,
|
|
|
|
default=-180, help="Minimum longitude to include in download [default: %(default)d]")
|
|
|
|
parser.add_argument("--right", dest="right", type=int,
|
|
|
|
default=180, help="Maximum longitude to include in download [default: %(default)d]")
|
2016-12-28 04:41:59 +00:00
|
|
|
|
2016-12-28 14:32:55 +00:00
|
|
|
args = parser.parse_args()
|
2016-12-28 04:41:59 +00:00
|
|
|
|
2016-12-28 14:32:55 +00:00
|
|
|
terraSync = TerraSync(args.url, args.target, args.quick, args.removeOrphan,
|
|
|
|
DownloadBoundaries(args.top, args.left, args.bottom, args.right))
|
2016-05-04 21:08:22 +00:00
|
|
|
|
2016-05-18 10:51:29 +00:00
|
|
|
terraSync.start()
|