commit: 23e5c88a70e950bf695dbbdb4a27d0b8d5a070c9
parent: eb150c10512cf49f4041f3ef943d04d6a7238159
Author: lanodan <haelwenn.monnier@gmail.com>
Date: Tue, 8 Jul 2014 23:24:30 +0200
Add Comments and imporved a bit of the code
Diffstat:
M | IRCBot/IRCBot.py | 104 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
1 file changed, 55 insertions(+), 49 deletions(-)
diff --git a/IRCBot/IRCBot.py b/IRCBot/IRCBot.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
-#Author: Haelwenn Monnier (lanodan) <haelwenn.monnier@gmail.com>
-#License: CC-BY-SA
+#@Author: Haelwenn Monnier (lanodan) <haelwenn.monnier@gmail.com>
+#@License: CC-BY-SA
import socket, sys, re, time, urllib, subprocess, ConfigParser, os
@@ -12,21 +12,22 @@ def get_element(textInput, elem):
idx2=textInput.find('</'+elem+'>')
return textInput[idx1+len('<'+elem+'>'):idx2].strip()
-# Output string into the channel
+# Output to the channel and console
def printIrc(ircout):
irc.send('PRIVMSG '+channel+' :'+ircout+'\n')
print 'PRIVMSG '+channel+' :'+ircout
-#line: string of text to add in the log
+#line: append to the log (output it to the console too)
def log(line):
print '[LOG]'+line
log = open('IRCBot.log', 'a')
log.write(line+'\n')
log.close()
-httpRegex = re.compile('https?://+')
+httpRegex = re.compile('https?://+') # http(s) regex
userRegex = re.compile(':[\w\d]')
+# Load the config file (config.ini)
config = ConfigParser.ConfigParser()
if (config.read('config.ini')):
botnick = config.get('Config', 'nick')
@@ -50,46 +51,51 @@ time.sleep(1)
irc.send('JOIN '+ channel +'\n')
printIrc(welcomeMsg)
-while 1: #puts it in a loop
- text=irc.recv(2048) #receive the text
- print text
+# Make a while loop to alway parse input
+while 1:
+ text=irc.recv(2048) # text is the socket input (I use text because input is already taken)
+ print text # print the input for debugging purpose
- if text.find('PING') != -1: #check if 'PING' is found
- irc.send('PONG ' + text.split() [1] + '\n') #returnes 'PONG' back t
- if text.find(':!hi') !=-1: #you can change !hi to whatever you want
- t = text.split(':!hi') #you can change t and to :)
- to = t[1].strip() #this code is for getting the first word after !hi
- printIrc('Ohayo-nyan '+str(to)+' ! [http://i.imgur.com/vzYFOkp.jpg]')
- if text.find(':!say') != -1: #you can change !hi to whatever you want
- t = text.split(':!say') #you can change t and to :)
- out = t[1].strip() #this code is for getting the first word after !hi
+ # Below is a bunch of conditions to see if something the bot can deal with is found
+
+ if text.find('PING') != -1:
+ irc.send('PONG ' + text.split() [1] + '\n') # Return PONG back (sort of "I'm alive")
+ if text.find(':!hi') !=-1:
+ t = text.split(':!hi') # split the message received at the :!hi part
+ to = t[1].strip() # take the string after !hi
+ printIrc('Ohayo-nyan '+str(to)+' ! [http://i.imgur.com/vzYFOkp.jpg]') # Say something crazy
+ #TODO: Custom !hi is the config
+ if text.find(':!say') != -1:
+ t = text.split(':!say')
+ out = t[1].strip()
printIrc(str(out))
- if text.find(':!action') != -1: #you can change !hi to whatever you want
- t = text.split(':!action') #you can change t and to :)
- out = t[1].strip() #this code is for getting the first word after !hi
- printIrc('\x01ACTION '+out+'\x01')
- if httpRegex.search(text) is not None:
- parse = re.findall('https?://[^\"\'\(\)\[\]\{\}\<\>\ ]+', text)
+ if text.find(':!action') != -1:
+ t = text.split(':!action')
+ out = t[1].strip()
+ printIrc('\x01ACTION '+out+'\x01') # equivalent of /me
+ if httpRegex.search(text) is not None: # Use httpRegex on input
+ parse = re.findall('https?://[^\"\'\(\)\[\]\{\}\<\>\ ]+', text) # get only the link in parse
try:
- url = str(parse[0]).rstrip() #took the first link and remove newline and whitespaces
- if (len(url) > 8) : # I assume a link is more than 8 characters long (thx NyanKiyoshi for spotting my drunkness here xD)
+ url = str(parse[0]).rstrip() # Take the parsed link
+ if (len(url) > 8) : # I assume a link is more than 8 characters long
try:
- get = urllib.urlopen(url)
- wget = get.read()
- mimeType = get.info().type
- print get.info()
- get.close()
- if (re.search('text/x?html', mimeType)):
- if wget.find('<title>') != -1:
- title = get_element(wget, 'title')
- printIrc('Title: '+title)
- log(url+'; '+str(mimeType)+'; '+title)
- if wget.find('<TITLE>') != -1:
- title = get_element(wget, 'TITLE')
- printIrc('Title: '+title)
- log(url+'; '+str(mimeType)+'; '+title)
+ get = urllib.urlopen(url) # Open the link
+ wget = get.read() # Read the input of the url
+ print get.info() # Print Headers
+ mimeType = get.info().type # Get the Content-Type
+ get.close() # Close the connection
+ # find the title
+ if wget.find('<title>') != -1:
+ title = get_element(wget, 'title')
+ printIrc('Title: '+title)
+ log(url+'; '+str(mimeType)+'; '+title)
+ elif wget.find('<TITLE>') != -1:
+ title = get_element(wget, 'TITLE')
+ printIrc('Title: '+title)
+ log(url+'; '+str(mimeType)+'; '+title)
+ #If we can't find the title print the Content-Type
else:
- printIrc('Type: '+mimeType)
+ printIrc('Type: '+mimeType+' Size:')
log(url+'; '+str(mimeType))
except Exception, e:
printIrc('Exception: '+str(e))
@@ -97,15 +103,15 @@ while 1: #puts it in a loop
printIrc('Link too short (not more than 8)')
except Exception, e:
printIrc('Exception: '+str(e))
+ # Output the link to find the source code
if text.find(':!source') != -1:
printIrc(source)
- if text.find('KICK '+channel) != -1:
+ if text.find('KICK '+channel) != -1: #ISSUE: parse all kick in the channel
time.sleep(5)
- irc.send('JOIN '+ channel +'\n') #join the chan
+ irc.send('JOIN '+ channel +'\n') #re-join the chan
time.sleep(1)
- printIrc(welcomeMsg)
- #if text.find('JOIN '+channel) != -1:
- # printIrc('Ohayo-nyan, '+str(userRegex.findall((text))+' ! [http://i.imgur.com/vzYFOkp.jpg]')
+ printIrc(welcomeMsg) # Say hello
+ # rick roll the chan
if text.find('tetris') != -1:
if tetris != 1:
printIrc('Never gonna give you up.')
@@ -116,12 +122,12 @@ while 1: #puts it in a loop
time.sleep(1)
printIrc('Never gonna say goodbye.')
printIrc('Never gonna tell a lie and hurt you.')
- tetris=1
+ tetris=1 # Prevent another rick roll
if text.find(':!stop in the name of sey') != -1:
irc.send('QUIT : '+quitMsg+'\n')
- irc.close()
break
if text == "":
- irc.close()
+ irc.send('QUIT : Empty socket input\n')
break
-sys.exit()
+irc.close() # Close the socket
+sys.exit() # Exit