September 11, 2012 14:07 / 1 comments / irc python

I wrote a little python IRC bot library a while back. It comes with a few silly examples like a google search bot, an ASCII art bot, even an example botnet. Today I was lurking around in a channel with a bunch of other local developers and noticed that we often are pasting links of images to "contextualize" things other folks have said.

For example,

<somebody> god damn you guys, why are there so many bots in the channel?
<cleifer> http://philly.barstoolsports.com/files/2011/08/asante-u-mad-bro2.jpg
<someoneelse> trololol

I wanted to make it easy to insert silly images, so I whipped up a quick bot to scrape google image search. The secret sauce? Preface every query with "crazy":

import httplib2
import json
import urllib

from irc import IRCBot, run_bot

class GoogleImageBot(IRCBot):
    def fetch_result(self, query):
        sock = httplib2.Http(timeout=1)
        query = 'crazy %s' % query # <--- SECRET SAUCE
        headers, response = sock.request(
            'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s' % \
            urllib.quote(query)
        )
        if headers['status'] in (200, '200'):
            response = json.loads(response)
            return response['responseData']['results'][0]['unescapedUrl']

    def find_me(self, nick, message, channel, query):
        result = self.fetch_result(query)
        if result:
            return result

    def command_patterns(self):
        return (
            self.ping('^(?P<query>.+)', self.find_me),
        )

host = 'irc.freenode.net'
port = 6667
nick = 'walrus-eye'

if __name__ == '__main__':
    run_bot(GoogleImageBot, host, port, nick, ['#SECRET'])

To fire it up, just run it with python and let hilarity ensue:

python google_img.py

Then in your IRC once you see the bot log in:

<someone> walrus-eye: erhmahgerd
<walrus-eye> http://25.media.tumblr.com/tumblr_m67khqXnK11qcg623o1_500.png

In case you were curious, that last image is:

Comments (1)

  • Allen | October 2012, at 14:56

    Hey, I'm a community blog curator for DZone. I wanted to talk with you about potentially featuring your blog on DZone's content portals. Send me an email at allenc [at] dzone [dot] com and I'll explain the details.


Commenting has been closed, but please feel free to contact me