Showing posts with label Google. Show all posts
Showing posts with label Google. Show all posts

Friday, March 11, 2011

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. But, in order to retrieve only the images out of the xml feed, I had to make modifications to the standard Google Feed API.

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 JavaScript excerpt:

[sourcecode language="JavaScript"]
<script type="text/javascript">
google.load("feeds", "1");

function replaceit(matches){
var pattern = new RegExp("<img([^>]+)(\s*[^\/])>", "g");
return matches.match(pattern);
}

function initialize() {
var feed = new google.feeds.Feed("http://feeds.feedburner.com/TechCrunch?format=xml");
feed.setNumEntries(20);

feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var div = document.createElement("div");
var wow = replaceit(entry.content);
div.innerHTML = wow;
container.appendChild(div);
}
}
});
}
google.setOnLoadCallback(initialize);
</script>
[/sourcecode]


Here is how the code works:

You define the feed on line 10, and on line 11 you set the number of entries you want to pull from the XML feed with set setNumEntries() function. If the feed loads successfully line 16 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 19 runs the content through a function I set up on line 4.

The function uses regular expressions to retrieve the img tags and only return the images. Line 21 injects the results into the div specified on line 15 and it uses appendChild to get it done. Hopefully, this is useful to someone!

Friday, March 4, 2011

Feeds in your Blog with Google Ajax Feed API

Put RSS Feeds in your WordPress blog without extra plugins or jQuery



I started using the Google Ajax Feed API and i really like it, so I decided to write a WordPress plugin that will use the API to place rss or xml feeds on your wordpress blog. There are plugins out there already that allow you to put feeds on your blog, and one of them actually uses the Google API, it's called jQuery Google Feed Plugin but as the name suggests it also uses jQuery. jQuery is a wonderful JavaScript library but sometimes I use prototype and I don't want to load both libraries. Actually, I don't want to load any extra library, and after looking at the simplicity of implementing this, I decided that writing a wordpress plugin for this is a waste of time. Anyone can insert feeds inside their wordpress blog with just a couple quick steps:

First, get your own free Google API key and insert this javascript in the header.php file right between the <head> and </head> tag:

[sourcecode language="javascript"]
<script type="text/javascript" src="http://www.google.com/jsapi?key=YOUR-GOOGLE-API-KEY-HERE"></script>
<script type="text/javascript">
google.load("feeds", "1");
function initialize() {
var feedControl = new google.feeds.FeedControl();
feedControl.addFeed("http://www.digg.com/rss/index.xml", "Digg");
feedControl.addFeed("http://feeds.feedburner.com/Techcrunch", "TechCrunch");
feedControl.setNumEntries(10);
feedControl.draw(document.getElementById("feedControl"));
}
google.setOnLoadCallback(initialize);
</script>

[/sourcecode]

Now, go anywhere in your blog where you want the feeds to appear and create the div where you want the results to be displayed:

<div id="feedControl"></div>

This feed technique will allow you to insert as many feeds as you want into your blog and configure the titles, number of entries, and the style via css.

feedControl.addFeed("http://www.digg.com/rss/index.xml", "Digg"); - Here the feed url is http://www.digg.com/rss/index.xml and feed title is Digg, you can change these or add another feed using the same format.

feedControl.setNumEntries(10); - The number 10 here represents how many entries you want to display, change it if you want to show less or more than 10 entries per feed.

If you don't like this technique and still want to insert feeds into your WordPress blog, you can try these plugins:

Friday, February 18, 2011

Tips for Better Google Searches

We all search for stuff and most of us use Google for our searches. Ability to search Google more efficiently will save you a lot of time. Thus, if you feel like saving some of your precious time and pulling the results you want, use some of these tips to make your searches more effective. Some of these might be obvious yet very significant. You should always think about using either/or operators, quotes for exact phrase, or “-” symbol to minus a phrase from your search. If you only use these three tips, your search results will be ten times better. Consider an example.
 
