From ff505e2f6e570a55adc04c3da391aa131eff5f7d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 31 Dec 2007 12:29:15 +0530 Subject: [PATCH] url filters --- core.py | 2 +- plugins.py | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/core.py b/core.py index 00313df..f31f608 100644 --- a/core.py +++ b/core.py @@ -104,5 +104,5 @@ if __name__ == '__main__': log.startLogging(sys.stdout) f = IRCBotFactory(channels) - reactor.connectTCP("irc.freenode.net", 6667, f) + reactor.connectTCP("irc.freenode.net", 6666, f) reactor.run() diff --git a/plugins.py b/plugins.py index 8d2f71c..68cf9f0 100644 --- a/plugins.py +++ b/plugins.py @@ -2,7 +2,7 @@ import twisted from twisted.python import log from datetime import datetime -import random, time +import random, time, re, urllib PREFIX = ',' @@ -29,6 +29,17 @@ def prefixMatch(string, prefixes): else: return None +def soupify(url): + from BeautifulSoup import BeautifulSoup + import urllib2 + req = urllib2.Request(url, None, {'User-agent': 'mozilla'}) + contents = urllib2.urlopen(req).read() + return BeautifulSoup(contents) + +def urlTitle(url): + soup = soupify(url) + return soup.title.contents[0].encode('utf-8').strip() + def decorate(proxy, original): # as decorator 'overwrites' namespace with these proxy # functions, we need to make the introspecting code happy. @@ -82,6 +93,9 @@ class BotActions(object): def toUser(self, msg): self.toChannel('%s: %s' % (self.user, msg)) + def doAction(self, action): + self.bot.me(self.channel, action) + def grep(self, msg, words): for word in words: if word in msg: @@ -151,7 +165,11 @@ class Commands(BotActions): def cmd_google(self, search_terms=None): # search the internet if search_terms: - self.toChannel('Search: %s' % search_terms) + soup = soupify('http://www.google.com/search?' + urllib.urlencode({'q': search_terms})) + firstLink = soup.find('a', attrs={'class': 'l'})['href'].encode('utf-8').strip() + log.msg('google:', firstLink) + # firstLink = 'http://google.com/' + self.toChannel('%s - %s' % (urlTitle(firstLink), firstLink)) cmd_g = cmd_google @admin(['srid']) @@ -225,7 +243,7 @@ class Filters(BotActions): self.respondWisely(msg, words, quotes) def filter_hatred(self, msg): - words = ['hate', 'suck'] + words = ['hate', 'suck', 'bad'] quotes = [ 'The first reaction to truth is hatred', 'Hatred does not cease by hatred, but only by love; this is the eternal rule.', @@ -247,3 +265,19 @@ class Filters(BotActions): ] self.respondWisely(msg, ['??'], answers) + + def filter_random(self, msg): + random.seed(time.time()) + if random.choice(range(100)) == 0: + self.doAction( + random.choice([ + 'looks around nervously', + ])) + + _filter_links_url = re.compile('.*(https?://[^ ]*).*') + def filter_links(self, msg): + "Show in channel" + match = self._filter_links_url.match(msg) + if match: + self.toChannel('Title: %s' % urlTitle(match.group(1))) + -- 2.11.4.GIT