Raspberry Pi + Dropbox Sync

http://cttoronto.com/03/16/2013/raspberry-pi-dropbox-sync/

March 16, 2013 by Marc Pelland

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:

1
2
3
4
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

1
2
3
4
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:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.