I gotta have my orange juice.

Jesu, Juva

SmugMug uploader

with 4 comments

[SmugMug]I’ve written a small Python script to upload pictures to a SmugMug gallery. I love SmugMug and use it extensively for family photos. I’m using this script for my personal use because it’s much simpler and much less of a resource hog than a browser-based uploader, and also because it was a fun exercise to try out the SmugMug API. You can run this script as follows to upload one or more files:

python upload.py gallery-name picture-file-name . . .

On Windows I’ve set up a desktop shortcut pointing to the script, and I can drag and drop a pile of picture files onto the icon and it will upload away. I’ve tested it using both Python 2.5 using simplejson, and also using Python 2.6 which has simplejson built in. Earlier versions of Python may require you to change the import of hashlib to md5, and change the hashlib.md5() invocation to a md5.new() invocation. You’ll also need to modify the script to contain your email address and SmugMug password, and obtain a SmugMug API key for your own development use, but this is a very painless process. Here is the script:

#!/usr/bin/python

##########
# Requirements: Python 2.6 or
#               simplejson from http://pypi.python.org/pypi/simplejson
##########

EMAIL='...'
PASSWORD='...'

##########
APIKEY='...'
API_VERSION='1.2.0'
API_URL='https://api.smugmug.com/hack/json/1.2.0/'
UPLOAD_URL='http://upload.smugmug.com/photos/xmlrawadd.mg'

import sys, urllib, urllib2, urlparse, hashlib, traceback
try    : import json
except : import simplejson as json

if len(sys.argv) < 3 :
  print 'Usage:'
  print '  upload.py  album  picture1  [picture2  [...]]'
  print
  sys.exit(0)

album_name = sys.argv[1]

def safe_geturl(request) :
  try :
    response = urllib2.urlopen(request).read()
    result = json.loads(response)
    if result['stat'] != 'ok' : raise Exception('Bad result code')
  except :
    print 'Error issuing request'
    print 'Request was:'
    print '  ' + str(request)
    try :
      print 'Response was:'
      print response
    except :
      pass
    traceback.print_exc()
    sys.exit(1)
  return result

def smugmug_request(method, params) :
  paramstrings = [urllib.quote(key)+'='+urllib.quote(params[key]) for key in params]
  paramstrings += ['method=' + method]
  url = urlparse.urljoin(API_URL, '?' + '&'.join(paramstrings))
  return safe_geturl(url)

result = smugmug_request('smugmug.login.withPassword',
                         {'APIKey'       : APIKEY,
                          'EmailAddress' : EMAIL,
                          'Password'     : PASSWORD})
session = result['Login']['Session']['id']

result = smugmug_request('smugmug.albums.get', {'SessionID' : session})
album_id = None
for album in result['Albums'] :
  if album['Title'] == album_name :
    album_id = album['id']
    break
if album_id is None :
  print 'That album does not exist'
  sys.exit(1)

for filename in sys.argv[2:] :
  data = open(filename, 'rb').read()
  print 'Uploading ' + filename
  upload_request = urllib2.Request(UPLOAD_URL,
                                   data,
                                   {'Content-Length'  : len(data),
                                    'Content-MD5'     : hashlib.md5(data).hexdigest(),
                                    'X-Smug-SessionID': session,
                                    'X-Smug-Version'  : API_VERSION,
				    'X-Smug-ResponseType' : 'JSON',
				    'X-Smug-AlbumID'  : album_id,
				    'X-Smug-FileName' : filename })
  safe_geturl(upload_request)

print 'Done'

I am donating this script to the public domain. You are welcome to use and modify it as you please without conditions. I’d appreciate hearing about your experience with this script or any changes and improvements you’ve made; please leave a comment. Thanks!

Written by Scott Moonen

December 1, 2008 at 3:33 pm

4 Responses

Subscribe to comments with RSS.

  1. Hello Scott,
    I’m working on a program that will sync my local photos with my smugmug account in the background. Your code is making it pretty easy for me. It is also the best example I’ve seen of working with json data in python (which is new to me).
    Thanks a bunch,
    Jason

    Jason

    December 8, 2008 at 2:20 pm

  2. It looks like your blog software munged the single quotes in that script such that when copy-pasted, the script will not run. You may want to correct that, or at least post a link to a raw copy of the script.

    Michael Leuchtenburg

    January 15, 2009 at 6:35 pm

  3. Hey,

    I just found this post and thought you might be interested in pysmug, my wrapper around the SmugMug API. I blog about it occasionally.

    thanks, brian

    Brian Zimmer

    March 25, 2009 at 5:41 pm

  4. This page definitely helped me to figure out what I was doing wrong with the API for smugmug, and has given me enough information to proceed with my planned tool. Thanks for posting this!

    Ben Pearson

    August 28, 2009 at 11:46 am


Leave a Reply