Commit 66d29f80 authored by Jonathan Harker's avatar Jonathan Harker

Collect URLs in a dictionary. Fetch page title.

parent cc83cfdb
...@@ -13,11 +13,10 @@ import sys, string, random, time ...@@ -13,11 +13,10 @@ import sys, string, random, time
from ircbot import SingleServerIRCBot from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower from irclib import nm_to_n, nm_to_h, irc_lower
import botcommon import botcommon
import urllib import time
import libxml2doc from mechanize import Browser
# Exclamations - wrong input # Exclamations - wrong input
exclamations = [ exclamations = [
"Zing!", "Zing!",
"Burns!", "Burns!",
...@@ -28,7 +27,6 @@ exclamations = [ ...@@ -28,7 +27,6 @@ exclamations = [
] ]
# Ponderings # Ponderings
ponderings = [ ponderings = [
"Hi, can I have a medium lamb roast, with just potatoes.", "Hi, can I have a medium lamb roast, with just potatoes.",
"Can I slurp on your Big Cock?", "Can I slurp on your Big Cock?",
...@@ -41,7 +39,8 @@ class LolBot(SingleServerIRCBot): ...@@ -41,7 +39,8 @@ class LolBot(SingleServerIRCBot):
self.channel = channel self.channel = channel
self.nickname = nickname self.nickname = nickname
self.urls = [] self.urls = {}
self.logfile = open('/tmp/lolbot.log', 'a')
self.helptext = "Adds URLs to a list. Commands: list - prints a bunch of URLs; clear - clears the list; lol - say something funny; <url> - adds the URL to the list; help - this message." self.helptext = "Adds URLs to a list. Commands: list - prints a bunch of URLs; clear - clears the list; lol - say something funny; <url> - adds the URL to the list; help - this message."
...@@ -49,6 +48,17 @@ class LolBot(SingleServerIRCBot): ...@@ -49,6 +48,17 @@ class LolBot(SingleServerIRCBot):
self.queue.start() self.queue.start()
self.start() self.start()
def save_url(self, url):
try:
br = Browser()
br.open(url)
title = br.title()
except Exception as ex:
print ex
title = ''
self.urls[url] = title
return title
def on_nicknameinuse(self, connection, event): def on_nicknameinuse(self, connection, event):
self.nickname = connection.get_nickname() + "_" self.nickname = connection.get_nickname() + "_"
connection.nick(self.nickname) connection.nick(self.nickname)
...@@ -64,14 +74,20 @@ class LolBot(SingleServerIRCBot): ...@@ -64,14 +74,20 @@ class LolBot(SingleServerIRCBot):
def on_pubmsg(self, connection, event): def on_pubmsg(self, connection, event):
"Deal with a public message in a channel." "Deal with a public message in a channel."
# TODO: log it # log it
self.logfile.write("%s\n" % event.arguments()[0])
# TODO: parse it for links and add it to the list
from_nick = nm_to_n(event.source()) from_nick = nm_to_n(event.source())
args = string.split(event.arguments()[0], ":", 1) args = string.split(event.arguments()[0], ":", 1)
if len(args) > 1 and irc_lower(args[0]) == irc_lower(self.nickname): if len(args) > 1 and irc_lower(args[0]) == irc_lower(self.nickname):
self.do_command(event, string.strip(args[1]), from_nick) self.do_command(event, string.strip(args[1]), from_nick)
else:
# parse it for links, add URLs to the list
words = event.arguments()[0].split(" ")
for w in words:
if w.startswith('http://') or w.startswith('https://'):
title = self.save_url(w)
self.say_public(title)
def say_public(self, text): def say_public(self, text):
"Print TEXT into public channel, for all to see." "Print TEXT into public channel, for all to see."
...@@ -119,18 +135,21 @@ class LolBot(SingleServerIRCBot): ...@@ -119,18 +135,21 @@ class LolBot(SingleServerIRCBot):
self.reply(self.ponder(), target) self.reply(self.ponder(), target)
elif cmd == 'urls' or cmd == 'list': elif cmd == 'urls' or cmd == 'list':
urls = ' || '.join(self.urls) for url, title in self.urls.items():
if urls == '': line = "%s %s" % (url, title)
urls = 'No URLs yet.' self.reply(line, target)
self.reply(urls, target) time.sleep(1)
elif cmd.startswith('http:') or cmd.startswith('https:'): elif cmd.startswith('http:') or cmd.startswith('https:'):
self.urls.append(cmd) title = self.save_url(cmd)
self.reply("URL added.", target) if title == '':
self.reply('URL added.', target)
if title != '':
self.reply('URL added: %s' % title, target)
elif cmd == 'clear': elif cmd == 'clear':
del self.urls del self.urls
self.urls = [] self.urls = {}
self.reply('URLs cleared.', target) self.reply('URLs cleared.', target)
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment