From 49f6ae52707e7023920a77c1aea224811352374a Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Tue, 31 Jan 2017 08:08:00 -0600 Subject: [PATCH] moved cloud creation code to an authorizer class added authorize as part of settings --- addon.xml | 2 +- authorize_cloud.py | 23 ++++ changelog.txt | 4 + resources/language/English/strings.xml | 1 + resources/lib/authorizers.py | 144 +++++++++++++++++++++++++ resources/lib/vfs.py | 106 +++--------------- resources/settings.xml | 3 +- 7 files changed, 192 insertions(+), 91 deletions(-) create mode 100644 authorize_cloud.py create mode 100644 resources/lib/authorizers.py diff --git a/addon.xml b/addon.xml index 7cdb5ce..bd34d8b 100644 --- a/addon.xml +++ b/addon.xml @@ -1,6 +1,6 @@  + name="Backup" version="1.1.0" provider-name="robweber"> diff --git a/authorize_cloud.py b/authorize_cloud.py new file mode 100644 index 0000000..1edbd65 --- /dev/null +++ b/authorize_cloud.py @@ -0,0 +1,23 @@ +import xbmc +import xbmcgui +import xbmcvfs +import resources.lib.utils as utils +from resources.lib.authorizers import DropboxAuthorizer,GoogleDriveAuthorizer + +#drobpox +if(utils.getSetting('remote_selection') == '2'): + authorizer = DropboxAuthorizer() + + if(authorizer.authorize()): + xbmcgui.Dialog().ok("Backup","Dropbox is authorized") + else: + xbmcgui.Dialog().ok("Backup","Error Authorizing Dropbox") + +#google drive +elif(utils.getSetting('remote_selection') == '3'): + authorizer = GoogleDriveAuthorizer() + + if(authorizer.authorize()): + xbmcgui.Dialog().ok("Backup","Google Drive is authorized") + else: + xbmcgui.Dialog().ok("Backup","Error Authorizing Google Drive") diff --git a/changelog.txt b/changelog.txt index fc00efa..ecd02c8 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +Version 1.1.0 + +added tinyurl for oauth urls + Version 1.0.9 fixed dropbox rest.py for Python 2.6 - thanks koying! diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index 6a315a8..be70a60 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -95,4 +95,5 @@ Error extracting the zip archive Click OK to enter code Google Drive Validation Code + Authorize Now diff --git a/resources/lib/authorizers.py b/resources/lib/authorizers.py new file mode 100644 index 0000000..ab6f61a --- /dev/null +++ b/resources/lib/authorizers.py @@ -0,0 +1,144 @@ +import xbmc +import xbmcgui +import xbmcvfs +import resources.lib.tinyurl as tinyurl +import resources.lib.utils as utils +from resources.lib.dropbox import client, rest, session +from resources.lib.pydrive.auth import GoogleAuth +from resources.lib.pydrive.drive import GoogleDrive + +class DropboxAuthorizer: + APP_KEY = "" + APP_SECRET = "" + + def __init__(self): + self.APP_KEY = utils.getSetting('dropbox_key') + self.APP_SECRET = utils.getSetting('dropbox_secret') + + self.setup() + + def setup(self): + if(self.APP_KEY == '' and self.APP_SECRET == ''): + #we can't go any farther, need these for sure + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30058),utils.getString(30059)) + return + + def isAuthorized(self): + user_token_key,user_token_secret = self._getToken() + + return user_token_key != '' and user_token_secret != '' + + def authorize(self): + result = True + if(self.isAuthorized()): + #delete the token to start over + self._deleteToken() + + sess = session.DropboxSession(self.APP_KEY,self.APP_SECRET,"app_folder") + + token = sess.obtain_request_token() + url = sess.build_authorize_url(token) + + #print url in log + utils.log("Authorize URL: " + url) + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30057),tinyurl.shorten(url)) + + #if user authorized this will work + user_token = sess.obtain_access_token(token) + self._setToken(user_token.key,user_token.secret) + + return result; + + #return the DropboxClient, or None if can't be created + def getClient(self): + result = None + sess = session.DropboxSession(self.APP_KEY,self.APP_SECRET,"app_folder") + user_token_key,user_token_secret = self._getToken() + + if(user_token_key != '' and user_token_secret != ''): + #create the client + sess.set_token(user_token_key,user_token_secret) + result = client.DropboxClient(sess) + + try: + utils.log(str(result.account_info())) + except: + #this didn't work, delete the token file + self._deleteToken() + + return result + + def _setToken(self,key,secret): + #write the token files + token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"),'w') + token_file.write("%s|%s" % (key,secret)) + token_file.close() + + def _getToken(self): + #get tokens, if they exist + if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))): + token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt")) + key,secret = token_file.read().split('|') + token_file.close() + + return [key,secret] + else: + return ["",""] + + def _deleteToken(self): + if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))): + xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt")) + +class GoogleDriveAuthorizer: + CLIENT_ID = '' + CLIENT_SECRET = '' + + def __init__(self): + self.CLIENT_ID = utils.getSetting('google_drive_id') + self.CLIENT_SECRET = utils.getSetting('google_drive_secret') + + self.setup() + + def setup(self): + if(self.CLIENT_ID == '' and self.CLIENT_SECRET == ''): + #we can't go any farther, need these for sure + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30058),utils.getString(30059)) + return + + def isAuthorized(self): + return xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "google_drive.dat")) + + def authorize(self): + result = True + + #create authorization helper and load default settings + gauth = GoogleAuth(xbmc.validatePath(xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml'))) + gauth.LoadClientConfigSettings() + + settings = {"client_id":self.CLIENT_ID,'client_secret':self.CLIENT_SECRET} + + drive_url = gauth.GetAuthUrl(settings) + + utils.log("Google Drive Authorize URL: " + drive_url) + + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30102),tinyurl.shorten(drive_url)) + code = xbmcgui.Dialog().input(utils.getString(30103)) + + gauth.Auth(code) + gauth.SaveCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat'))) + + return result + + def getClient(self): + #create authorization helper and load default settings + gauth = GoogleAuth(xbmc.validatePath(xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml'))) + gauth.LoadClientConfigSettings() + + gauth.LoadCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat'))) + + result = GoogleDrive(gauth) + + return result + + + diff --git a/resources/lib/vfs.py b/resources/lib/vfs.py index 8e8475c..54000e6 100644 --- a/resources/lib/vfs.py +++ b/resources/lib/vfs.py @@ -6,9 +6,10 @@ import xbmcgui import zipfile import zlib import os -from dropbox import client, rest, session -from pydrive.auth import GoogleAuth +import sys +from dropbox import client from pydrive.drive import GoogleDrive +from authorizers import DropboxAuthorizer,GoogleDriveAuthorizer class Vfs: root_path = None @@ -122,43 +123,15 @@ class DropboxFileSystem(Vfs): def __init__(self,rootString): self.set_root(rootString) - - self.APP_KEY = utils.getSetting('dropbox_key') - self.APP_SECRET = utils.getSetting('dropbox_secret') - self.setup() + authorizer = DropboxAuthorizer() - def setup(self): - if(self.APP_KEY == '' or self.APP_SECRET == ''): - xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30058),utils.getString(30059)) - return - - user_token_key,user_token_secret = self.getToken() - - sess = session.DropboxSession(self.APP_KEY,self.APP_SECRET,"app_folder") - - if(user_token_key == '' and user_token_secret == ''): - token = sess.obtain_request_token() - url = sess.build_authorize_url(token) - - #print url in log - utils.log("Authorize URL: " + url) - xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30057),tinyurl.shorten(url)) - - #if user authorized this will work - user_token = sess.obtain_access_token(token) - self.setToken(user_token.key,user_token.secret) - + if(authorizer.isAuthorized()): + self.client = authorizer.getClient() else: - sess.set_token(user_token_key,user_token_secret) - - self.client = client.DropboxClient(sess) - - try: - utils.log(str(self.client.account_info())) - except: - #this didn't work, delete the token file - self.deleteToken() + #tell the user to go back and run the authorizer + xbmcgui.Dialog().ok(utils.getString(30010),"Authorize this remote service in the settings first") + sys.exit() def listdir(self,directory): if(self.client != None and self.exists(directory)): @@ -254,75 +227,30 @@ class DropboxFileSystem(Vfs): def _fix_slashes(self,filename): return filename.replace('\\','/') - - def setToken(self,key,secret): - #write the token files - token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"),'w') - token_file.write("%s|%s" % (key,secret)) - token_file.close() - - def getToken(self): - #get tokens, if they exist - if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))): - token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt")) - key,secret = token_file.read().split('|') - token_file.close() - - return [key,secret] - else: - return ["",""] - - def deleteToken(self): - if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))): - xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt")) class GoogleDriveFilesystem(Vfs): drive = None history = {} - CLIENT_ID = '' - CLIENT_SECRET = '' FOLDER_TYPE = 'application/vnd.google-apps.folder' def __init__(self,rootString): self.set_root(rootString) - - self.CLIENT_ID = utils.getSetting('google_drive_id') - self.CLIENT_SECRET = utils.getSetting('google_drive_secret') - self.setup() - - def setup(self): - #create authorization helper and load default settings - gauth = GoogleAuth(xbmc.validatePath(xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml'))) - gauth.LoadClientConfigSettings() - - #check if this user is already authorized - if(not xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "google_drive.dat"))): - settings = {"client_id":self.CLIENT_ID,'client_secret':self.CLIENT_SECRET} - - drive_url = gauth.GetAuthUrl(settings) - - utils.log("Google Drive Authorize URL: " + drive_url) + authorizer = GoogleDriveAuthorizer() - xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30102),tinyurl.shorten(drive_url)) - code = xbmcgui.Dialog().input(utils.getString(30103)) - - gauth.Auth(code) - gauth.SaveCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat'))) + if(authorizer.isAuthorized()): + self.drive = authorizer.getClient() else: - gauth.LoadCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat'))) - - #create the drive object - self.drive = GoogleDrive(gauth) - + #tell the user to go back and run the authorizer + xbmcgui.Dialog().ok(utils.getString(30010),"Authorize this remote service in the settings first") + sys.exit() + #make sure we have the folder we need xbmc_folder = self._getGoogleFile(self.root_path) - print xbmc_folder if(xbmc_folder == None): self.mkdir(self.root_path) - - + def listdir(self,directory): files = [] dirs = [] diff --git a/resources/settings.xml b/resources/settings.xml index cda2404..5a0682f 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -13,7 +13,8 @@ - + +