From: "andrew cooke" <andrew@...>
Date: Sun, 15 Jun 2008 03:27:10 -0400 (CLT)
I am rewriting my website (not yet public) and wanted to include the
latest images from my flickr page. At first I assumed this would be AJAX,
but it turns out to be simpler to just use an iframe (saves you rewriting
the DOM yourself).
The following code, run as a cgi, pulls details of the last 4 images from
my flickr account and generates the HTML for the iframe. To re-use you
need to add your own api_key (and change parameters to get the user you
want).
I didn't use XSL because that's not in the standard Python libs and this
has to run on my ISP.
#!/usr/bin/python2.5
from httplib import HTTPConnection
from re import compile
photos = compile(r'(<photo [^>]+>)(.*)')
attributes = compile(r'([a-z]+)="([^"]+)"(.*)')
cnx = HTTPConnection("api.flickr.com")
cnx.request("GET",
"/services/rest/?method=flickr.people.getPublicPhotos&api_key=...&user_id=...&per_page=4")
rsp = cnx.getresponse()
if 200 == rsp.status:
print "Content-type: text/html"
print
print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link type="text/css" rel="stylesheet" href="index.css" title="default"/>
<title>
andrew cooke
</title>
</head>
<body>"""
xml = rsp.read()
xml = xml.replace('\r', ' ')
xml = xml.replace('\n', ' ')
while xml:
match = photos.search(xml)
if match:
(photo, xml) = photos.search(xml).groups()
attrs = {}
while photo:
match = attributes.search(photo)
if match:
photo = match.group(3)
attrs[match.group(1)] = match.group(2)
else:
photo = ''
print '<a
href="http://www.flickr.com/photos/acooke/%(id)s"><img
src="http://farm%(farm)s.static.flickr.com/%(server)s/%(id)s_%(secret)s_s.jpg"/></a>'
% attrs
else:
xml = ''
print """ </body>
</html>"""
Andrew