moved cloud creation code to an authorizer class

added authorize as part of settings
This commit is contained in:
Rob Weber 2017-01-31 08:08:00 -06:00
parent 2e7552896a
commit 49f6ae5270
7 changed files with 192 additions and 91 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.xbmcbackup"
name="Backup" version="1.0.9" provider-name="robweber">
name="Backup" version="1.1.0" provider-name="robweber">
<requires>
<!-- jarvis -->
<import addon="xbmc.python" version="2.24.0"/>

23
authorize_cloud.py Normal file
View File

@ -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")

View File

@ -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!

View File

@ -95,4 +95,5 @@
<string id="30101">Error extracting the zip archive</string>
<string id="30102">Click OK to enter code</string>
<string id="30103">Google Drive Validation Code</string>
<string id="30104">Authorize Now</string>
</strings>

View File

@ -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

View File

@ -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
@ -123,42 +124,14 @@ 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()
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)
authorizer = DropboxAuthorizer()
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)):
@ -255,74 +228,29 @@ 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')
authorizer = GoogleDriveAuthorizer()
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)
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 = []

View File

@ -13,7 +13,8 @@
<setting id="dropbox_secret" type="text" label="30029" visible="eq(-4,2)" default="" />
<setting id="google_drive_id" type="text" label="Client ID" visible="eq(-5,3)" default="" />
<setting id="google_drive_secret" type="text" label="Client Secret" visible="eq(-6,3)" default="" />
<setting id="remove_auth_button" type="action" label="30093" action="RunScript(special://home/addons/script.xbmcbackup/remove_auth.py)" visible="gt(-7,1)"/>
<setting id="remove_auth_button" type="action" label="30104" action="RunScript(special://home/addons/script.xbmcbackup/authorize_cloud.py)" visible="gt(-7,1)"/>
<setting id="remove_auth_button" type="action" label="30093" action="RunScript(special://home/addons/script.xbmcbackup/remove_auth.py)" visible="gt(-8,1)"/>
</category>
<category id="selection" label="30012">
<setting id="backup_addons" type="bool" label="30030" default="true" />