Originally Posted by
ryzhov_al
Достаточно просто, в Tutorial'е
всё расписано с примерами. Только нужны базовые знания по Python.^)
Базовых знаний python-а нету Но, немного погуглив, доработал Ваш скрипт(делал по аналогии):
Code:
#!/opt/bin/python
import cmd
import locale
import os
import pprint
import shlex
import sys
import time
from dropbox import client, rest, session
APP_KEY = 'xxx'
APP_SECRET = 'yyy'
ACCESS_TYPE = 'dropbox'
TOKEN_FILE = "/opt/var/token_store_dropbox.txt"
class StoredSession(session.DropboxSession):
"""a wrapper around DropboxSession that stores a token to a file on disk"""
def __init__(self, consumer_key, consumer_secret, access_type,tokenFile, locale=None):
self.tokenFile = tokenFile
super(StoredSession,self).__init__(consumer_key, consumer_secret, access_type, locale);
def load_creds(self):
try:
stored_creds = open(self.tokenFile).read()
self.set_token(*stored_creds.split('|'))
return True
except IOError:
return False
def write_creds(self, token):
f = open(self.tokenFile, 'w+')
f.write("|".join([token.key, token.secret]))
f.close()
def delete_creds(self):
os.unlink(self.tokenFile)
def link(self):
request_token = self.obtain_request_token()
url = self.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()
self.obtain_access_token(request_token)
self.write_creds(self.token)
def unlink(self):
self.delete_creds()
session.DropboxSession.unlink(self)
def main():
if APP_KEY == '' or APP_SECRET == '':
exit("You need to set a APP_KEY and APP_SECRET!")
sess = StoredSession(APP_KEY, APP_SECRET, access_type=ACCESS_TYPE, tokenFile=TOKEN_FILE)
#check command line arguments
nextIsInput = False
nextIsDir = False
from_file_path = None
dir_name = None
for arg in sys.argv:
if arg == "-s":
#setup mode
sess.link()
exit("script is now ready to go")
break
elif arg == "-p":
nextIsDir = True
elif nextIsDir:
dir_name = arg
nextIsDir = False
elif arg == "-f":
#next one is the input file
nextIsInput = True
elif nextIsInput:
from_file_path = arg
nextIsInput = False
if not from_file_path:
exit("no arguments given")
api_client = client.DropboxClient(sess)
if not sess.load_creds():
exit("You need to run this script in setup mode (-s option) before you can use it")
from_file_path.strip()
from_file = open(os.path.expanduser(from_file_path))
from_file_parts = from_file.name.split("/")
if not dir_name:
api_client.put_file("/" + from_file_parts[len(from_file_parts)-1], from_file)
else:
dir_name = dir_name.strip()
api_client.put_file("/" + dir_name +"/" + from_file_parts[len(from_file_parts)-1], from_file)
if __name__ == '__main__':
main()
Зеленым отмечены мои вставки. Теперь ключом -p задается директория в dropbox, например:
uploadDropboxScriptF.py -f file -p dir
Если директории dir нет, то она создается.
Вроде, работает. =)