If you just type in Paris Hilton into the search, the results will include all the words: websites that talk about places like Paris, France and websites that talk about people like Nicky Hilton. This is not so obvious with Paris Hilton as an example because she is a very popular celebrity but it’s very evident when searching something with fewer results. So, if you want to see websites that only talk about the actual celebrity, you can add quotes to your search (”Paris Hilton”) and get results for the exact phrase. You can also use minus to not include certain phrases in your search. Searching “Paris Hilton” –jail should remove the websites that talk about Paris Hilton being in jail. If you want to search for one term or another, you can use the OR operator or | as in Paris | Hilton which provides results about the term Paris or the term Hilton.
 
If you arrived on a page that is outdated or changed, it might be useful to search Google’s cache. Use the “cached:” operator to search for a version of a page the Google stores on its own servers. If you want to search for similar terms, you can use the “~” symbol. Searching for ~anorexic Nicole Richie -anorexic will result in pages that contain “thin Nicole Richie” and “skinny Nicole Richie” but not “anorexic Nicole Richie.” The “*” symbol is a wildcard and it can be used to search for a part of a phrase that you do not remember. If you want the lyrics to Paris Hilton’s song “Stars are Blind” and you only recall a part of the lyrics and search Even though the * are crazy you will get some of the proper results. The wildcard is also useful for finding stuff in only certain domains, for example *.edu to search only educational websites.
 
To search only within a certain website, use the “site:” operator. For example, site:music.com Paris Hilton will search for Paris Hilton only within the music.com domain. All the results will point to that domain. The “link:” operator will find results that link to a particular URL. If you are searching for a list of movie show times close to your zip code, you can use the “movie:” operator. For example, movie: knocked realizes that I am looking for the movie “Knocked Up” and displays the movie information, links to trailer, IMDB, and reviews. Once you select a zip code, you can see all the movie listings near you. There is also the “music:” operator that displays only music content. For example, music:Paris Hilton will only provide Paris Hilton’s music information.
 
Did you know that you can search for content in particular file types on Google? If you want to see information about Paris Hilton only in pdf files you can use the “filetype:” operator. The following search Paris Hilton filetype:pdf results in content about Paris Hilton taken only from PDF files. You can also search for your term in certain locations within websites using operators such as “inurl:”(only within the URL), “intitle:”(only within the title), “intext:”(only within the text), and “inanchor:”(only within the anchor text that describes a link). For example, intitle:Paris Hilton will display websites that only have Paris Hilton in their titles.
 
Google Search can also be used to for definitions and calculations. Use the “define:” operator to get a quick definition. define:celebrity will list a couple definitions from various sources with links. Use the +, -, *, / symbols and parentheses to do a simple equation. For example, 2+2 will perform the calculations and it’s much faster then loading the computer’s calculator. For other calculations, such as conversions you can use the “in” operator. For example, 12 usd in euro converts 12 USD to euro and 12 meters in yards converts 12 meters to the value in yards.
 
You can also search for a range of numbers. The “..” symbol is used for range and if you search for Paris Hilton 2002..2004 it will return websites that connect information about Paris Hilton and the years 2002, 2003, and 2004. Google’s algorithms can also distinguish patterns in numbers you enter, so you can search for things like weather forecast, telephone area codes, stock quotes, UPC codes, Vehicle ID numbers (US), Federal Communications Commission (FCC) equipment numbers (US), Federal Aviation Administration (FAA) registration numbers (US), Patent numbers (US), etc. For example, searching for GOOG results in Google’s stock information on the top of the search and websites below.
 
Finally, you should consider doing a vertical search or using Google’s Advance Search. Vertical Search refers to searching within a particular field instead of searching for a term across the entire web. If you are searching for something that is covered in Blogs, you can search Google’s Blog Search or if you want to only search for images, you can search Google’s Image Search. There are also tips for searching within these specific fields, such as searching for faces in image results by adding the following text string to the end of the URL: &imgtype=face. If you only want to see Paris Hilton’s face, try the following search. Notice the &imgtype=face added to the end of the URL. In the end, try using Google’s Advance Search.