A Flickr macro for MoinMoin



Would you like to include Flickr photos in your MoinMoin wiki pages? Here’s a MoinMoin macro that allows you to do just that.

The code is a little ugly, but I was inspired by this affirmation of the “release early, release often” philosophy to make the source available.

Slide on over to my wiki to see the my design notes for this macro.


#!/usr/bin/env python

"""
    MoinMoin - Flickr Macro

    A macro to embed Flickr photos on wiki pages.

    Usage: <>

    Parameters:

      photo_id: The ID of the photo you want to embed. You can find
                this within most Flickr URLS.
      size:     One of "Square", "Thumbnail", "Small", "Medium",
                or "Original"

    @copyright: 2009 by MoinMoin:TylerOderkirk
    @license: GNU GPL.
"""

from MoinMoin import wikiutil
import flickrapi

def execute(macro, args):

    argParser = wikiutil.ParameterParser("%(photo_id)i%(size)s")
    argDict = argParser.parse_parameters(args)
    # print argDict
    # photoid
    photoID = argDict[1]["photo_id"] # "444769890"
    desiredSize = argDict[1]["size"] # "Medium"
    # size Square, Thumbnail, Small, Medium, Original
    # TODO: add img caching/backup
    # TODO: logging as to not overuse API key.
    # TODO: store api_key somewhere else. wikiconfig.py?

    api_key = 'BEEEEEEEEEEEEEEEF'

    flickr = flickrapi.FlickrAPI(api_key)
    sizes = flickr.photos_getSizes(photo_id=photoID)
    info = flickr.photos_getInfo(photo_id=photoID)

    photoSourceURL = "asdf"
    for i in sizes.sizes[0].size:
        # print i['label']
        if i['label'] == desiredSize:
            photoSourceURL = i['source']

    # TODO: ensure this is an URL of type 'photopage'. ref
    # http://www.flickr.com/services/api/flickr.photos.getInfo.html
    photoPageURL = info.photo[0].urls[0].url[0].text
    photoDescription = info.photo[0].description[0].text
    # TODO: XSS here?
    photoTitle = info.photo[0].title[0].text
    photoAuthorUsername = info.photo[0].owner[0]['username']

    # print photoSourceURL, photoPageURL, photoDescription

    # TODO: wrap/truncate long titles. maybe put credits on new line?
    # TODO: potentially include description instead of title

    # TODO: use moin markup for table instead of raw html
    # ref. http://moinmo.in/MacroMarket/MiniPage
    # ref. http://unsyncopated.com/BrainSolvent/MoinMoin Flickr
    #       Macro?action=diff&rev2=29&rev1=28
    #  pypaper.py
    # Thanks to NirSoffer for the CSS
    # ref. http://moinmo.in/HelpOnTables
    return '
<div>
'\
     '
'\
     '<a href="%s"><img title="%s" src="%s" alt="%s" />'\
     '</a>
%s (Flickr image by'\
     '%s)
<table style="float: right; font-size: 0.85em; '\&lt;br /&gt;
     'background: #eeeeee; margin: 0 0 1em 1em;">
<tbody></tbody>
<tbody></tbody>
</table>
</div>
' % (photoPageURL,
     photoTitle, photoSourceURL,photoTitle, photoTitle,
     photoAuthorUsername )

if __name__ == "__main__":
    print( execute( 0, "photo_id=2088619558,size=Small" ) )4153379126

Caveats

  • Beware copyrighted photos. Luckily, Flickr allows you to specify license criteria on their “advanced search” page. I use yubnub’s “flcc” command to do this.
  • If the macro experiences an error at runtime and you have MoinMoin tracebacks enabled the tracebacks will contain your Flickr API key. Patches welcome `;]`

Update 3-1-09: Flickr’s API just went down. Seems like a good time to implement exception handling `:]`. Email me for the updated code.


Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!