Small change in the flickr API output breaking my bad code
Wednesday, December 31st, 2008During my absence I got an email that the first version of my unobtrusive Flickr badge has stopped working. The reason was that Flickr changed the output of the API. I the old version the JSON object contained an HTML description that was run through htmlspecialchars() first. I always considered this a bit of a nuisance and wondered about the reason. Now the Flickr feed does not have the encoding any longer as you can see for example in the JSON output of my latest photos:
{
“title”: “Terminal 5 animated discs thingamabob *”,
“link”: “http://www.flickr.com/photos/codepo8/3141566868/”,
“media”: {m“},
“date_taken”: “2008-12-27T08:10:35-08:00”,
“description”: “
codepo8 posted a video:
* technical term, really
“,
“published”: “2008-12-27T16:10:35Z”,
“author”: “nobody@flickr.com (codepo8)”,
“author_id”: “11414938@N00”,
“tags”: “london art heathrow awesome animation t5 ba britishairways terminal5”
},
Before the change this would have been:
{
“title”: “Terminal 5 animated discs thingamabob *”,
“link”: “http://www.flickr.com/photos/codepo8/3141566868/”,
“media”: {m“},
“date_taken”: “2008-12-27T08:10:35-08:00”,
“description”: “
codepo8 posted a video:
* technical term, really
“,
“published”: “2008-12-27T16:10:35Z”,
“author”: “nobody@flickr.com (codepo8)”,
“author_id”: “11414938@N00”,
“tags”: “london art heathrow awesome animation t5 ba britishairways terminal5”
},
This broke my code as I was relying on regular expressions to get the photos. Taught me a lesson, and as we have a media property now there is no need for this any longer. The fix for the badge code was:
// buggy, bad
temp=obj.items[i].description.match(/src="([^&]*)"/)[1];”
// works :)
temp=obj.items[i].media.m;
Lesson learned: Don’t trust regular expressions and data scraping.
Thanks to Jennifer Forman Orth for flagging this up!