Using the flickr XML/API as a source of RSS feeds.
You may know that I seek from time to time royalty free pictures on flickr.com for my other personal blog. The flickr API can be used to search for these images but it is currently not possible to generate a RSS feed to be alerted when a new image is posted on flickr.
The following XSLT stylesheet transforms the XML returned by www.flickr.com to a RSS feed (latest source: https://github.com/lindenb/xslt-sandbox/blob/master/stylesheets/flickr/flickr2rss.xsl ):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:dc="http://purl.org/dc/elements/1.1/" version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:param name="title">No Title</xsl:param>
<xsl:template match="/">
<rss version="2.0">
<channel>
<title><xsl:value-of select="$title"/></title>
<link>http://www.flickr.com</link>
<description><xsl:value-of select="$title"/></description>
<pubDate><xsl:value-of select="date:date-time()"/></pubDate>
<lastBuildDate><xsl:value-of select="date:date-time()"/></lastBuildDate>
<generator>http://www.flickr.com/</generator>
<xsl:apply-templates select="rsp/photos/photo"/>
</channel>
</rss>
</xsl:template>
<xsl:template match="photo">
<item>
<title><xsl:value-of select="@title"/> : <xsl:value-of select="$title"/></title>
<link>http://www.flickr.com/photos/<xsl:value-of select="@owner"/>/<xsl:value-of select="@id"/>/</link>
<pubDate><xsl:value-of select="@datetaken"/></pubDate>
<author>
<xsl:choose>
<xsl:when test="@ownername"><xsl:value-of select="@ownername"/></xsl:when>
<xsl:otherwise><xsl:value-of select="@owner"/></xsl:otherwise>
</xsl:choose>
</author>
<guid isPermaLink="false">http://www.flickr.com/photos/<xsl:value-of select="@owner"/>/<xsl:value-of select="@id"/>/</guid>
<description>
<xsl:text><p><img </xsl:text>
<xsl:choose>
<xsl:when test="@height_s and @width_s">
<xsl:text> width='</xsl:text>
<xsl:value-of select="@width_s"/>
<xsl:text>' height='</xsl:text>
<xsl:value-of select="@height_s"/>
<xsl:text>' </xsl:text>
</xsl:when>
<xsl:when test="@height_m and @width_m">
<xsl:text> width='</xsl:text>
<xsl:value-of select="@width_m"/>
<xsl:text>' height='</xsl:text>
<xsl:value-of select="@height_m"/>
<xsl:text>' </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:text> src='</xsl:text>
<xsl:choose>
<xsl:when test="@url_s"><xsl:value-of select="@url_s"/></xsl:when>
<xsl:otherwise>http://farm<xsl:value-of select="@farm"/>.staticflickr.com/<xsl:value-of select="@server"/>/<xsl:value-of select="@id"/>_<xsl:value-of select="@secret"/>_s.jpg</xsl:otherwise>
</xsl:choose>
<xsl:text>' /></p></xsl:text>
</description>
</item>
</xsl:template>
</xsl:stylesheet>
xmlns:date="http://exslt.org/dates-and-times"
xmlns:dc="http://purl.org/dc/elements/1.1/" version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:param name="title">No Title</xsl:param>
<xsl:template match="/">
<rss version="2.0">
<channel>
<title><xsl:value-of select="$title"/></title>
<link>http://www.flickr.com</link>
<description><xsl:value-of select="$title"/></description>
<pubDate><xsl:value-of select="date:date-time()"/></pubDate>
<lastBuildDate><xsl:value-of select="date:date-time()"/></lastBuildDate>
<generator>http://www.flickr.com/</generator>
<xsl:apply-templates select="rsp/photos/photo"/>
</channel>
</rss>
</xsl:template>
<xsl:template match="photo">
<item>
<title><xsl:value-of select="@title"/> : <xsl:value-of select="$title"/></title>
<link>http://www.flickr.com/photos/<xsl:value-of select="@owner"/>/<xsl:value-of select="@id"/>/</link>
<pubDate><xsl:value-of select="@datetaken"/></pubDate>
<author>
<xsl:choose>
<xsl:when test="@ownername"><xsl:value-of select="@ownername"/></xsl:when>
<xsl:otherwise><xsl:value-of select="@owner"/></xsl:otherwise>
</xsl:choose>
</author>
<guid isPermaLink="false">http://www.flickr.com/photos/<xsl:value-of select="@owner"/>/<xsl:value-of select="@id"/>/</guid>
<description>
<xsl:text><p><img </xsl:text>
<xsl:choose>
<xsl:when test="@height_s and @width_s">
<xsl:text> width='</xsl:text>
<xsl:value-of select="@width_s"/>
<xsl:text>' height='</xsl:text>
<xsl:value-of select="@height_s"/>
<xsl:text>' </xsl:text>
</xsl:when>
<xsl:when test="@height_m and @width_m">
<xsl:text> width='</xsl:text>
<xsl:value-of select="@width_m"/>
<xsl:text>' height='</xsl:text>
<xsl:value-of select="@height_m"/>
<xsl:text>' </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:text> src='</xsl:text>
<xsl:choose>
<xsl:when test="@url_s"><xsl:value-of select="@url_s"/></xsl:when>
<xsl:otherwise>http://farm<xsl:value-of select="@farm"/>.staticflickr.com/<xsl:value-of select="@server"/>/<xsl:value-of select="@id"/>_<xsl:value-of select="@secret"/>_s.jpg</xsl:otherwise>
</xsl:choose>
<xsl:text>' /></p></xsl:text>
</description>
</item>
</xsl:template>
</xsl:stylesheet>
To invoke this stylesheet and generate the RSS feed, I wrote the following small quick'n dirty cgi-script "flickr.cgi". The script was made executable, installed into my public_html/cgi-bin directory. It also requires to get an API key from flickr.
#!/bin/sh echo "Content-Type: application/rss+xml" echo APIKEY=12345678910 TAGS=`echo ${QUERY_STRING}|tr "?&" "\n" | egrep '^tags=' | cut -d '=' -f 2 | sed 's/,/%2C/g'` TEXT=`echo ${QUERY_STRING}|tr "?&" "\n" | egrep '^text=' | cut -d '=' -f 2 | tr " " "+"` curl -s "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${APIKEY}&tags=${TAGS}&format=rest&extras=url_s,date_upload,date_taken,icon_server,owner_name&tag_mode=all&per_page=20&license=2,4,1,5,7&text=${TEXT}" | xsltproc --novalid --stringparam title "${TAGS} ${TEXT}" flickr2rss.xsl -
I can know add some new RSS feeds into thunderbird and receive the new items: for example http://localhost/~me/cgi-bin/flickr.cgi?tags=science
<rss version="2.0" xmlns:date="http://exslt.org/dates-and-times" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" >
<channel>
<title>science </title>
<link>http://www.flickr.com</link>
<description>science </description>
<pubDate>2012-07-09T23:09:58+02:00</pubDate>
<lastBuildDate>2012-07-09T23:09:58+02:00</lastBuildDate>
<generator>http://www.flickr.com/</generator>
<item>
<title>2012 NOAA HABs Forecast : science </title>
<link>http://www.flickr.com/photos/41398337@N07/7537830696/</link>
<pubDate>2012-07-05 10:16:56</pubDate>
<author>Ohio Sea Grant and Stone Laboratory</author>
<guid isPermaLink="false">http://www.flickr.com/photos/41398337@N07/7537830696/</guid>
<description><p><img width='240' height='160' src='http://farm8.staticflickr.com/7252/7537830696_2a66783e70_m.jpg' /></p></description>
</item>
<item>
<title>2012 NOAA HABs Forecast : science </title>
<link>http://www.flickr.com/photos/41398337@N07/7537829638/</link>
<pubDate>2012-07-05 10:18:37</pubDate>
<author>Ohio Sea Grant and Stone Laboratory</author>
<guid isPermaLink="false">http://www.flickr.com/photos/41398337@N07/7537829638/</guid>
<description><p><img width='240' height='160' src='http://farm8.staticflickr.com/7252/7537829638_4c6dd535b4_m.jpg' /></p></description>
</item>
<item>
(...)
</item>
</channel>
</rss>
<channel>
<title>science </title>
<link>http://www.flickr.com</link>
<description>science </description>
<pubDate>2012-07-09T23:09:58+02:00</pubDate>
<lastBuildDate>2012-07-09T23:09:58+02:00</lastBuildDate>
<generator>http://www.flickr.com/</generator>
<item>
<title>2012 NOAA HABs Forecast : science </title>
<link>http://www.flickr.com/photos/41398337@N07/7537830696/</link>
<pubDate>2012-07-05 10:16:56</pubDate>
<author>Ohio Sea Grant and Stone Laboratory</author>
<guid isPermaLink="false">http://www.flickr.com/photos/41398337@N07/7537830696/</guid>
<description><p><img width='240' height='160' src='http://farm8.staticflickr.com/7252/7537830696_2a66783e70_m.jpg' /></p></description>
</item>
<item>
<title>2012 NOAA HABs Forecast : science </title>
<link>http://www.flickr.com/photos/41398337@N07/7537829638/</link>
<pubDate>2012-07-05 10:18:37</pubDate>
<author>Ohio Sea Grant and Stone Laboratory</author>
<guid isPermaLink="false">http://www.flickr.com/photos/41398337@N07/7537829638/</guid>
<description><p><img width='240' height='160' src='http://farm8.staticflickr.com/7252/7537829638_4c6dd535b4_m.jpg' /></p></description>
</item>
<item>
(...)
</item>
</channel>
</rss>
That's it,
Pierre
No comments:
Post a Comment