O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  



HACK
#66
Google from IRC
Performing Google searches from IRC is not only convenient, but also efficient. See how fast you can Google for something on IRC and click on the URL highlighted by your IRC client
The Code
[Discuss (0) | Link to this hack]

The Code

First, unless you've already done so, you will need to grab a copy of the Google Web APIs Developer's Kit (http://www.google.com/apis/download.html) and create a Google account and obtain a license key []. As I write this, the free license key entitles you to 1,000 automated queries per day. This is more than enough for a single IRC channel.

The googleapi.jar file included in the kit contains the classes that the bot will use to perform Google searches, so you will need to make sure this is in your classpath when you compile and run the bot (the simplest way is to drop it into the same directory as the bot's code itself).

The GoogleBot is built upon the PircBot Java IRC API (http://www.jibble.org/pircbot.php), a framework for writing IRC bots. You'll need to download a copy of the PircBot ZIP file, unzip it, and drop pircbot.jar into the current directory, along with the googleapi.jar.

TIP

For more on writing Java-based bots with the PircBot Java IRC API, be sure to check out "IRC with Java and PircBot" [Hack #35] in IRC Hacks (O'Reilly) by Paul Mutton.

Create a file called GoogleBot.java:

import org.jibble.pircbot.*;
import com.google.soap.search.*;
     
public class GoogleBot extends PircBot {
    
    // Change this so it uses your license key!
    private static final String googleKey = "000000000000000000000000000000";
    
    public GoogleBot(String name) {
        setName(name);
    }
    
    public void onMessage(String channel, String sender, String login,
            String hostname, String message) {
        
        message = message.toLowerCase( ).trim( );
        if (message.startsWith("!google ")) {
            String searchTerms = message.substring(8);
            
            String result = null;
            try {
                GoogleSearch search = new GoogleSearch( );
                search.setKey(googleKey);
                search.setQueryString(searchTerms);
                search.setMaxResults(1);
                GoogleSearchResult searchResult = search.doSearch( );
                GoogleSearchResultElement[] elements =
                        searchResult.getResultElements( );
                if (elements.length == 1) {
                    GoogleSearchResultElement element = elements[0];
                    // Remove all HTML tags from the title.
                    String title = element.getTitle( ).replaceAll("<.*?>", "");
                    result = element.getURL( ) + " (" + title + ")";
                    if (!element.getCachedSize( ).equals("0")) {
                        result = result + " - " + element.getCachedSize( );
                    }
                }
            }
            catch (GoogleSearchFault e) {
                // Something went wrong. Say why.
                result = "Unable to perform your search: " + e;
            }
            
            if (result == null) {
                // No results were found for the search terms.
                result = "I could not find anything on Google.";
            }
            
            // Send the result to the channel.
            sendMessage(channel, sender + ": " + result);
        }
    }
    
}

Your license key will be a simple string, so you can store that in the GoogleBot class as googleKey.

You now need to tell the bot which channels to join. If you want, you can tell the bot to join more than one channel, but remember, you are limited in the number of Google searches that you can do per day.

Create the file GoogleBotMain.java:

public class GoogleBotMain {
    
    public static void main(String[] args) throws Exception {
        GoogleBot bot = new GoogleBot("GoogleBot");        
        bot.setVerbose(true);
        bot.connect("irc.freenode.net");
        bot.joinChannel("#irchacks");
    }
    
}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.