mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-29 08:21:27 +00:00
labels: pull in separate thread. also fix error messages
This commit is contained in:
parent
3bbd3685d8
commit
3948ef64fa
1 changed files with 37 additions and 41 deletions
|
@ -2,6 +2,7 @@ from electrum.util import print_error
|
||||||
|
|
||||||
import httplib, urllib
|
import httplib, urllib
|
||||||
import socket
|
import socket
|
||||||
|
import threading
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
from urlparse import urlparse, parse_qs
|
from urlparse import urlparse, parse_qs
|
||||||
|
@ -74,7 +75,7 @@ class Plugin(BasePlugin):
|
||||||
|
|
||||||
if self.auth_token():
|
if self.auth_token():
|
||||||
# If there is an auth token we can try to actually start syncing
|
# If there is an auth token we can try to actually start syncing
|
||||||
self.full_pull()
|
threading.Thread(target=self.do_full_pull).start()
|
||||||
|
|
||||||
def auth_token(self):
|
def auth_token(self):
|
||||||
return self.config.get("plugin_label_api_key")
|
return self.config.get("plugin_label_api_key")
|
||||||
|
@ -141,7 +142,7 @@ class Plugin(BasePlugin):
|
||||||
layout.addWidget(self.upload, 2,1)
|
layout.addWidget(self.upload, 2,1)
|
||||||
|
|
||||||
self.download = QPushButton("Force download")
|
self.download = QPushButton("Force download")
|
||||||
self.download.clicked.connect(lambda: self.full_pull(True))
|
self.download.clicked.connect(self.full_pull)
|
||||||
layout.addWidget(self.download, 2,2)
|
layout.addWidget(self.download, 2,2)
|
||||||
|
|
||||||
c = QPushButton(_("Cancel"))
|
c = QPushButton(_("Cancel"))
|
||||||
|
@ -165,8 +166,12 @@ class Plugin(BasePlugin):
|
||||||
if self.do_full_push():
|
if self.do_full_push():
|
||||||
QMessageBox.information(None, _("Labels uploaded"), _("Your labels have been uploaded."))
|
QMessageBox.information(None, _("Labels uploaded"), _("Your labels have been uploaded."))
|
||||||
|
|
||||||
def full_pull(self, force = False):
|
def full_pull(self):
|
||||||
if self.do_full_pull(force) and force:
|
try:
|
||||||
|
self.do_full_pull(True)
|
||||||
|
except BaseException as e:
|
||||||
|
QMessageBox.information(None, _("Error"), str(e))
|
||||||
|
return
|
||||||
QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized."))
|
QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized."))
|
||||||
self.window.update_history_tab()
|
self.window.update_history_tab()
|
||||||
self.window.update_completions()
|
self.window.update_completions()
|
||||||
|
@ -180,12 +185,12 @@ class Plugin(BasePlugin):
|
||||||
try:
|
try:
|
||||||
encoded_key = self.encode(key)
|
encoded_key = self.encode(key)
|
||||||
except:
|
except:
|
||||||
print_error('cannot encode', encoded_key)
|
print_error('cannot encode', key)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
encoded_value = self.encode(value)
|
encoded_value = self.encode(value)
|
||||||
except:
|
except:
|
||||||
print_error('cannot encode', encoded_value)
|
print_error('cannot encode', value)
|
||||||
continue
|
continue
|
||||||
bundle["labels"][encoded_key] = encoded_value
|
bundle["labels"][encoded_key] = encoded_value
|
||||||
|
|
||||||
|
@ -211,20 +216,14 @@ class Plugin(BasePlugin):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def do_full_pull(self, force = False):
|
def do_full_pull(self, force = False):
|
||||||
try:
|
|
||||||
connection = httplib.HTTPConnection(self.target_host)
|
connection = httplib.HTTPConnection(self.target_host)
|
||||||
connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())),"", {'Content-Type': 'application/json'})
|
connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())),"", {'Content-Type': 'application/json'})
|
||||||
response = connection.getresponse()
|
response = connection.getresponse()
|
||||||
if response.reason == httplib.responses[httplib.NOT_FOUND]:
|
if response.reason == httplib.responses[httplib.NOT_FOUND]:
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
response = json.loads(response.read())
|
response = json.loads(response.read())
|
||||||
except ValueError as e:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if "error" in response:
|
if "error" in response:
|
||||||
QMessageBox.warning(None, _("Error"),_("Could not sync labels: %s" % response["error"]))
|
raise BaseException(_("Could not sync labels: %s" % response["error"]))
|
||||||
return False
|
|
||||||
|
|
||||||
for label in response:
|
for label in response:
|
||||||
decoded_key = self.decode(label["external_id"])
|
decoded_key = self.decode(label["external_id"])
|
||||||
|
@ -238,7 +237,4 @@ class Plugin(BasePlugin):
|
||||||
if force or not self.wallet.labels.get(decoded_key):
|
if force or not self.wallet.labels.get(decoded_key):
|
||||||
self.wallet.labels[decoded_key] = decoded_label
|
self.wallet.labels[decoded_key] = decoded_label
|
||||||
self.wallet.storage.put('labels', self.wallet.labels)
|
self.wallet.storage.put('labels', self.wallet.labels)
|
||||||
return True
|
print_error("received labels")
|
||||||
except socket.gaierror as e:
|
|
||||||
print_error('Error connecting to service: %s ' % e)
|
|
||||||
return False
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue