Connecting to Dropbox with Python

Yesterday, I stumbled on to Dropbox’s Python API. I ended up using their tutorial to design a simple class for accessing my Dropbox. You’ll need to download their dropbox module to follow along or use “pip install dropbox” to install it. You will also need to sign up for a key and secret. Once you have that, you’ll need to name your application and choose your access level. Then you should be good to go!

Now we’re ready to start writing some code. Here’s what I came up with:

import dropbox
import os
import sys
import webbrowser

from configobj import ConfigObj

########################################################################
class DropObj(object):
    """
    Dropbox object that can access your dropbox folder,
    as well as download and upload files to dropbox
    """

    #----------------------------------------------------------------------
    def __init__(self, filename=None, path='/'):
        """Constructor"""
        self.base_path = os.path.dirname(os.path.abspath(__file__))
        self.filename = filename
        self.path = path
        self.client = None
        
        config_path = os.path.join(self.base_path, "config.ini")
        if os.path.exists(config_path):
            try:
                cfg = ConfigObj(config_path)
            except IOError:
                print "ERROR opening config file!"
                sys.exit(1)
            self.cfg_dict = cfg.dict()
        else:
            print "ERROR: config.ini not found! Exiting!"
            sys.exit(1)
           
        self.connect()
            
    #----------------------------------------------------------------------
    def connect(self):
        """
        Connect and authenticate with dropbox
        """
        app_key = self.cfg_dict["key"]
        app_secret = self.cfg_dict["secret"]
        
        access_type = "dropbox"
        session = dropbox.session.DropboxSession(app_key,
                                                 app_secret,
                                                 access_type)
                
        request_token = session.obtain_request_token()
        
        url = session.build_authorize_url(request_token)
        msg = "Opening %s. Please make sure this application is allowed before continuing."
        print msg % url
        webbrowser.open(url)
        raw_input("Press enter to continue")
        access_token = session.obtain_access_token(request_token)
        
        self.client = dropbox.client.DropboxClient(session)
        
    #----------------------------------------------------------------------
    def download_file(self, filename=None, outDir=None):
        """
        Download either the file passed to the class or the file passed
        to the method
        """
        
        if filename:
            fname = filename
            f, metadata = self.client.get_file_and_metadata("/" + fname)
        else:
            fname = self.filename
            f, metadata = self.client.get_file_and_metadata("/" + fname)
            
        if outDir:
            dst = os.path.join(outDir, fname)
        else:
            dst = fname
            
        with open(fname, "w") as fh:
            fh.write(f.read())
            
        return dst, metadata
        
    #----------------------------------------------------------------------
    def get_account_info(self):
        """
        Returns the account information, such as user's display name,
        quota, email address, etc
        """
        return self.client.account_info()
        
    #----------------------------------------------------------------------
    def list_folder(self, folder=None):
        """
        Return a dictionary of information about a folder
        """
        if folder:
            folder_metadata = self.client.metadata(folder)
        else:
            folder_metadata = self.client.metadata("/")
        return folder_metadata
        
    #----------------------------------------------------------------------
    def upload_file(self):
        """
        Upload a file to dropbox, returns file info dict
        """
        try:
            with open(self.filename) as fh:
                path = os.path.join(self.path, self.filename)
                res = self.client.put_file(path, fh)
                print "uploaded: ", res
        except Exception, e:
            print "ERROR: ", e
            
        return res
            
if __name__ == "__main__":
    drop = DropObj("somefile.txt")

I put my key and secret into a config file that looks something like this:

key = someKey
secret = secret

Then I use configobj to extract that information into a Python dictionary. I tried to find a way to cache the request token, but I always got an error about my token being expired, so this script will always pop up a browser window asking you to “Allow” your application access to your Dropbox. Once you’re connected, the client is instantiated and you can extract all kinds of information about your Dropbox. For example, you can get your account information or a bunch of metadata about any folder in your Dropbox. I also created and upload_file method to allow easy uploading of files. I probably should have made it so you could pass a file to the method too, but that’ll have to wait until version 2. The download_file method follows the official methodology that Dropbox espouses, however every file I downloaded ended up being corrupt.

Anyway, I thought it was a fun API to play with and hope you’ll find this little script helpful too.

1 thought on “Connecting to Dropbox with Python”

Comments are closed.