SyntaxHighlighter

Showing posts with label flickr. Show all posts
Showing posts with label flickr. Show all posts

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!

Thursday, October 4, 2012

Pleiades Machine Tags for Blog Posts? Yes!

So, a few minutes ago I noticed a new post in my feed reader from a blog I've admired for a while: Javier Andreu Pintado's Oppida Imperii Romani. I've thought for a long time that I ought to get in touch with him (we don't know each other from Adam as far as I know) and see if we could figure out a more-or-less automated way to get his posts to show up on the associated Pleiades pages.

Then it hit me:

Why can't we just use labels incorporating Pleiades IDs like we've been doing with machine tags on Flickr and query the Blogger API to get the associated posts?

Why not indeed. It turns out it just works.

To test, I added the string "pleiades:depicts=579885" as a label on my blog post from last December, "Pleiades, Flickr, and the Ancient World Image Bank" (Since that tag is used in an example in that post. I recognize that the blog post doesn't actually depict that place, which is what that label term ought to mean, but this is just a test).

Then I went to the Google APIs Explorer page for the Blogger "list posts" function (which I found by googling) and entered by blog's ID and the label string in the appropriate fields.



And, in a matter of milliseconds, I got back a JSON representation of my blog post.



So now I'm thinking we might explore the possibility of creating a widget on Pleiades place pages to feature blog posts tagged like this from selected blogs. It appears that, to execute the API queries against Blogger, we have to do them blog-by-blog with known IDs, but that's probably OK anyway so we can curate the process of blog selection and prevent spam.

It occurs to me that the Pelagios community might be interested in looking at this approach in order to build a gateway service to inject blog posts into the Pelagios network.

And while I'm name-checking, I wonder if any Wordpress aficionados out there can come up with a functionally equivalent mechanism.

Tuesday, December 20, 2011

Pleiades, Flickr, and the Ancient World Image Bank

Many of you are already aware that Pleiades, Flickr, and the Ancient World Image Bank have joined forces to link together online, open-access imagery and ancient geographical information. This blog post is intended to answer some lingering questions that users and potential contributors have been asking about the process.

Other Blog Posts
How to Construct a Pleiades Machine Tag

In Flickr, you add the machine tag the same way you add regular tags when editing an individual image or a set or group in the organizer. The machine tag should use the following syntax:
pleiades:TERM=#####
where "TERM" is one of the recognized terms (originally from the Concordia Thesaurus, aka the Graph of Ancient World Data, or GAWD, terms) listed below and "#####" is the numeric identifier of the Pleiades place you wish to associate with the photo.

You can get the identifier by visiting pleiades.stoa.org then searching for and finding the place. Copy the numeric portion of the URL of the place page and paste it into your tag.

So, for example, if I wanted to tag a photo that "depicts" ancient Athens (or a portion thereof), I'd visit Pleiades and search for Athens. I'd find this place page: http://pleiades.stoa.org/places/579885/. So, I'd grab 579885 and construct the following machine tag for use in Flickr:
pleiades:depicts=579885
Recognized Terms in Pleiades Machine Tags

TERMS for use in Pleiades machine tags should begin with a lowercase letter. The following TERMS are recognized in Pleiades machine tags:
depicts
the photo so tagged can be said to "depict" the referenced ancient place or a significant or exemplary portion thereof
(this term is equivalent to CIDOC CRM p62 "depicts")
findspot
the photo so tagged shows an object that was first found in modern times at the referenced ancient place
(this term is particularly useful for items now in museums or elsewhere, especially those no longer at the initial place of finding)
origin
the photo so tagged shows an object that is believed, with reasonably high certainty, to have been originally located or produced at the referenced ancient place
(this can differ from the findspot, as when an inscription or other object was moved in antiquity)
observedAt
the photo so tagged shows an object that was observed in modern times at the referenced ancient place
(the implication being that the place observed is neither the modern findspot nor the presumed original location; I suspect this term will rarely need to be used to link photos with Pleiades place resources)
where
the photo so tagged is related in some way to the referenced ancient place, but for some unspecified reason no more specific relationship can be asserted
(this term should not be used unless none of above terms are deemed to be appropriate)
place
this term is DEPRECATED; it was originally used in exploring the Pleiades machine tag idea (and is highlighted in my previous blog post). Its semantics are assumed to be equivalent to "where". If at all possible, photos carrying this tag should be updated to use one of the more specific terms above.

Please note that pleiades:places=##### (i.e., with a plural) is not a recognized machine tag. Its behavior in Flickr or Pleiades is undefined. So is the behavior of any Pleiades machine tag with a misspelling or a term not included in the list above. ("finspot" seems to be a popular typo at present).

Any photo tagged with the proper Pleiades machine tag syntax and one of the terms above will be noticed by Pleiades and picked up in the summary counts and links on individual place resource pages. In order for a photo to be considered for the Pleiades Places group on Flickr (and therefore as a "portrait image" for a Pleiades place), the photo must be tagged with a pleiades:depicts tag.

How do I Add Pleiades Machine Tags Quickly?

It would be unreasonable of us to ask a prolific photographer with an extensive, well-tagged collection already on Flickr to go through an individually add appropriate machine tags by hand. Fortunately, Flickr provides a mechanism for easy batch editing of tags.
  1. Suppose that you have tagged a large number of your photos with the name of the ancient site (e.g., Halicarnassus)
  2. Visit the following link: http://www.flickr.com/photos/me/alltags/; it will give you an alphabetical list of all your tags.
  3. Find the name of the site and click on the corresponding link. You'll see all the photos thus tagged in your photostream.
  4. Find and click on the "Change this tag" link (Really. Skip "Edit these in a batch" link for now).
  5. Insert the cursor after your existing tag string, type a space, then type or paste in the desired Pleiades machine tag (read the fine print on that page for an extended explanation of what's going on).
  6. Click the "save" button. Flickr will go off and add the new tag to all those images at once.
  7. If you'd rather be more selective about which photos you want to add a tag to, you can choose the "Edit these in a batch" link I told you to skip above, then paste the Pleiades machine tag into the tag lists associated with only those images you wish to update.
Why Can't We Just Use Geotagging That's Already in the Photos?

Some photographers have geotagged their photos, either using a GPS-enabled digital camera or some method of post-processing. I was recently asked on twitter why we're putting people to all the trouble above when we could just use the geotagging? There are several reasons:
  • Not everybody's photos are machine-tagged.
  • Many ancient sites are coincident with urban areas (or areas of natural beauty or places where someone took a picture of their dog) and so mere proximity to an ancient site can't be interpreted as indicating a given photo is relevant to a nearby Pleiades place.
  • Horizontal precision and accuracy can vary widely in geotagged photos as a function of the geotagging method used and the interests and skill of the person doing the geotagging. As a result, a photo might be geotagged at a location closer to another, unrelated pleiades place.
  • The horizontal precision and accuracy of Pleiades coordinates also varies widely given the varying sources from which it derives and the subsequent coordinate extraction methods. This makes the process of proximity correlation even more fraught.
This is not to say that we're not interested in exploring uses for geotagged photos in Flickr (or supporting a geotag-your-photo-using-Pleiades-coordinates tool), but I hope this discussion helps explain why we like the machine-tag approach for indicating relevance.

Please let me know, via comments here, if you have additional questions or suggestions. 

Saturday, September 10, 2011

Feeds of Flickr Photos Depicting Pleiades Places

Some months ago, ISAW started adding Pleiades machine tags to the Ancient World Image Bank (AWIB) photos we've been uploading to Flickr. This post will explain what that means, how it might be useful to you and how you can add Pleiades machine tags to your own photos so we can find out about them.

Updated: 8:45pm EDT, 10 September 2011 (changes highlighted in orange).
Updated: 10:43am EST, 20 December 2011 (some of what's here is now superseded by recent developments; see further this new post: Pleiades, Flickr, and the Ancient World Image Bank)

Pleiades Machine Tags

Pleiades is a collaborative, open-access digital gazetteer for the ancient world. AWIB is an open-access publication that uses the Flickr photo-sharing site to publish free, reusable photos of ancient sites and artifacts. Machine tags are an extension to Flickr's basic tag-this-photo functionality that "use a special syntax to define extra information about a tag" (Aaron Straup Cope, "Ladies and Gentlemen: Machine Tags," 24 January 2007).

A Pleiades machine tag looks like this:
pleiades:place=795868
where 795868 is the stable identifier portion of a Pleiades Uniform Resource Identifier (URI).  In this example, the URI corresponding to the machine tag above is:
http://pleiades.stoa.org/places/795868
Note what's in common between the machine tag and the URI (highlighted in yellow).

What Pleiades Machine Tags Are Good For

The Flickr API makes it possible to request lists of machine-tagged photos in the RSS webfeed format. So, to get a list of all photos in Flickr that are tagged with the example machine tag above, pop this into your feed reader:
http://api.flickr.com/services/feeds/photos_public.gne?tags=pleiades%3Aplace=795868&lang=en-us&format=rss_200
The same results can be viewed in HTML in a browser by resolving the following:
http://www.flickr.com/photos/tags/pleiades:place=795868
To get a list of all photos in Flickr that are tagged with any Pleiades machine tag, try this (the API syntax supports wildcards!):
http://api.flickr.com/services/feeds/photos_public.gne?tags=pleiades%3Aplace%3D&lang=en-us&format=rss_200
The same results, viewed in HTML on the Flickr site:
http://www.flickr.com/photos/tags/pleiades:place
Feeds like these aren't just for feed readers anymore. You can add user-interface widgets to your blog or website to summarize the latest content for your readers (check out the right-hand column in this blog). You can hook up services like Networked Blogs or Twitterfeed to pass on the latest changes to your Facebook friends or Twitter followers. If you've got a web-facing numismatic database that you've already linked up with Pleiades for the mint locations, you could write custom code to pull a corresponding picture of the ancient site into your web interface (say, alongside the map you've already got).

Add Pleiades Machine Tags to Your Own Photos on Flickr

Many of you have been taking amazing photos of ancient sites and artifacts for years. Many of you have posted some of them to Flickr and shared them with great groups like Chiron, Visibile Parlare - Greek Inscriptions and Visibile Parlare - Latin Inscriptions. If you'd like these photos to appear in queries and feeds (like those described above), right alongside the photos that we're publishing via AWIB, all you have to do is add the appropriate Pleiades machine tags in Flickr. Just look up your site on Pleiades, copy the numeric ID from the URI in your browser's location bar, append it to the string "pleiades:place=" and tag your Flickr photos with it. In this way, you can help us improve findability of good photos of ancient sites and the artifacts found there for everyone on the web. Who knows ... maybe enough people will join us in this effort that we can someday get the Flickr development team to give Pleiades machine tags some extra love.

Kudos to: