That Flickr uploading script I wrote has been sitting unimproved for over a month. I’ve been using it regularly, and it works fine, but I’ve always wanted it to have a little more flexibility, so tonight I added an option for making the photos public when they upload.

The added option isn’t a big deal in itself—although it will save me some time—but to include it I had to put in a section of code for handling command line options. That section can be easily expanded in the future to add more options, like choosing or creating a photo set for the images to go in, adding tags and a description, etc.

I’ve added up2flickr to my Flicker stuff GitHub repository, which also has some other scripts and a few TextExpander snippets that work with Flickr. Here’s the code:

python:
1:  #!/usr/bin/python
2:
3:  from flickrapi import FlickrAPI
4:  from os.path import basename, splitext
5:  import sys
6:  import getopt
7:
8:  usage = """Usage: up2flickr [options] [files]
9:
10:  Options:
11:    -p   make the photos public
12:    -h   show this help message
13:
14:  Upload image files to Flickr. The images will be private to
15:  friends and family by default."""
16:
17:  # Flickr parameters
19:  key = 'Get key from Flickr'
20:  secret = 'Get secret from Flickr'
21:
22:  # Get the command line options.
23:  try:
24:    options, filenames = getopt.getopt(sys.argv[1:], 'ph')
25:  except getopt.GetoptError, err:
26:    print str(err)
27:    sys.exit(2)
28:
29:  # Set the option values.
30:  public = 0       # default
31:  for o, a in options:
32:    if o == '-p':
33:      public = 1
34:    else:
35:      print usage
36:      sys.exit()
37:
38:  # Upload the files on the command line.
39:  flickr = FlickrAPI(api_key=key, secret=secret)
40:
41:  for fn in filenames:

The usage string near the top describes how up2flickr works, and command line options are handled by the getopt functions in Lines 23-27 and the loop in Lines 30-36. Python has fancier libraries for handling and documenting the options, but I find them actually harder to use because of all their features. Also, the Python folks can’t seem to decide which one they want you to use. The library they were touting just a few years ago is now deprecated. I’m betting plain old getopt will always be part of the standard Python distribution.