Extract data about a Flickr photo

A command-line tool for getting the URL and metadata given a Flickr URL.
Contents
Related files

The use case

Because Flickr has so many great, free images in its vast Commons gallery, I often find myself wanting to copy and embed stock images from the Flickr website, and to include attribution and link-backs for their creators.

However, besides the tediousness of copying the title and author information, the direct URLs to the image files require two clicks to get to. So I’ve written a script that, given a Flickr photo page URL, extracts the important metadata, including the URL of the biggest available photo size.

In this example, I’ve chosen to output the data in a non-structured, human-readable way. However, I’ve also included boilerplate HTML for embedding the photo and metadata that I can paste into a blog post.

The routine

  1. Visit Flickr photo URL in browser

  2. Copy-paste title

  3. Copy-paste author name

  4. Click the “Download this photo” link

  5. Click “Original size” link

  6. Right-click photo to get its direct URL

Demo

Given a Flickr photo page URL:

https://www.flickr.com/photos/zokuga/14392889220/

image

Run flickurl.py like so:

python flickurl.py https://www.flickr.com/photos/zokuga/14392889220/

This is what the output looks like:

image

The boilerplate HTML image link is pretty basic, consisting only of hot-linking the Flickr image file, wrapped inside a clickable-link back to the Flickr photo page, and alt text. I’ve also chosen by default to pick the first photo size that has both width and height under 1600 pixels, so that we don’t hot-link to the URL of a 8000x6000-pixel wide photo:

<a href="https://www.flickr.com/photos/zokuga/14392889220/" title="Go to Flickr page for: July 4th Fireworks, Brooklyn Bridge, 2014 by: Dan Nguyen"><img src="https://farm4.staticflickr.com/3917/14392889220_518de64af0_h.jpg" alt="July 4th Fireworks, Brooklyn Bridge, 2014 by: Dan Nguyen"></a>

And here’s the result of that HTML, note that the image is clickable, and that this site’s CSS keeps it from being displayed at its full width on the page.

July 4th Fireworks, Brooklyn Bridge, 2014 by: Dan Nguyen

You could further alter this script to print the HTML tags for displaying a caption.

Code explanation

The flickurl.py is pretty straightforward. Its verbosity is due to the verbose nature of Flickr’s JSON responses. For example, this is what the title attribute looks like in the photos.getInfo response:

{
  "title": {
    "_content": "July 4th Fireworks, Brooklyn Bridge, 2014"
  }
}

So the Python needed to extract that value is:

title = photo_info['title']['_content']

Colorful output

To make the program’s output easier to read, I used the colorama package and created my own function for printing out the headers to the output:

from colorama import Fore, Style, Back, init as initcolorama
initcolorama()
# ...
def cprint(*txt):
    t = ' '.join([str(x) for x in txt])
    print(Back.BLACK + Fore.CYAN + Style.BRIGHT + t + Style.RESET_ALL)

image

colorama is a pretty cool library, here’s a screenshot from the docs showing all the different ways it can color text:

Github image of colorama output

Flickr APIs

This script uses two endpoints:

Sample flickr.photos.getInfo JSON

