In my last post, I explained how to sync your Raspberry Pi with Dropbox via Python. Now we are going to take that one step further and share images from that Dropbox as they get downloaded by the Pi. I wrote this script for the Pi Day event, while it didn’t get used live because of a lack of a stable connection, I ran it this morning with the files that were saved in our Dropbox App the night of the Pi Day event and all worked really well.
First, take a look at the previous tutorial about how to get everything connected. When we left off the images were simply being displayed but not downloaded or shared. We will start off here with downloading the images that are returned in the client.delta call. The following gets a list of files, checks to see if they exist, if they do not it will download them to the specified folder.
# get a report of files that have changed delta = client.delta() # get the list of files from the report entries = delta["entries"] #loop through the entries for entry in entries: # entry[0] is the filename print entry[0] # tell it where you would like it to save pathname = "./dropbox" + entry[0] # check to see if the file already exists if os.path.isfile(pathname): print "exists" else: try: #save the file f, metadata = client.get_file_and_metadata(entry[0]) out = open(pathname, 'w') out.write(f.read()) out.close() except: print "ERROR, TRY AGAIN"
Now that you have the file (image in our case), it is time to share that with the world. For this, we used If This Then That (ifttt.com) which allows you to make recipes for connecting different social networks together. We made two simple reciepes; one that accepts an email from contact@cttoronto.com with an attachment and shares that attachment and the email subject with twitter and one that does the same on Facebook.
Here is the final code that we used for putting all the pieces together. It runs on a loop, checking the dropbox for updates. When there is an update, it downloads the file. Once the file is downloaded it attaches it to an email and sends that email to trigger@ifttt.com which does the rest for us.
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' USERNAME = "YOUR_EMAIL" PASSWORD = "YOUR_EMAIL_PASSWORD" MAILTO = "trigger@ifttt.com" MAIL_SERVER = 'domain.com:587' # 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" delta = client.delta() entries = delta["entries"] for entry in entries: # print entry[0] pathname = "./dropbox" + entry[0] if os.path.isfile(pathname): print "exists" else: try: print "CONNECTING TO DROPBOX" f, metadata = client.get_file_and_metadata(entry[0]) out = open(pathname, 'w') out.write(f.read()) out.close() print "FILE DOWNLOADED" # create the email, mutlipart because of image and text msg = MIMEMultipart(); #'This is an email generated on the Raspberry Pi, synced with Dropbox. To be auto tweeted') msg['Subject'] = random.choice(randomMessages) #'This is an email generated on the Raspberry Pi, synced with Dropbox. To be auto tweeted' msg['From'] = USERNAME msg['To'] = MAILTO fp = open(pathname, 'rb') img = MIMEImage(fp.read()) fp.close() #attach an image to the email msg.attach(img) #send the email to the SMTP server server = smtplib.SMTP(MAIL_SERVER) server.ehlo_or_helo_if_needed() server.starttls() server.ehlo_or_helo_if_needed() server.login(USERNAME,PASSWORD) server.sendmail(USERNAME, MAILTO, msg.as_string()) server.quit() print "EMAIL SENT" except: print "CONNECTION FAIL, TRY AGAIN"