import mechanize
import os
import glob
verbose = True
def upload_files(local_files, remote_dir, email, password):
""" Upload a local file to Dropbox """
# Fire up a browser using mechanize
br = mechanize.Browser()
br.set_handle_equiv(True)
# br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
# br.set_handle_robots(False)
# br.set_debug_http(True)
# br.set_debug_responses(True)
# br.set_debug_redirects(True)
br.addheaders = [('User-agent', ' Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1018.0 Safari/535.19')]
if verbose: print 'Opening login page...'
# Browse to the login page
r = br.open('https://www.dropbox.com/login')
# just in case you need the html
# html = r.read()
# this shows a lot of info
print r.info()
if verbose: print br.title(), br.geturl()
# Enter the username and password into the login form
isLoginForm = lambda l: l.action == "https://www.dropbox.com/login" and l.method == "POST"
try:
if verbose: print 'selecting form...'
br.select_form(predicate=isLoginForm)
except:
print("Unable to find login form.");
exit(1);
br['login_email'] = email
br['login_password'] = password
# Send the form
if verbose: print 'submitting login form...'
response = br.submit()
# Add our file upload to the upload form once logged in
isUploadForm = lambda u: u.action == "https://dl-web.dropbox.com/upload" and u.method == "POST"
for local_file in local_files:
try:
br.select_form(predicate=isUploadForm)
except:
print("Unable to find upload form.");
print("Make sure that your login information is correct.");
exit(1);
br.form.find_control("dest").readonly = False
br.form.set_value(remote_dir, "dest")
remote_file = os.path.basename(local_file)
br.form.add_file(open(local_file, "rb"), "", remote_file)
# Submit the form with the file
if verbose: print 'Uploading %s... to Dropbox:/%s/%s' % (local_file, remote_dir, remote_file),
br.submit()
if verbose: print 'Ok'
print 'All completed Ok!'
if __name__ == "__main__":
import sys
from getpass import getpass
email = raw_input("Enter Dropbox email address:")
password = getpass("Enter Dropbox password:")
# email = ''
# password = ''
local_dir = ''
local_files = []
remote_dir = "/"
# allow multiple local file names as input args
# first arg with 'dir:' prefix is parsed as remote path
if len(sys.argv) > 1:
if sys.argv[1].lower().startswith('dir:'):
remote_dir = sys.argv[1][4:]
if not remote_dir.startswith('/'):
remote_dir = '/' + remote_dir
if verbose: print 'Using remote_dir=', remote_dir
del sys.argv[1]
local_files = sys.argv[1:]
prepared_local_files = []
for local_file in local_files:
# explode globs
if '*' in local_file:
prepared_local_files += glob.glob(local_file)
else:
prepared_local_files += local_file
if not len(prepared_local_files):
print 'No local files provided! Quitting.'
sys.exit(2)
upload_files(prepared_local_files, remote_dir, email, password)
Diff to Previous Revision
--- revision 1 2012-02-01 17:31:17
+++ revision 2 2012-02-02 13:20:29
@@ -1,9 +1,10 @@
import mechanize
import os
+import glob
verbose = True
-def upload_file(local_file, remote_dir, remote_file, email, password):
+def upload_files(local_files, remote_dir, email, password):
""" Upload a local file to Dropbox """
# Fire up a browser using mechanize
@@ -27,11 +28,11 @@
# just in case you need the html
# html = r.read()
# this shows a lot of info
-# print 'Response:', br.response().read()
+
print r.info()
if verbose: print br.title(), br.geturl()
-# if verbose: print br.forms()
+
# Enter the username and password into the login form
isLoginForm = lambda l: l.action == "https://www.dropbox.com/login" and l.method == "POST"
@@ -52,52 +53,64 @@
# Add our file upload to the upload form once logged in
isUploadForm = lambda u: u.action == "https://dl-web.dropbox.com/upload" and u.method == "POST"
- try:
- br.select_form(predicate=isUploadForm)
- except:
- print("Unable to find upload form.");
- print("Make sure that your login information is correct.");
- exit(1);
+ for local_file in local_files:
+ try:
+ br.select_form(predicate=isUploadForm)
+ except:
+ print("Unable to find upload form.");
+ print("Make sure that your login information is correct.");
+ exit(1);
+
+ br.form.find_control("dest").readonly = False
+ br.form.set_value(remote_dir, "dest")
- br.form.find_control("dest").readonly = False
- br.form.set_value(remote_dir, "dest")
- br.form.add_file(open(local_file, "rb"), "", remote_file)
-
- # Submit the form with the file
- if verbose: print 'Uploading file %s...' % os.path.basename(local_file),
- br.submit()
- if verbose: print 'Ok',
-
+ remote_file = os.path.basename(local_file)
+
+ br.form.add_file(open(local_file, "rb"), "", remote_file)
+ # Submit the form with the file
+ if verbose: print 'Uploading %s... to Dropbox:/%s/%s' % (local_file, remote_dir, remote_file),
+ br.submit()
+
+ if verbose: print 'Ok'
+ print 'All completed Ok!'
+
if __name__ == "__main__":
import sys
from getpass import getpass
email = raw_input("Enter Dropbox email address:")
password = getpass("Enter Dropbox password:")
- #upload_file("example.py","/pc/Scripts","example.py",email,password)
+ # email = ''
+ # password = ''
- local_dir = r'___ENTER_YOUR_DEFAULT_DIR_HERE___'
- local_files = ["__ENTER_YOUR_DEFAULT_FILE_NAME_HERE"]
- remote_dir = "/" # you can pass 'dir:/my_remote_dir' as *first* argument
+ local_dir = ''
+ local_files = []
+ remote_dir = "/"
# allow multiple local file names as input args
# first arg with 'dir:' prefix is parsed as remote path
if len(sys.argv) > 1:
if sys.argv[1].lower().startswith('dir:'):
remote_dir = sys.argv[1][4:]
+
if not remote_dir.startswith('/'):
remote_dir = '/' + remote_dir
+
if verbose: print 'Using remote_dir=', remote_dir
del sys.argv[1]
local_files = sys.argv[1:]
+ prepared_local_files = []
+
+ for local_file in local_files:
+
+ # explode globs
+ if '*' in local_file:
+ prepared_local_files += glob.glob(local_file)
+ else:
+ prepared_local_files += local_file
+
+ if not len(prepared_local_files):
+ print 'No local files provided! Quitting.'
+ sys.exit(2)
- for local_file in local_files:
- # if first file has path then use that instead of the hard-coded path
- lpath, lfile = os.path.split(local_file)
- if lpath:
- local_dir = lpath
- local_file = lfile
-
- remote_file = local_file
- upload_file(os.path.join(local_dir, local_file), remote_dir, remote_file, email, password)
- print("['%s' to the Dropbox folder '%s']" % (os.path.join(local_dir, local_file), remote_dir))
+ upload_files(prepared_local_files, remote_dir, email, password)