Skip to content

Instantly share code, notes, and snippets.

@vineethguna
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save vineethguna/4687b671bc105609fabe to your computer and use it in GitHub Desktop.

Select an option

Save vineethguna/4687b671bc105609fabe to your computer and use it in GitHub Desktop.
To create a share folder in dropbox and share it with the specified emails
__author__ = 'Vineeth'
import requests
import re
class Dropbox():
def __init__(self, username, password):
self.MAIN_URL = 'https://www.dropbox.com'
self.LOGIN_URL = 'https://www.dropbox.com/ajax_login'
self.HOME_URL = 'https://www.dropbox.com/home'
self.SHARE_FOLDER_URL = 'https://www.dropbox.com/share_ajax/new'
self.username = username
self.password = password
self.uid = None
self.share_t_value = None
self.session = requests.Session()
self.login()
def login(self):
# Do a GET to MAIN URL of dropbox
main_url_response = self.session.get(self.MAIN_URL)
if main_url_response.status_code == 200:
# Get t value from the cookie of the session
t_value = main_url_response.cookies['t']
# Login Payload
payload = {
'is_xhr': True,
't': t_value,
'cont': '/',
'login_email': self.username,
'login_password': self.password
}
# Do a POST Request to LOGIN_URL with the payload
# This will make you login to Dropbox.
# Store the UUID which you get from response
login_url_response = self.session.post(self.LOGIN_URL, data=payload,
verify=False, allow_redirects=True)
if login_url_response.status_code == 200:
print "Login Successful"
# Set t value which is used for share operations
self.share_t_value = login_url_response.cookies['t']
self.uid = login_url_response.json()['id']
else:
raise Exception('Login unsuccessful')
else:
raise Exception('Response code from home page is not 200')
def share_folder(self, folder_name, emails_list):
# Do a GET to HOME URL of dropbox
home_url_response = self.session.get(self.HOME_URL)
if home_url_response.status_code == 200:
# Get X-dropbox-request-id from the header
parent_id = home_url_response.headers['x-dropbox-request-id']
# Share Payload
emails = ",".join(emails_list)
payload = {
'emails': emails,
'custom_message': "Share",
'inviter': 'all',
'access_type': 2,
'folder_settings': 1,
'folder_name': folder_name,
'custom_folder_settings': 1,
'_subject_uid': self.uid,
't': self.share_t_value,
'is_xhr': True,
'parent_request_id': parent_id
}
# Do a POST Request to SHARE_URL with the payload
# This will create shared folder and sends invites
# to all the emails which are given
share_url_response = self.session.post(self.SHARE_FOLDER_URL, data=payload,
verify=False, allow_redirects=True)
if share_url_response.status_code == 200:
content = share_url_response.text
if re.search(r'success_msg', content):
print "Folder {0} has successfully shared to {1}".format(folder_name, emails)
elif re.search(r'err', content):
print "Folder {0} was not successfully shared to {1}".format(folder_name, emails)
print content
else:
print "Folder sharing unsuccessful. Response code {0}".format(share_url_response.status_code)
else:
print "Could not query Home page"
# create a dropbox object
dropbox = Dropbox('<email-id>', '<password>')
# Example to share a folder to one email
dropbox.share_folder('testfoo1', ['[email protected]'])
# Example to share a folder to multiple emails
dropbox.share_folder('testfoo2', ['[email protected]','[email protected]'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment