Raspberry Pi + Dropbox Sync

For Pi Day, we decided that it would be fun if we built a system that would auto-post photos from the Raspberry Pi to our Facebook and Twitter accounts.  We knew that it would be easier to just run all of it from the computer that was running the game, but this seemed like a good opportunity to learn something about the Raspberry Pi.  As a result, we setup a system that would save images from the webcams in Flash to a dropbox folder, that folder was synchronized with a Dropbox app that the Raspberry Pi would connect to and send that image to ifttt.com for automation.  This is how we did it

Setup

Courtesy of this site: http://raspberrypigadget.wordpress.com/2012/07/01/day-six-dropbox-access/, we were able to get the Dropbox SDK installed on the Raspberry Pi.

First installing setup tools:

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar xzf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
sudo python setup.py install

Then grab the SDK

mget https://www.dropbox.com/static/developers/dropbox-python-sdk-1.5.1.zip
unzip dropbox-python-sdk-1.5.1.zip
cd dropbox-1.5.1
sudo python setup.py install

Next you need to create an app on Dropbox: (https://www.dropbox.com/developers/apps). This app will be what you can sync with using the SDK. For this example and for the Pi Day project, we setup the app as an app_folder. So go in and create your app and make not of the app key and app secret.

Once you have that setup, it is time to connect to it with python. This is a two stage process. The first is to connect to the app and capture your access token. We did this and captured it to a text file:

 

import os, time

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# going to store the access info in this file
TOKENS = 'dropbox_token.txt'

# Get your app key and secret from the Dropbox developer website
APP_KEY = 'KEY_FROM_DROPBOX'
APP_SECRET = 'SECRET_FROM_DROPBOX'
ACCESS_TYPE = 'app_folder'

sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

request_token = sess.obtain_request_token()

url = sess.build_authorize_url(request_token)

# Make the user sign in and authorize this token
print "url:", url
print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."

raw_input()

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)

# save the key to the file so you don't need to do this again
token_file = open(TOKENS,'w')

token_file.write("%s|%s" % (access_token.key,access_token.secret) )

token_file.close()

# just for reference, print out the account info that you are accessing with
print "linked account:", client.account_info()

Now that you have an access token, you can remove the code that was in place to capture that and simply pass the information from the text file to the app. The following is the second step in the connection process where we can sync with the Dropbox App.

import os, time

# Include the Dropbox SDK libraries

from dropbox import client, rest, session

TOKENS = 'dropbox_token.txt'

# Get your app key and secret from the Dropbox developer website
APP_KEY = 'KEY_FROM_DROPBOX'
APP_SECRET = 'SECRET_FROM_DROPBOX'
ACCESS_TYPE = 'app_folder'

# get the stored key from step one and use it
token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()

# init the session and the client
sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )
sess.set_token(token_key,token_secret)

client = client.DropboxClient(sess)
path_to_watch = "."

while 1:
	print "CHECKING DELTA"

	#check to see if there is a difference on dropbox with what you have
	print client.delta

	#delay before the next attempt
	time.sleep (10000)

I will be doing another post later on that shows how to tie this in to an email that will post to your social networks.

 

8 Replies to “Raspberry Pi + Dropbox Sync”

  1. while compiling the first process in python i got some errors.like this
    ./dropbox.py: 1: ./dropbox.py: import: not found
    from: can’t read /var/mail/dropbox
    ./dropbox.py: 7: ./dropbox.py: TOKENS: not found
    ./dropbox.py: 10: ./dropbox.py: APP_KEY: not found
    ./dropbox.py: 11: ./dropbox.py: APP_SECRET: not found
    ./dropbox.py: 12: ./dropbox.py: ACCESS_TYPE: not found
    ./dropbox.py: 14: ./dropbox.py: Syntax error: “(” unexpected

    Please help me………

    1. is that from our code verbatim? what have you modified in the code? I don’t believe that we are connecting to any mail account in our script.

      what version of python are you using?

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami