2017-01-31 15:08:00 +01:00
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcvfs
|
|
|
|
import resources.lib.tinyurl as tinyurl
|
|
|
|
import resources.lib.utils as utils
|
2019-08-26 22:36:53 +02:00
|
|
|
|
|
|
|
#don't die on import error yet, these might not even get used
|
|
|
|
try:
|
|
|
|
import dropbox
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
from resources.lib.pydrive.auth import GoogleAuth
|
|
|
|
from resources.lib.pydrive.drive import GoogleDrive
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
class DropboxAuthorizer:
|
|
|
|
APP_KEY = ""
|
|
|
|
APP_SECRET = ""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
if(self.APP_KEY == '' and self.APP_SECRET == ''):
|
|
|
|
#we can't go any farther, need these for sure
|
2017-11-07 21:19:27 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30027) + ' ' + utils.getString(30058),utils.getString(30059))
|
|
|
|
|
|
|
|
result = False
|
|
|
|
|
|
|
|
return result
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
def isAuthorized(self):
|
2017-12-04 00:32:21 +01:00
|
|
|
user_token = self._getToken()
|
2017-01-31 15:08:00 +01:00
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
return user_token != ''
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
def authorize(self):
|
|
|
|
result = True
|
2017-11-07 21:19:27 +01:00
|
|
|
|
|
|
|
if(not self.setup()):
|
|
|
|
return False
|
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
if(self.isAuthorized()):
|
|
|
|
#delete the token to start over
|
|
|
|
self._deleteToken()
|
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
#copied flow from http://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#dropbox.oauth.DropboxOAuth2FlowNoRedirect
|
|
|
|
flow = dropbox.oauth.DropboxOAuth2FlowNoRedirect(self.APP_KEY,self.APP_SECRET)
|
2017-01-31 15:08:00 +01:00
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
url = flow.start()
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
#print url in log
|
|
|
|
utils.log("Authorize URL: " + url)
|
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30057),tinyurl.shorten(url))
|
2017-12-04 00:32:21 +01:00
|
|
|
|
|
|
|
#get the auth code
|
|
|
|
code = xbmcgui.Dialog().input(utils.getString(30027) + ' ' + utils.getString(30103))
|
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
#if user authorized this will work
|
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
try:
|
|
|
|
user_token = flow.finish(code)
|
|
|
|
self._setToken(user_token.access_token)
|
2019-08-19 22:23:48 +02:00
|
|
|
except Exception as e:
|
2017-12-04 00:32:21 +01:00
|
|
|
utils.log("Error: %s" % (e,))
|
|
|
|
result = False
|
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
return result;
|
|
|
|
|
|
|
|
#return the DropboxClient, or None if can't be created
|
|
|
|
def getClient(self):
|
|
|
|
result = None
|
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
user_token = self._getToken()
|
|
|
|
|
|
|
|
if(user_token != ''):
|
2017-01-31 15:08:00 +01:00
|
|
|
#create the client
|
2017-12-04 00:32:21 +01:00
|
|
|
result = dropbox.Dropbox(user_token)
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
try:
|
2017-12-04 00:32:21 +01:00
|
|
|
result.users_get_current_account()
|
2017-01-31 15:08:00 +01:00
|
|
|
except:
|
|
|
|
#this didn't work, delete the token file
|
|
|
|
self._deleteToken()
|
2017-12-04 00:32:21 +01:00
|
|
|
result = None
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
def _setToken(self,token):
|
2017-01-31 15:08:00 +01:00
|
|
|
#write the token files
|
|
|
|
token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"),'w')
|
2017-12-04 00:32:21 +01:00
|
|
|
token_file.write(token)
|
2017-01-31 15:08:00 +01:00
|
|
|
token_file.close()
|
|
|
|
|
|
|
|
def _getToken(self):
|
2017-12-04 00:32:21 +01:00
|
|
|
#get token, if it exists
|
2017-01-31 15:08:00 +01:00
|
|
|
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))):
|
|
|
|
token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"))
|
2017-12-04 00:32:21 +01:00
|
|
|
token = token_file.read()
|
2017-01-31 15:08:00 +01:00
|
|
|
token_file.close()
|
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
return token
|
2017-01-31 15:08:00 +01:00
|
|
|
else:
|
2017-12-04 00:32:21 +01:00
|
|
|
return ""
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
def setup(self):
|
2017-11-07 21:19:27 +01:00
|
|
|
result = True
|
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
if(self.CLIENT_ID == '' and self.CLIENT_SECRET == ''):
|
|
|
|
#we can't go any farther, need these for sure
|
2017-11-07 21:19:27 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30098) + ' ' + utils.getString(30058),utils.getString(30108))
|
2017-11-07 22:19:46 +01:00
|
|
|
result = False
|
2017-11-07 21:19:27 +01:00
|
|
|
|
|
|
|
return result
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
def isAuthorized(self):
|
|
|
|
return xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "google_drive.dat"))
|
|
|
|
|
|
|
|
def authorize(self):
|
|
|
|
result = True
|
|
|
|
|
2017-11-07 21:19:27 +01:00
|
|
|
if(not self.setup()):
|
|
|
|
return False
|
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
#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))
|
2017-12-04 00:32:21 +01:00
|
|
|
code = xbmcgui.Dialog().input(utils.getString(30098) + ' ' + utils.getString(30103))
|
2017-01-31 15:08:00 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|