support dynamic sync url

This commit is contained in:
Damien Elmes 2018-07-28 18:08:16 +10:00
parent c15df04622
commit ae46bfa8d1
3 changed files with 31 additions and 29 deletions

View file

@ -50,8 +50,7 @@ STARTING_FACTOR = 2500
SCHEMA_VERSION = 11 SCHEMA_VERSION = 11
SYNC_ZIP_SIZE = int(2.5*1024*1024) SYNC_ZIP_SIZE = int(2.5*1024*1024)
SYNC_ZIP_COUNT = 25 SYNC_ZIP_COUNT = 25
SYNC_BASE = "https://sync.ankiweb.net/" SYNC_BASE = "https://sync%s.ankiweb.net/"
SYNC_MEDIA_BASE = "https://sync.ankiweb.net/msync/"
SYNC_VER = 9 SYNC_VER = 9
HELP_SITE="http://ankisrs.net/docs/manual.html" HELP_SITE="http://ankisrs.net/docs/manual.html"

View file

@ -55,6 +55,7 @@ class Syncer:
self.rmod = meta['mod'] self.rmod = meta['mod']
self.maxUsn = meta['usn'] self.maxUsn = meta['usn']
self.uname = meta.get("uname", "") self.uname = meta.get("uname", "")
self.hostNum = meta.get("hostNum")
meta = self.meta() meta = self.meta()
self.col.log("lmeta", meta) self.col.log("lmeta", meta)
self.lmod = meta['mod'] self.lmod = meta['mod']
@ -472,11 +473,20 @@ class _MonitoringFile(io.BufferedReader):
class HttpSyncer: class HttpSyncer:
def __init__(self, hkey=None, client=None): def __init__(self, hkey=None, client=None, hostNum=None):
self.hkey = hkey self.hkey = hkey
self.skey = checksum(str(random.random()))[:8] self.skey = checksum(str(random.random()))[:8]
self.client = client or AnkiRequestsClient() self.client = client or AnkiRequestsClient()
self.postVars = {} self.postVars = {}
self.hostNum = hostNum
self.prefix = "sync/"
def syncURL(self):
if devMode:
url = "https://l1sync.ankiweb.net/"
else:
url = SYNC_BASE % (self.hostNum or "")
return url + self.prefix
def assertOk(self, resp): def assertOk(self, resp):
# not using raise_for_status() as aqt expects this error msg # not using raise_for_status() as aqt expects this error msg
@ -552,13 +562,8 @@ Content-Type: application/octet-stream\r\n\r\n""")
class RemoteServer(HttpSyncer): class RemoteServer(HttpSyncer):
def __init__(self, hkey): def __init__(self, hkey, hostNum):
HttpSyncer.__init__(self, hkey) HttpSyncer.__init__(self, hkey, hostNum=hostNum)
def syncURL(self):
if devMode:
return "https://l1sync.ankiweb.net/sync/"
return SYNC_BASE + "sync/"
def hostKey(self, user, pw): def hostKey(self, user, pw):
"Returns hkey or none if user/pw incorrect." "Returns hkey or none if user/pw incorrect."
@ -619,19 +624,14 @@ class RemoteServer(HttpSyncer):
class FullSyncer(HttpSyncer): class FullSyncer(HttpSyncer):
def __init__(self, col, hkey, client): def __init__(self, col, hkey, client, hostNum):
HttpSyncer.__init__(self, hkey, client) HttpSyncer.__init__(self, hkey, client, hostNum=hostNum)
self.postVars = dict( self.postVars = dict(
k=self.hkey, k=self.hkey,
v="ankidesktop,%s,%s"%(anki.version, platDesc()), v="ankidesktop,%s,%s"%(anki.version, platDesc()),
) )
self.col = col self.col = col
def syncURL(self):
if devMode:
return "https://l1.ankiweb.net/sync/"
return SYNC_BASE + "sync/"
def download(self): def download(self):
runHook("sync", "download") runHook("sync", "download")
self.col.close() self.col.close()
@ -810,14 +810,10 @@ class MediaSyncer:
class RemoteMediaServer(HttpSyncer): class RemoteMediaServer(HttpSyncer):
def __init__(self, col, hkey, client): def __init__(self, col, hkey, client, hostNum):
self.col = col self.col = col
HttpSyncer.__init__(self, hkey, client) HttpSyncer.__init__(self, hkey, client, hostNum=hostNum)
self.prefix = "msync/"
def syncURL(self):
if devMode:
return "https://l1.ankiweb.net/msync/"
return SYNC_MEDIA_BASE
def begin(self): def begin(self):
self.postVars = dict( self.postVars = dict(

View file

@ -43,7 +43,9 @@ class SyncManager(QObject):
# create the thread, setup signals and start running # create the thread, setup signals and start running
t = self.thread = SyncThread( t = self.thread = SyncThread(
self.pm.collectionPath(), self.pm.profile['syncKey'], self.pm.collectionPath(), self.pm.profile['syncKey'],
auth=auth, media=self.pm.profile['syncMedia']) auth=auth, media=self.pm.profile['syncMedia'],
hostNum=self.pm.profile.get("hostNum"),
)
t.event.connect(self.onEvent) t.event.connect(self.onEvent)
self.label = _("Connecting...") self.label = _("Connecting...")
prog = self.mw.progress.start(immediate=True, label=self.label) prog = self.mw.progress.start(immediate=True, label=self.label)
@ -64,6 +66,7 @@ class SyncManager(QObject):
showText(self.thread.syncMsg) showText(self.thread.syncMsg)
if self.thread.uname: if self.thread.uname:
self.pm.profile['syncUser'] = self.thread.uname self.pm.profile['syncUser'] = self.thread.uname
self.pm.profile['hostNum'] = self.thread.hostNum
def delayedInfo(): def delayedInfo():
if self._didFullUp and not self._didError: if self._didFullUp and not self._didError:
showInfo(_("""\ showInfo(_("""\
@ -290,12 +293,13 @@ class SyncThread(QThread):
event = pyqtSignal(str, str) event = pyqtSignal(str, str)
def __init__(self, path, hkey, auth=None, media=True): def __init__(self, path, hkey, auth=None, media=True, hostNum=None):
QThread.__init__(self) QThread.__init__(self)
self.path = path self.path = path
self.hkey = hkey self.hkey = hkey
self.auth = auth self.auth = auth
self.media = media self.media = media
self.hostNum = hostNum
self._abort = 0 # 1=flagged, 2=aborting self._abort = 0 # 1=flagged, 2=aborting
def flagAbort(self): def flagAbort(self):
@ -311,7 +315,7 @@ class SyncThread(QThread):
except: except:
self.fireEvent("corrupt") self.fireEvent("corrupt")
return return
self.server = RemoteServer(self.hkey) self.server = RemoteServer(self.hkey, hostNum=self.hostNum)
self.client = Syncer(self.col, self.server) self.client = Syncer(self.col, self.server)
self.sentTotal = 0 self.sentTotal = 0
self.recvTotal = 0 self.recvTotal = 0
@ -405,6 +409,7 @@ class SyncThread(QThread):
self.fireEvent("error", "Unknown sync return code.") self.fireEvent("error", "Unknown sync return code.")
self.syncMsg = self.client.syncMsg self.syncMsg = self.client.syncMsg
self.uname = self.client.uname self.uname = self.client.uname
self.hostNum = self.client.hostNum
# then move on to media sync # then move on to media sync
self._syncMedia() self._syncMedia()
@ -419,7 +424,8 @@ class SyncThread(QThread):
f = self.fullSyncChoice f = self.fullSyncChoice
if f == "cancel": if f == "cancel":
return return
self.client = FullSyncer(self.col, self.hkey, self.server.client) self.client = FullSyncer(self.col, self.hkey, self.server.client,
hostNum=self.hostNum)
try: try:
if f == "upload": if f == "upload":
if not self.client.upload(): if not self.client.upload():
@ -437,7 +443,8 @@ class SyncThread(QThread):
def _syncMedia(self): def _syncMedia(self):
if not self.media: if not self.media:
return return
self.server = RemoteMediaServer(self.col, self.hkey, self.server.client) self.server = RemoteMediaServer(self.col, self.hkey, self.server.client,
hostNum=self.hostNum)
self.client = MediaSyncer(self.col, self.server) self.client = MediaSyncer(self.col, self.server)
try: try:
ret = self.client.sync() ret = self.client.sync()