SyntaxHighlighter

Wednesday, August 21, 2013

Get a List of Machine Tags from the Flickr API

I want to know what Pleiades machine tags are in use on photos throughout Flickr (more background here). I thought I'd learn how to ask for that information from the Flickr API via a script. I requested and got an API key (see http://www.flickr.com/help/api/). I set up a Python virtual environment and git repository for the project. I went looking for Python code that already implemented interaction with the API and settled (somewhat arbitrarily) on Beej's Python Flickr API kit (now maintained by Sybren Stüvel). Then used pip install flickrapi to get the package.

Here's a command-line session running the script and showing its output:

(pleiades-flickr)darkstar:pleiades-flickr paregorios$ python src/listptags.py 
pleiades:atteststo is used on 15 photos in Flickr
pleiades:denotes is used on 1 photos in Flickr
pleiades:depcits is used on 2 photos in Flickr
pleiades:depicts is used on 7229 photos in Flickr
pleiades:findspot is used on 2197 photos in Flickr
pleiades:finspot is used on 2 photos in Flickr
pleiades:foundat is used on 1 photos in Flickr
pleiades:observedat is used on 3 photos in Flickr
pleiades:origin is used on 225 photos in Flickr
pleiades:place is used on 970 photos in Flickr
pleiades:places is used on 19 photos in Flickr
pleiades:where is used on 119 photos in Flickr
(pleiades-flickr)darkstar:pleiades-flickr paregorios$ 


Here's the code (version at github):

#!/usr/bin/env python
"""
A Flickr tag bot
"""

import argparse
import flickrapi
import json
import logging as l
import os
import sys
import traceback

from myflickr import API_KEY, NAMESPACE_DEFAULT

SCRIPT_DESC = "poll machine tags from flickr"

def main ():
    """ Unleash the bot! """

    global args
    global l

    flickr = flickrapi.FlickrAPI(API_KEY)
    resp = flickr.machinetags_getPairs(namespace=args.namespace, format="json")
    if resp[:14] == "jsonFlickrApi(":
        jstr = resp[14:-1]
        j = json.loads(jstr)
        ptags = [(p['_content'], p['usage']) for p in j['pairs']['pair']]
        for ptag in ptags:
            print "%s is used on %s photos in Flickr" % ptag


if __name__ == "__main__":
    try:
        parser = argparse.ArgumentParser(description=SCRIPT_DESC, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        parser.add_argument ("-n", "--namespace", default=NAMESPACE_DEFAULT, help="namespace to use in requesting machine tags")
        parser.add_argument ("-v", "--verbose", action="store_true", default=False, help="verbose output")
        args = parser.parse_args()
        if args.verbose:
            l.basicConfig(level=l.DEBUG)
        else:
            l.basicConfig(level=l.WARNING)
        main()
        sys.exit(0)
    except KeyboardInterrupt, e: # Ctrl-C
        raise e
    except SystemExit, e: # sys.exit()
        raise e
    except Exception, e:
        print "ERROR, UNEXPECTED EXCEPTION"
        print str(e)
        traceback.print_exc()
        os._exit(1)


Comments, questions, and constructive criticism welcomed!

No comments: