I would like some feed back on my code.
It is an IRC bot written in Python 2.6.
What I might be doing wrong/inefficient.
Better ways of doing something.
And anything that will help sharpen my skills.
I know I probably should have wrapped it into a class, and the error handling is sloppy. This is my first attempt at a IRC Bot as well.
The hardest part about this project was figuring out the IRC protocol and how to send certain commands.
The only module that isn't a part of the standard library is xgoogle and can be found here:
http://www.catonmat.net/blog/python-library-for-google-search/
No IRC SSL support yet.
Well, here's the code:
#-------------------------------------------------------------------------------
# Name: PyIRC
# Purpose: Python based IRC bot
#
# Author: K.B. Carte (techb)
#
# Created: 04/27/2010
# Copyright: (c) K.B. Carte (techb) 2010
# Licence: General Public License (GPL) V.3
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import socket, string, time, random, re, xgoogle, urllib2, cookielib
from xgoogle.search import GoogleSearch, SearchError
from xgoogle.translate import Translator
chan = '' #example: chan = 'ITChannel'
ircsite = '' #example: site = 'irc.mibbit.com'
port = 6667
lang = Translator().lang
translate = Translator().translate
def sentance():
noun = ['school', 'yard', 'house', 'ball', 'shoes', 'shirt',
'fan', 'purse', 'bag', 'pants', 'toaster', 'lamp', 'floor',
'door', 'table', 'bread', 'dresser', 'cup', 'salt', 'pepper',
'plate', 'dog', 'cat', 'wood', 'stool', 'suitcase', 'plane',
'bus', 'car', 'bike', 'phone', 'pillow', 'wall', 'window',
'bed', 'blanket', 'hand', 'head', 'bra', 'eyes', 'sock',
'plastic', 'card board', 'pantys', 'oven', 'bow', 'hair',
'person', 'clock', 'foot', 'boy', 'book', 'ear', 'girl',
'park', 'basket', 'woman', 'street', 'box', 'man']
verb = ['bounced', 'cried', 'jumped', 'yelled', 'flew', 'screamed',
'coughed','smoked','sneezed','exploded','puked','fell','burned',
'smiled','spied','slept','drove','fucked','skipped','ran','walked',
'died','laughed','sang','tripped','frowned','slipped','committed murder']
adjective = ['beautiful','black','old','ugly','wet','red','loud','blue','dry',
'quiet','purple','hairy','medicore','yellow','bald','meloncholy',
'white','smooth','bright','clear','rough','dark','round','sexual',
'heavy','square','sexy','smelly','triangular','angry','discusting',
'octagon','sad','delicious','precious','happy','fat','distinguished',
'scared','skinny','burnt','brown','new']
ending = ['?', '.', '...', '!']
while 1:
random.shuffle(noun)
random.shuffle(verb)
random.shuffle(adjective)
random.shuffle(ending)
n = noun[0]
a = adjective[0]
v = verb[0]
e = ending[0]
s = "The %s %s %s %s" % (a,n,v,e)
return(s)
irc = socket.socket()
irc.connect((ircsite, port))
print irc.recv(1024)
n = 'TechBot%d' % random.randint(0,9999)
irc.send('NICK %s\r\n' % n)
irc.send("USER %s %s bla :%s\r\n" % ("Ohlook", 'itsnotmy', 'Realname'))
r = False
readbuffer = ''
while True:
readbuffer= irc.recv(1024)
temp=string.split(readbuffer, "\n")
Check = readbuffer.split(':')
print readbuffer
if 'PING' in readbuffer:
irc.send("PONG :%s" % Check[1])
if r == False:
irc.send("JOIN #%s\r\n" % chan)
print "tried to join"
r = True
if 'JOIN' in readbuffer:
na = Check[1].split('!')
irc.send("PRIVMSG #%s :Hello %s\r\n" % (chan, str(na[0])))
if "^translate" in readbuffer:
if "^translate lang" in readbuffer:
na = readbuffer.split('!')
for i in lang:
irc.send("PRIVMSG %s :%s = %s\r\n" % (na[0].strip(':'),lang[i],i))
else:
tran = readbuffer.split(':')
na = readbuffer.split('!')
tran = tran[2:]
if not tran:
irc.send("PRIVMSG #%s :syntax'^translate :search term :<lang>\r\n'" % chan)
else:
tr = tran[1].strip()
try:
l = translate(tr, lang_to=tran[2].strip())
irc.send("PRIVMSG #%s :%s\r\n" % (chan, l))
except xgoogle.translate.TranslationError:
irc.send("PRIVMSG #%s :not a valid language\r\n" % chan)
if "^quote" in readbuffer:
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders.append(('User-agent', 'Mozilla/4.0'))
opener.addheaders.append( ('Referer', 'http://www.hellboundhackers.org/index.php') )
resp = opener.open('http://www.hellboundhackers.org/index.php')
r = resp.read()
resp.close()
del cj, opener
da = re.findall("nter; width:70%;'>(.*)</div>",r)
irc.send("PRIVMSG #%s :%s\r\n" % (chan, da[0]))
irc.send("PRIVMSG techb :test\r\n")
if "^google" in readbuffer:
try:
fin = readbuffer.split(':')
print fin[2]
if not fin:
irc.send("PRIVMSG #%s :syntax'^google :search term\r\n'" % chan)
else:
fin = fin[3].strip()
gs = GoogleSearch(fin)
gs.results_per_page = 10
results = gs.get_results()
x =[]
for results in results:
irc.send("PRIVMSG #%s :%s\r\n" % (chan, results.url.encode("utf8")))
except IndexError:
irc.send("PRIVMSG #%s :syntax ^+google :search term\r\n" % chan)
except SearchError, e:
irc.send("PRIVMAS #%s :Search failed: %s" % (chan, e))
if "^time" in readbuffer:
irc.send("PRIVMSG #%s :%s\r\n" % (chan, time.strftime('%I')+':'+time.strftime('%M')))
if "^sentance" in readbuffer:
sen = sentance()
irc.send("PRIVMSG #%s :%s\r\n" % (chan, sen))
if "^boobs" in readbuffer:
irc.send("PRIVMSG #%s :(.Y.)\r\n" % chan)
if "^say" in readbuffer:
th = readbuffer.split('^say')
th = th[-1].split('\r\n')
irc.send("PRIVMSG #%s :%s\r\n" % (chan, th[0].strip()))
if "^test" in readbuffer:
irc.send("PRIVMSG #%s :I'm still alive...\r\n" % chan)
if "^commands" in readbuffer:
irc.send("PRIVMSG #%s :^ + (commands, say <term/phrase>, sentance, test, whoareyou, boobs, google :<search term>, quote,\r\n" % chan)
irc.send("PRIVMSG #%s :translate :<term/phrase> :<lang code> **type 'translate lang' for list of available languages**)\r\n" % chan)
if "^whoareyou" in readbuffer:
irc.send("PRIVMSG #%s :I am %s, I was created By: TechB\r\n" % (chan, n))
irc.send("PRIVMSG #%s :I was written in Python 2.6, and edited with PyScripter\r\n" % chan)
irc.send("PRIVMSG #%s :I am still in my early stages and hope to one day develop A.I.\r\n" % chan)