{
  "rotation": 0,
  "urls": {
    "url": [
      {
        "_content": "https://www.flickr.com/photos/zokuga/14392889220/",
        "type": "photopage"
      }
    ]
  },
  "originalsecret": "192bbf7c1c",
  "visibility": {
    "ispublic": 1,
    "isfamily": 0,
    "isfriend": 0
  },
  "publiceditability": {
    "canaddmeta": 1,
    "cancomment": 1
  },
  "owner": {
    "location": "Manhattan, U.S.A",
    "path_alias": "zokuga",
    "username": "Dan Nguyen @ New York City",
    "nsid": "32451477@N02",
    "iconfarm": 9,
    "realname": "Dan Nguyen",
    "iconserver": "8475"
  },
  "people": {
    "haspeople": 0
  },
  "media": "photo",
  "comments": {
    "_content": "13"
  },
  "server": "3917",
  "id": "14392889220",
  "usage": {
    "candownload": 1,
    "canprint": 0,
    "canblog": 0,
    "canshare": 1
  },
  "secret": "6ef01a0fa4",
  "farm": 4,
  "safety_level": "0",
  "description": {
    "_content": "Woooo! What a show!\n\nThe fireworks barges had to move down river because of the currents, which meant that those of us on the Promenade missed out on about 90% of the show...but we still had a great view of the bridge-launched pyrotechnics. You can see all my other shots in this album: <a href=\"https://www.flickr.com/photos/zokuga/sets/72157645547685522\">www.flickr.com/photos/zokuga/sets/72157645547685522</a>"
  },
  "editability": {
    "canaddmeta": 0,
    "cancomment": 0
  },
  "license": "4",
  "geoperms": {
    "ispublic": 1,
    "iscontact": 0,
    "isfamily": 0,
    "isfriend": 0
  },
  "originalformat": "jpg",
  "dateuploaded": "1404571905",
  "views": "26832",
  "tags": {
    "tag": [
      {
        "_content": "gothamist",
        "id": "32431129-14392889220-22328",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "Gothamist"
      },
      {
        "_content": "july4th",
        "id": "32431129-14392889220-31187",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "July 4th"
      },
      {
        "_content": "nyc",
        "id": "32431129-14392889220-106",
        "author": "32451477@N02",
        "machine_tag": false,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "NYC"
      },
      {
        "_content": "fireworks",
        "id": "32431129-14392889220-4626",
        "author": "32451477@N02",
        "machine_tag": false,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "fireworks"
      },
      {
        "_content": "summer",
        "id": "32431129-14392889220-245",
        "author": "32451477@N02",
        "machine_tag": false,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "summer"
      },
      {
        "_content": "independenceday",
        "id": "32431129-14392889220-34515",
        "author": "32451477@N02",
        "machine_tag": false,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "Independence Day"
      },
      {
        "_content": "bridge",
        "id": "32431129-14392889220-127",
        "author": "32451477@N02",
        "machine_tag": false,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "bridge"
      },
      {
        "_content": "brooklynbridge",
        "id": "32431129-14392889220-15688",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "Brooklyn Bridge"
      },
      {
        "_content": "brooklynbridgepark",
        "id": "32431129-14392889220-506504",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "Brooklyn Bridge Park"
      },
      {
        "_content": "brookyln",
        "id": "32431129-14392889220-191498",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "Brookyln"
      },
      {
        "_content": "dumbo",
        "id": "32431129-14392889220-7703",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "DUMBO"
      },
      {
        "_content": "newyorkcity",
        "id": "32431129-14392889220-109",
        "author": "32451477@N02",
        "machine_tag": 0,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "New York City"
      },
      {
        "_content": "night",
        "id": "32431129-14392889220-1213",
        "author": "32451477@N02",
        "machine_tag": false,
        "authorname": "Dan Nguyen @ New York City",
        "raw": "night"
      }
    ]
  },
  "isfavorite": 0,
  "location": {
    "country": {
      "place_id": "nz.gsghTUb4c2WAecA",
      "_content": "United States",
      "woeid": "23424977"
    },
    "accuracy": "16",
    "locality": {
      "place_id": ".skCPTpTVr.Q3WKW",
      "_content": "New York",
      "woeid": "2459115"
    },
    "context": "0",
    "place_id": "CldNEG9QUL_lkqUcYA",
    "region": {
      "place_id": "ODHTuIhTUb75gdBu",
      "_content": "New York",
      "woeid": "2347591"
    },
    "longitude": "-73.992705",
    "latitude": "40.704432",
    "woeid": "12589335",
    "county": {
      "place_id": "CldNEG9QUL_lkqUcYA",
      "_content": "Brooklyn",
      "woeid": "12589335"
    }
  },
  "title": {
    "_content": "July 4th Fireworks, Brooklyn Bridge, 2014"
  },
  "notes": {
    "note": []
  },
  "dates": {
    "takengranularity": "0",
    "posted": "1404571905",
    "takenunknown": 0,
    "lastupdate": "1436192865",
    "taken": "2014-07-04 21:49:26"
  }
}

[github] [raw]

Sample flickr.photos.getSizes JSON

{
  "stat": "ok",
  "sizes": {
    "candownload": 1,
    "canblog": 0,
    "size": [
      {
        "width": 75,
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/sq/",
        "height": 75,
        "label": "Square",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_s.jpg",
        "media": "photo"
      },
      {
        "width": "150",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/q/",
        "height": "150",
        "label": "Large Square",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_q.jpg",
        "media": "photo"
      },
      {
        "width": "100",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/t/",
        "height": "67",
        "label": "Thumbnail",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_t.jpg",
        "media": "photo"
      },
      {
        "width": "240",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/s/",
        "height": "160",
        "label": "Small",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_m.jpg",
        "media": "photo"
      },
      {
        "width": "320",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/n/",
        "height": 213,
        "label": "Small 320",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_n.jpg",
        "media": "photo"
      },
      {
        "width": "500",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/m/",
        "height": "333",
        "label": "Medium",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4.jpg",
        "media": "photo"
      },
      {
        "width": "640",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/z/",
        "height": "427",
        "label": "Medium 640",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_z.jpg",
        "media": "photo"
      },
      {
        "width": "800",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/c/",
        "height": 534,
        "label": "Medium 800",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_c.jpg",
        "media": "photo"
      },
      {
        "width": "1024",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/l/",
        "height": "683",
        "label": "Large",
        "source": "https://farm4.staticflickr.com/3917/14392889220_6ef01a0fa4_b.jpg",
        "media": "photo"
      },
      {
        "width": "1600",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/h/",
        "height": 1067,
        "label": "Large 1600",
        "source": "https://farm4.staticflickr.com/3917/14392889220_518de64af0_h.jpg",
        "media": "photo"
      },
      {
        "width": "2048",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/k/",
        "height": 1365,
        "label": "Large 2048",
        "source": "https://farm4.staticflickr.com/3917/14392889220_696c83d60c_k.jpg",
        "media": "photo"
      },
      {
        "width": "4945",
        "url": "https://www.flickr.com/photos/zokuga/14392889220/sizes/o/",
        "height": "3297",
        "label": "Original",
        "source": "https://farm4.staticflickr.com/3917/14392889220_192bbf7c1c_o.jpg",
        "media": "photo"
      }
    ],
    "canprint": 0
  }
}

[github] [raw]