< Day Day Up > |
Hack 66. Google from IRCPerforming 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. When someone pops into your IRC channel with a question, you can bet your life that 9 times out of 10, he could have easily found the answer on Google. If you think this is the case, you could tell him that, or you could do it slightly more subtly by suggesting a Google search term to an IRC bot, which will then go and look for a result. Most IRC clients are capable of highlighting URLs in channels. Clicking on a highlighted URL will open your default web browser and load the page. For some people, this is a lot quicker than finding the icon to start your web browser and then typing or pasting the URL. More obviously, a single Google search will present its result to everybody in the channel. The goal is to have an IRC bot called GoogleBot that responds to the !google command. It will respond by showing the title and URL of the first Google search result. If the size of the page is known, that will also be displayed. 5.9.1. The CodeFirst, 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 [Chapter 9]. 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.
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"); } } 5.9.2. Running the HackWhen you compile the bot, remember to include both pircbot.jar and googleapi.jar in the classpath: C:\java\GoogleBot> javac -classpath .;pircbot.jar;googleapi.jar *.java You can then run the bot like so: C:\java\GoogleBot> java -classpath .;pircbot.jar;googleapi.jar GoogleBotMain The bot will then start up and connect to the IRC server. 5.9.3. The ResultsFigure 5-26 shows GoogleBot running in an IRC channel and responding with the URL, title, and size of each of the results of a Google search. Figure 5-26. The GoogleBot performing an IRC-related searchPerforming a Google search is a popular task for bots to do. Take this into account if you run your bot in a busy channel, because there might already be a bot there that lets users search Google. |
< Day Day Up > |