xbmcbackup/resources/lib/authorizers.py

148 lines
4.3 KiB
Python
Raw Normal View History

2019-11-25 22:48:42 +01:00
import xbmcgui
import xbmcvfs
2021-04-08 22:43:26 +02:00
import pyqrcode
import resources.lib.tinyurl as tinyurl
import resources.lib.utils as utils
Update for Leia (#117) * updated addon.xml for Krypton * default log level is always debug now * added screenshots per krypton format * started new way of defining backup directories * reconfigured simple backup process * added an advanced backup editor and combined settings.xml scripts into a launcher * added strings for advanced editor * there was a function to do this * match excluded with regex * updated def for the addons set * directory has to end in slash to use exists() * added a backup set chooser on restore * added string for restore browser * utilize details to show root folder and icons * save non translated paths, better cross platform support * revert dropbox python 2.6 changes * start of #132 * can't have duplicate ids * updated strings * closes #132 * added a disclaimer for breaking changes * split backup and restore into separate functions * updated scripting to pass in list of sets to restore * beta version * added 2 min delay in startup - part of #147 * forgot to remove debug message * change to wait for abort in case someone tries to close Kodi * add retroplayer game saves to default file list * display restore points with most recent on top * remove length check, breaking change with this version means old archives are no longer compatible * format restore list according to regional settings * this function isn't used anymore, legacy of old file manager * use images folder as default * added note about compatibility * added utils function for regional date, use for scheduler notifications as well * add/remove include and exclude directories to a set * paths should have / at the end * show path relative to root * if in advanced mode allow jumping to editor from launch screen * check that path is within root folder of set * cannot have duplicate set names or rules regarding folders within a set * put strings in correct lang file * beta version bump * accidentally deleted string id * change exclude criteria. Regex was not matching in complex cases * make sure the dest folder (backup set root) exists before writing to it * modify select display to show recursive value for included folders * use a context menu here * added ability to toggle recursion of sub folders * beta 3 * added support doc * wrong branch * don't need this import anymore * don't need these imports * part of #133
2019-08-26 22:40:15 +02:00
2019-11-25 22:19:57 +01:00
# don't die on import error yet, these might not even get used
Update for Leia (#117) * updated addon.xml for Krypton * default log level is always debug now * added screenshots per krypton format * started new way of defining backup directories * reconfigured simple backup process * added an advanced backup editor and combined settings.xml scripts into a launcher * added strings for advanced editor * there was a function to do this * match excluded with regex * updated def for the addons set * directory has to end in slash to use exists() * added a backup set chooser on restore * added string for restore browser * utilize details to show root folder and icons * save non translated paths, better cross platform support * revert dropbox python 2.6 changes * start of #132 * can't have duplicate ids * updated strings * closes #132 * added a disclaimer for breaking changes * split backup and restore into separate functions * updated scripting to pass in list of sets to restore * beta version * added 2 min delay in startup - part of #147 * forgot to remove debug message * change to wait for abort in case someone tries to close Kodi * add retroplayer game saves to default file list * display restore points with most recent on top * remove length check, breaking change with this version means old archives are no longer compatible * format restore list according to regional settings * this function isn't used anymore, legacy of old file manager * use images folder as default * added note about compatibility * added utils function for regional date, use for scheduler notifications as well * add/remove include and exclude directories to a set * paths should have / at the end * show path relative to root * if in advanced mode allow jumping to editor from launch screen * check that path is within root folder of set * cannot have duplicate set names or rules regarding folders within a set * put strings in correct lang file * beta version bump * accidentally deleted string id * change exclude criteria. Regex was not matching in complex cases * make sure the dest folder (backup set root) exists before writing to it * modify select display to show recursive value for included folders * use a context menu here * added ability to toggle recursion of sub folders * beta 3 * added support doc * wrong branch * don't need this import anymore * don't need these imports * part of #133
2019-08-26 22:40:15 +02:00
try:
from dropbox import dropbox
2020-12-18 16:31:52 +01:00
from dropbox import oauth
Update for Leia (#117) * updated addon.xml for Krypton * default log level is always debug now * added screenshots per krypton format * started new way of defining backup directories * reconfigured simple backup process * added an advanced backup editor and combined settings.xml scripts into a launcher * added strings for advanced editor * there was a function to do this * match excluded with regex * updated def for the addons set * directory has to end in slash to use exists() * added a backup set chooser on restore * added string for restore browser * utilize details to show root folder and icons * save non translated paths, better cross platform support * revert dropbox python 2.6 changes * start of #132 * can't have duplicate ids * updated strings * closes #132 * added a disclaimer for breaking changes * split backup and restore into separate functions * updated scripting to pass in list of sets to restore * beta version * added 2 min delay in startup - part of #147 * forgot to remove debug message * change to wait for abort in case someone tries to close Kodi * add retroplayer game saves to default file list * display restore points with most recent on top * remove length check, breaking change with this version means old archives are no longer compatible * format restore list according to regional settings * this function isn't used anymore, legacy of old file manager * use images folder as default * added note about compatibility * added utils function for regional date, use for scheduler notifications as well * add/remove include and exclude directories to a set * paths should have / at the end * show path relative to root * if in advanced mode allow jumping to editor from launch screen * check that path is within root folder of set * cannot have duplicate set names or rules regarding folders within a set * put strings in correct lang file * beta version bump * accidentally deleted string id * change exclude criteria. Regex was not matching in complex cases * make sure the dest folder (backup set root) exists before writing to it * modify select display to show recursive value for included folders * use a context menu here * added ability to toggle recursion of sub folders * beta 3 * added support doc * wrong branch * don't need this import anymore * don't need these imports * part of #133
2019-08-26 22:40:15 +02:00
except ImportError:
pass
2019-11-26 17:43:38 +01:00
2021-04-08 22:43:26 +02:00
class QRCode(xbmcgui.WindowXMLDialog):
def __init__(self, *args, **kwargs):
self.image = kwargs["image"]
self.text = kwargs["text"]
self.url = kwargs['url']
def onInit(self):
self.imagecontrol = 501
self.textbox1 = 502
self.textbox2 = 504
self.okbutton = 503
self.showdialog()
def showdialog(self):
self.getControl(self.imagecontrol).setImage(self.image)
self.getControl(self.textbox1).setText(self.text)
self.getControl(self.textbox2).setText(self.url)
self.setFocus(self.getControl(self.okbutton))
def onClick(self, controlId):
if (controlId == self.okbutton):
self.close()
class DropboxAuthorizer:
APP_KEY = ""
APP_SECRET = ""
2019-11-26 17:43:38 +01:00
def __init__(self):
self.APP_KEY = utils.getSetting('dropbox_key')
self.APP_SECRET = utils.getSetting('dropbox_secret')
def setup(self):
2017-11-07 21:19:27 +01:00
result = True
2019-11-25 22:33:34 +01:00
if(self.APP_KEY == '' and self.APP_SECRET == ''):
2019-11-25 22:19:57 +01:00
# we can't go any farther, need these for sure
xbmcgui.Dialog().ok(utils.getString(30010), '%s %s\n%s' % (utils.getString(30027), utils.getString(30058), utils.getString(30059)))
2017-11-07 21:19:27 +01:00
result = False
2019-11-25 22:33:34 +01:00
return result
def isAuthorized(self):
user_token = self._getToken()
2019-11-26 17:49:17 +01:00
return user_token != ''
def authorize(self):
result = True
2017-11-07 21:19:27 +01:00
if(not self.setup()):
return False
2019-11-25 22:33:34 +01:00
if(self.isAuthorized()):
2019-11-25 22:19:57 +01:00
# delete the token to start over
self._deleteToken()
2019-11-25 22:19:57 +01:00
# copied flow from http://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#dropbox.oauth.DropboxOAuth2FlowNoRedirect
2020-12-18 16:31:52 +01:00
flow = oauth.DropboxOAuth2FlowNoRedirect(self.APP_KEY, self.APP_SECRET)
url = flow.start()
2019-11-25 22:19:57 +01:00
# print url in log
utils.log("Authorize URL: " + url)
2021-04-08 22:43:26 +02:00
# create a QR Code
shortUrl = str(tinyurl.shorten(url), 'utf-8')
imageFile = xbmcvfs.translatePath(utils.data_dir() + '/qrcode.png')
qrIMG = pyqrcode.create(shortUrl)
qrIMG.png(imageFile, scale=10)
# show the dialog prompt to authorize
qr = QRCode("script-backup-qrcode.xml", utils.addon_dir(), "default", image=imageFile, text=utils.getString(30056), url=shortUrl)
qr.doModal()
# cleanup
del qr
xbmcvfs.delete(imageFile)
2019-11-25 22:19:57 +01:00
# get the auth code
code = xbmcgui.Dialog().input(utils.getString(30027) + ' ' + utils.getString(30103))
2019-11-25 22:33:34 +01:00
2019-11-26 17:43:38 +01:00
# if user authorized this will work
try:
user_token = flow.finish(code)
self._setToken(user_token.access_token)
2019-08-19 22:23:48 +02:00
except Exception as e:
utils.log("Error: %s" % (e,))
result = False
2019-11-25 22:33:34 +01:00
2019-11-26 17:49:17 +01:00
return result
2019-11-25 22:19:57 +01:00
# return the DropboxClient, or None if can't be created
def getClient(self):
result = None
user_token = self._getToken()
if(user_token != ''):
2019-11-26 17:43:38 +01:00
# create the client
result = dropbox.Dropbox(user_token)
try:
result.users_get_current_account()
except:
2019-11-26 17:43:38 +01:00
# this didn't work, delete the token file
self._deleteToken()
result = None
2019-11-25 22:33:34 +01:00
return result
2019-11-25 22:45:41 +01:00
def _setToken(self, token):
2019-11-25 22:19:57 +01:00
# write the token files
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"), 'w')
token_file.write(token)
token_file.close()
def _getToken(self):
2019-11-25 22:19:57 +01:00
# get token, if it exists
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))):
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))
token = token_file.read()
token_file.close()
return token
else:
return ""
2019-11-25 22:33:34 +01:00
def _deleteToken(self):
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))):
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))