restructure utilities, harmonize imports, add doc on other bots

This commit is contained in:
Ryan Hitchman 2009-07-08 11:04:30 -06:00
parent e6545f8ce7
commit 40be3038ab
48 changed files with 110 additions and 44 deletions

View File

@ -3,3 +3,4 @@ syntax: glob
*.pyc *.pyc
iambuttbot_password iambuttbot_password
persist persist
pep8.py

26
docs/bots Normal file
View File

@ -0,0 +1,26 @@
Other bots we should "borrow" ideas from:
supybot http://supybot.com/
- horribly bloated plugin structure, each plugin has its own directory and 4 files (unit testing for plugins what)
phenny http://inamidst.com/phenny/
- inspiration for skybot, too much magic and not easy enough to change
pyfibot http://code.google.com/p/pyfibot/
- interesting, but lots of magic
rbot http://linuxbrit.co.uk/rbot/
- Ruby
- lots of plugins
pyirc http://www.k-pdt.net/pyirc/
- very simple, not multithreaded
- poor use of regexes, skybot has much better parsing, but it implements many more irc control godes
- can convert irc colors to vt100 escape codes -- should implement this
- autoreconnect
pybot
- can handle multiple servers, but not multithreaded
- ugly modules
- too many external dependencies
- attempt at NLP

View File

@ -1,10 +1,8 @@
import yaml
import urllib import urllib
import htmlentitydefs import htmlentitydefs
import re import re
import hook from util import hook, yaml
########### from http://effbot.org/zone/re-sub.htm#unescape-html ############# ########### from http://effbot.org/zone/re-sub.htm#unescape-html #############

View File

@ -4,7 +4,8 @@ http://brainfuck.sourceforge.net/brain.py'''
import re import re
import random import random
import hook from util import hook
BUFFER_SIZE = 5000 BUFFER_SIZE = 5000
MAX_STEPS = 1000000 MAX_STEPS = 1000000

View File

@ -1,6 +1,6 @@
import urllib import urllib
import hook from util import hook
@hook.command('god') @hook.command('god')

View File

@ -5,7 +5,8 @@ simulates dicerolls
import re import re
import random import random
import hook from util import hook
whitespace_re = re.compile(r'\s+') whitespace_re = re.compile(r'\s+')
valid_diceroll_re = re.compile(r'^[+-]?(\d+|\d*d\d+)([+-](\d+|\d*d\d+))*$') valid_diceroll_re = re.compile(r'^[+-]?(\d+|\d*d\d+)([+-](\d+|\d*d\d+))*$')

View File

@ -1,8 +1,7 @@
import yaml
import urllib import urllib
from lxml import html from lxml import html
import hook from util import hook, yaml
def api_get(kind, query): def api_get(kind, query):

View File

@ -1,4 +1,4 @@
import hook from util import hook
#@hook.command #@hook.command

View File

@ -1,6 +1,6 @@
import hashlib import hashlib
import hook from util import hook
@hook.command @hook.command

View File

@ -4,7 +4,8 @@ posts everything buttbot says to the iambuttbot twitter account
""" """
import urllib import urllib
import hook
from util import hook
@hook.command(hook=r'(.*)', prefix=False, ignorebots=False) @hook.command(hook=r'(.*)', prefix=False, ignorebots=False)

View File

@ -8,7 +8,8 @@ import codecs
import time import time
import re import re
import hook from util import hook
lock = thread.allocate_lock() lock = thread.allocate_lock()
log_fds = {} # '%(net)s %(chan)s' : (filename, fd) log_fds = {} # '%(net)s %(chan)s' : (filename, fd)

View File

@ -1,4 +1,4 @@
import hook from util import hook
@hook.event('KICK INVITE') @hook.event('KICK INVITE')

View File

@ -1,6 +1,6 @@
# for crusty old rotor # for crusty old rotor
import hook from util import hook
@hook.command @hook.command

View File

@ -1,7 +1,8 @@
import urllib import urllib
import re import re
import hook from util import hook
re_lineends = re.compile(r'[\r\n]*') re_lineends = re.compile(r'[\r\n]*')

View File

@ -6,7 +6,8 @@ import os
import thread import thread
import codecs import codecs
import hook from util import hook
lock = thread.allocate_lock() lock = thread.allocate_lock()
memory = {} memory = {}

View File

