Davide Cassenti

Davide Cassenti

Gentleman and Scholar Software Engineer

Upload to Flickr with Python

Today I have found an interesting python script that allows you to upload photos from a local directory to Flickr; I decided to modify it and post here my modifications. The original script can be found here.

The script is pretty easy to use: it searches image files in a given directory, check an history file in order to know if the photo has been already uploaded, and if it is not the case, upload it on flickr; it is possible to specify if the photo needs to be public, private or shared, which tags assign to it and which name/description add.

The only thing I didn’t like was the way used by the script to know if the file was been already uploaded: simply, only the file name was used; I decided to change the script in order to allow to rename or move the photos inside the same directory (and sub directories) that uploadr.py is checking: to do so, I use the md5 digest of the image file as key in the history file. Another little thing I have added, is a lock file: if the script is already running it will exit with an error message on the screen.

The md5 digest is calculated by reading the content of the image file:

def uploadImage( self, image ):
	# calculate image md5
        image = str( image )
        f = open( image, 'r' )
        digestmd5 = str( md5.new( f.read() ).hexdigest() )
        f.close()

        if ( not self.uploaded.has_key( digestmd5 ) ):
            print "Uploading ", image , "...",

This digest is then used in the logUpload() function in order to save the information about the photo. The function is called if the upload is valid with a

self.logUpload( digestmd5 )

. This is the new logUpload() function:

def logUpload( self, d ):
        d = str( d )
        self.uploaded[ d ] = "ok"

I’ve also changed a little thing in the upload() function: the history file is now opened and closed for each upload. This makes the script slower, I know, but in case of failure (such as a kill), we avoid the problem to have an inconsistent history file.

for image in newImages:
            self.uploaded = shelve.open( HISTORY_FILE )
            self.uploadImage( image )
            self.uploaded.close()

The script may be run using the

nohup python uploadr.py -d &

command or, better, by adding a new line in the crontab file to call the script every X minutes. Before using the script, you need to change the parameters at the beginning of the file, then at the first run you will be prompted to allow upload.py to access your flickr account. Here’s the configurations to change, they are pretty simple:

#
# Location to scan for new images
#
IMAGE_DIR = "/directory/with/photos/"
#
#   Flickr settings
#
FLICKR = {"title": "",
        "description": "",
        "tags": "auto-upload",
        "is_public": "0",
        "is_friend": "0",
        "is_family": "0" }
#
#   How often to check for new images to upload  (in seconds )
#
SLEEP_TIME = 60 * 60
#
#   File we keep the history of uploaded images in.
#
HISTORY_FILE = "uploadr.history"

You can download my uploadr.py script by clicking here: remember to remove the .txt extension to use it. If you have any comment, let me know.

Share this:

Tags: ,

One Response to “Upload to Flickr with Python”

  1. This is great! Thanks for your article. I am a newbie at python and this was a big help.

Leave a Reply