2012-10-26 16:42:10 +02:00
|
|
|
import utils as utils
|
2012-11-05 19:25:25 +01:00
|
|
|
import xbmc
|
2012-11-02 21:59:40 +01:00
|
|
|
import xbmcvfs
|
2012-11-04 21:39:17 +01:00
|
|
|
import xbmcgui
|
2012-11-05 19:25:25 +01:00
|
|
|
import sys
|
2012-11-02 21:59:40 +01:00
|
|
|
from dropbox import client, rest, session
|
|
|
|
|
2012-12-29 02:06:12 +01:00
|
|
|
APP_KEY = utils.getSetting('dropbox_key')
|
|
|
|
APP_SECRET = utils.getSetting('dropbox_secret')
|
2012-10-26 16:42:10 +02:00
|
|
|
|
|
|
|
class Vfs:
|
2012-11-04 21:39:17 +01:00
|
|
|
root_path = None
|
2012-10-26 16:42:10 +02:00
|
|
|
|
2013-01-28 22:44:22 +01:00
|
|
|
def __init__(self,rootString):
|
|
|
|
self.set_root(rootString)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def set_root(self,rootString):
|
2012-11-05 18:13:48 +01:00
|
|
|
old_root = self.root_path
|
2012-11-04 21:39:17 +01:00
|
|
|
self.root_path = rootString
|
|
|
|
|
|
|
|
#fix slashes
|
|
|
|
self.root_path = self.root_path.replace("\\","/")
|
|
|
|
|
|
|
|
#check if trailing slash is included
|
|
|
|
if(self.root_path[-1:] != "/"):
|
|
|
|
self.root_path = self.root_path + "/"
|
2012-11-05 18:13:48 +01:00
|
|
|
|
|
|
|
#return the old root
|
|
|
|
return old_root
|
2012-11-04 21:39:17 +01:00
|
|
|
|
|
|
|
def listdir(self,directory):
|
2012-10-26 16:42:10 +02:00
|
|
|
return {}
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def mkdir(self,directory):
|
2012-10-26 16:42:10 +02:00
|
|
|
return True
|
|
|
|
|
2012-11-06 18:37:39 +01:00
|
|
|
def put(self,source,dest):
|
2012-10-26 16:42:10 +02:00
|
|
|
return True
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2012-11-06 18:37:39 +01:00
|
|
|
def getFile(self,source):
|
|
|
|
return True
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def rmdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return True
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def exists(self,aFile):
|
2012-11-02 21:59:40 +01:00
|
|
|
return True
|
2012-10-26 16:42:10 +02:00
|
|
|
|
2012-11-02 21:59:40 +01:00
|
|
|
class XBMCFileSystem(Vfs):
|
2013-01-28 22:44:22 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def listdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.listdir(directory)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def mkdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.mkdir(directory)
|
|
|
|
|
2012-11-06 18:37:39 +01:00
|
|
|
def put(self,source,dest):
|
2012-11-04 21:39:17 +01:00
|
|
|
return xbmcvfs.copy(source,dest)
|
2012-11-06 18:37:39 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def rmdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.rmdir(directory,True)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def exists(self,aFile):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.exists(aFile)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
class DropboxFileSystem(Vfs):
|
2012-11-05 19:25:25 +01:00
|
|
|
client = None
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2013-01-28 22:44:22 +01:00
|
|
|
def __init__(self,rootString):
|
2013-03-04 17:22:09 +01:00
|
|
|
self.set_root(rootString)
|
2013-01-28 22:44:22 +01:00
|
|
|
self.setup()
|
|
|
|
|
|
|
|
def setup(self):
|
2012-12-30 22:18:59 +01:00
|
|
|
if(APP_KEY == '' or APP_SECRET == ''):
|
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30058),utils.getString(30059))
|
|
|
|
return
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
user_token_key,user_token_secret = self.getToken()
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
sess = session.DropboxSession(APP_KEY,APP_SECRET,"app_folder")
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
if(user_token_key == '' and user_token_secret == ''):
|
2012-11-04 21:39:17 +01:00
|
|
|
token = sess.obtain_request_token()
|
|
|
|
url = sess.build_authorize_url(token)
|
2012-11-05 19:25:25 +01:00
|
|
|
|
|
|
|
#print url in log
|
|
|
|
utils.log("Authorize URL: " + url)
|
2012-11-06 20:09:33 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30057))
|
2012-11-05 19:25:25 +01:00
|
|
|
|
|
|
|
#if user authorized this will work
|
|
|
|
user_token = sess.obtain_access_token(token)
|
|
|
|
self.setToken(user_token.key,user_token.secret)
|
|
|
|
|
|
|
|
else:
|
|
|
|
sess.set_token(user_token_key,user_token_secret)
|
|
|
|
|
|
|
|
self.client = client.DropboxClient(sess)
|
2012-12-13 21:18:14 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
utils.log(str(self.client.account_info()))
|
|
|
|
except:
|
|
|
|
#this didn't work, delete the token file
|
|
|
|
self.deleteToken()
|
2012-11-05 19:25:25 +01:00
|
|
|
|
2012-11-05 22:46:17 +01:00
|
|
|
def listdir(self,directory):
|
|
|
|
if(self.client != None and self.exists(directory)):
|
|
|
|
files = []
|
|
|
|
dirs = []
|
|
|
|
metadata = self.client.metadata(directory)
|
|
|
|
|
|
|
|
for aFile in metadata['contents']:
|
|
|
|
if(aFile['is_dir']):
|
|
|
|
dirs.append(aFile['path'][len(directory):])
|
|
|
|
else:
|
|
|
|
files.append(aFile['path'][len(directory):])
|
|
|
|
|
|
|
|
return [dirs,files]
|
|
|
|
else:
|
|
|
|
return [[],[]]
|
|
|
|
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
def mkdir(self,directory):
|
2013-03-04 17:22:09 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2012-11-05 19:25:25 +01:00
|
|
|
if(self.client != None):
|
|
|
|
if(not self.exists(directory)):
|
|
|
|
self.client.file_create_folder(directory)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-11-05 22:46:17 +01:00
|
|
|
def rmdir(self,directory):
|
2013-03-04 17:22:09 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2012-11-05 22:46:17 +01:00
|
|
|
if(self.client != None and self.exists(directory)):
|
|
|
|
self.client.file_delete(directory)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
def exists(self,aFile):
|
2013-03-04 17:22:09 +01:00
|
|
|
aFile = self._fix_slashes(aFile)
|
2012-11-05 19:25:25 +01:00
|
|
|
if(self.client != None):
|
2012-11-04 21:39:17 +01:00
|
|
|
try:
|
2012-11-05 19:25:25 +01:00
|
|
|
meta_data = self.client.metadata(aFile)
|
|
|
|
#if we make it here the file does exist
|
|
|
|
return True
|
2012-11-04 21:39:17 +01:00
|
|
|
except:
|
2012-11-05 19:25:25 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2013-03-04 17:22:09 +01:00
|
|
|
def put(self,source,dest):
|
|
|
|
dest = self._fix_slashes(dest)
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
if(self.client != None):
|
|
|
|
f = open(source,'rb')
|
2012-11-05 22:46:17 +01:00
|
|
|
try:
|
|
|
|
response = self.client.put_file(dest,f,True)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
#if we have an exception retry
|
|
|
|
retry = True
|
2012-11-06 22:12:14 +01:00
|
|
|
return self.put(source,dest)
|
2012-11-05 19:25:25 +01:00
|
|
|
else:
|
|
|
|
return False
|
2012-11-06 18:37:39 +01:00
|
|
|
|
|
|
|
def get_file(self,source,dest):
|
|
|
|
if(self.client != None):
|
|
|
|
#write the file locally
|
|
|
|
out = open(dest,'wb')
|
|
|
|
f = self.client.get_file(source).read()
|
|
|
|
out.write(f)
|
|
|
|
out.close()
|
2013-03-04 17:27:25 +01:00
|
|
|
return True
|
2012-11-06 18:37:39 +01:00
|
|
|
else:
|
|
|
|
return False
|
2013-03-04 17:22:09 +01:00
|
|
|
|
|
|
|
def _fix_slashes(self,filename):
|
|
|
|
return filename.replace('\\','/')
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
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 ["",""]
|
2012-12-13 21:18:14 +01:00
|
|
|
|
|
|
|
def deleteToken(self):
|
|
|
|
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))):
|
|
|
|
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt"))
|
2012-11-04 21:39:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|