@ -2,11 +2,11 @@
import sqlite3 import sqlite3
import datetime, time import datetime, time
import hook
import os import os
from timesince import timesince
dbname = "skydb" from util import hook, timesince
dbname = "seen.db"
def adapt_datetime(ts): def adapt_datetime(ts):
return time.mktime(ts.timetuple()) return time.mktime(ts.timetuple())
@ -18,18 +18,9 @@ def seeninput(bot,input):
dbpath = os.path.join(bot.persist_dir, dbname) dbpath = os.path.join(bot.persist_dir, dbname)
conn = dbconnect(dbpath) conn = dbconnect(dbpath)
cursor = conn.cursor() cursor = conn.cursor()
command = "select count(name) from seen where name = ? and chan = ?" cursor.execute("insert or replace into seen(name, date, quote, chan) values(?,?,?,?)"
cursor.execute(command, (input.nick,input.chan)) command, (input.nick, datetime.datetime.now(), input.msg, input.chan))
if(cursor.fetchone()[0] == 0):
command = "insert into seen(name, date, quote, chan) values(?,?,?,?)"
cursor.execute(command, (input.nick, datetime.datetime.now(), input.msg, input.chan))
else:
command = "update seen set date=?, quote=? where name = ? and chan = ?"
cursor.execute(command, (datetime.datetime.now(), input.msg, input.nick, input.chan))
conn.commit() conn.commit()
conn.close() conn.close()
@ -66,7 +57,7 @@ def seen(bot, input):
# check to see that our db has the the seen table, and return a connection. # check to see that our db has the the seen table, and return a connection.
def dbconnect(db): def dbconnect(db):
conn = sqlite3.connect(db) conn = sqlite3.connect(db)
results = conn.execute("select count(*) from sqlite_master where name=?", ("seen" ,)).fetchone() results = conn.execute("select count(*) from sqlite_master where name=?", ("seen",)).fetchone()
if(results[0] == 0): if(results[0] == 0):
conn.execute("create table if not exists "+ \ conn.execute("create table if not exists "+ \

View File

@ -1,6 +1,6 @@
import re import re
import hook from util import hook
@hook.sieve @hook.sieve

View File

@ -1,10 +1,12 @@
" tell.py: written by sklnd in July 2009" " tell.py: written by sklnd in July 2009"
import datetime, time
import sqlite3
import hook
import os import os
from timesince import timesince import time
import datetime
import sqlite3
from util import hook, timesince
dbname = "skydb" dbname = "skydb"

View File

@ -1,7 +1,7 @@
import re import re
import urllib2 import urllib2
import hook from util import hook
tinyurl_re = re.compile(r'http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)', flags=re.IGNORECASE) tinyurl_re = re.compile(r'http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)', flags=re.IGNORECASE)

View File

@ -6,7 +6,7 @@ retrieves most recent tweets
import urllib import urllib
from lxml import etree from lxml import etree
import hook from util import hook
def unescape_xml(string): def unescape_xml(string):

View File

@ -1,7 +1,7 @@
from lxml import html from lxml import html
import urllib import urllib
import hook from util import hook
@hook.command('u') @hook.command('u')

0
plugins/util/__init__.py Normal file
View File

View File

@ -6,7 +6,8 @@ import thread
import urllib import urllib
from lxml import etree from lxml import etree
import hook from util import hook
lock = thread.allocate_lock() lock = thread.allocate_lock()
stalk = {} stalk = {}

View File

@ -5,7 +5,8 @@ import urllib
from lxml import etree from lxml import etree
import re import re
import hook from util import hook
api_prefix = "http://en.wikipedia.org/w/api.php" api_prefix = "http://en.wikipedia.org/w/api.php"
search_url = api_prefix + "?action=opensearch&search=%s&format=xml" search_url = api_prefix + "?action=opensearch&search=%s&format=xml"

View File

@ -2,9 +2,7 @@ import re
from lxml import etree from lxml import etree
import locale import locale
import hook from util import hook
locale.setlocale(locale.LC_ALL, "")
def ytdata(id): def ytdata(id):

43
plugins/youtube.py.orig Normal file
View File

@ -0,0 +1,43 @@
import re
from lxml import etree
import locale
import hook
locale.setlocale(locale.LC_ALL, "")
def ytdata(id):
url = 'http://gdata.youtube.com/feeds/api/videos/' + id
x = etree.parse(url)
# I can't figure out how to deal with schemas/namespaces properly :(
yt = '{http://gdata.youtube.com/schemas/2007}'
media = '{http://search.yahoo.com/mrss/}'
rating = x.find('{http://schemas.google.com/g/2005}rating')
data = dict(rating.items())
data['title'] = x.find('{http://www.w3.org/2005/Atom}title').text
data['views'] = locale.format('%d', int(x.find(yt + 'statistics').get(
'viewCount')), 1)
length = int(x.find(media + 'group/' + yt + 'duration').get('seconds'))
data['length'] = ''
if length / 3600: # > 1 hour
data['length'] += str(length/3600) + 'h '
if length / 60: # > 1 minute
data['length'] += str(length/60 % 60) + 'm '
data['length'] += "%ds" % (length % 60)
return data
youtube_re = re.compile(r'.*youtube.*v=([-_a-z0-9]+)', flags=re.IGNORECASE)
@hook.command(hook=r'(.*)', prefix=False, ignore='chan:#cobol chan:#yospos')
def youtube(inp):
m = youtube_re.match(inp)
if m:
data = ytdata(m.group(1))
return '\x02%(title)s\x02 - rated \x02%(average)s/%(max)s\x02 ' \
'(%(numRaters)s) - views \x02%(views)s\x02 - length \x02' \
'%(length)s\x02' % data