Grabbing only Pictures out of an XML feed
Pulling Images from feeds with Google AJAX Feed API and Regular Expressions
Recently, I have been building a super aggregator and search engine website using various google APIs. The search engine is using the Google search API and most of the rss and xml aggregation is handled using the Google AJAX Feed API. I still need to install a couple cron jobs and curl scrapes to make other things work so the website is still not finished, but go to YeFind to check it out.
If you look at the image search, on the left sidebar you can find the latest images pulled from numerous aggregators and websites on the internet, like flickr, digg, reddit, etc. Actually, I am using picurls feeds to grab this content but I had to make modifications to the standard Google Feed API example in order to retrieve only the images out of the xml feed.
The xml feed had some images and some other links and text, but I just wanted to grab the linkable pictures so I had to write an extra function and use regular expressions to get rid of the content i did not want. I assume that this piece of code could be useful to other who want to just pull images out of an xml feed that contains more than just pictures. Here is the code to retrieve images from an xml feed using the Google AJAX Feed API.
To download just save the html file on your computer and if you are going to use the code make sure to replace the API key with your own google API key on line 6. Here is how the code works:
You define the feed on line 17, and on line 18 you set the number of entries you want to pull from the XML feed with set setNumEntries() function. If the feed loads successfully line 23 loops through the 20 items and pulls the content out of the feed. The content contains some text and links that I need to get rid of so line 26 runs the content through a function I set up on line 11. The function uses regular expressions to retrieve the text and returns only the images. Line 28 injects the results into the div specified on line 22 and it uses appendChild to get it done. Hopefully, this is useful to someone!
Popularity: 2%
If you enjoyed this post, please consider to leave a comment.












Comments
No comments yet.
Leave a comment