#!/usr/bin/python # Fix flickr dates # Hugo Haas -- mailto:hugo@larve.net -- http://larve.net/people/hugo/ # License: GPLv2 import sys import libxml2 import urllib import getopt import time import os.path # Beej's Python Flickr API # http://beej.us/flickr/flickrapi/ from flickrapi import FlickrAPI # Gotten from Flickr flickrAPIKey = '465b969bc54ba23b42cb6c37f52fe6f2' flickrSecret = '82f9673c10b93eb4' class DateFix: def __init__(self, key, secret, uid, browser = "lynx"): """Instantiates a DateFix object An API key is needed, as well as an API secret and a user id. A browser can be specified to be used for authorizing the program to access the user account.""" self.__flickrAPIKey = key self.__flickrSecret = secret # Get authentication token self.fapi = FlickrAPI(self.__flickrAPIKey, self.__flickrSecret) self.token = self.fapi.getToken(browser=browser, perms="write") self.flickrUserId = uid def __testFailure(self, rsp): """Returns whether the previous call was successful""" if rsp['stat'] == "fail": print "Error!" return True else: return False def setDate(self, date, id): print "Setting taken date of %s to %s" % (id, date) rsp = self.fapi.photos_setDates(api_key=self.__flickrAPIKey, auth_token=self.token, user_id = self.flickrUserId, photo_id = id, date_taken = date) if self.__testFailure(rsp): sys.exit(1) def usage(): """Command line interface usage""" print "Usage: DateFix.py -i -d [photo ids]" print "Sets date taken of photos" def main(): """Command-line interface""" # Default options flickrUserId = None date = None browser = 'opera' # Parse command line try: opts, args = getopt.getopt(sys.argv[1:], "hd:i:", ["help"]) except getopt.GetoptError: usage() sys.exit(2) for o, a in opts: if o in ('-h', '--help'): usage() sys.exit(0) if o == '-i': flickrUserId = a if o == '-d': date = a if o == '-b': browser = a # Check that we have a user id specified if flickrUserId == None: print "You need to specify a Flickr Id" sys.exit(1) # Check that we have a date specified if date == None: print "You need to specify a date" sys.exit(1) datefix = DateFix(flickrAPIKey, flickrSecret, flickrUserId, browser) for p in args: datefix.setDate(date, p) if __name__ == "__main__": main()