1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#!/usr/bin/env python
#
# https://launchpad.net/eeebotu
# eeebotu.py: Copyright 2008 Mike Rooney <eeebotu@rowk.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time, socket, traceback, sys
try:
import feedparser
from BeautifulSoup import BeautifulSoup
except ImportError:
print "Error: EeeBotu requires both python-feedparser and python-beautifulsoup but one or both is not installed."
sys.exit(1)
# Start up logging of stdin and stderr
logFile = open("logfile", "a")
sys.stderr = sys.stdout = logFile
# Import the configuration from the settings file.
from settings import config
def printMsg(msg):
print str(time.ctime()) + ' ' + str(msg)
# we do not, really, need this, but I want to see the messages ASAP ;-)
logFile.flush()
def getIRC():
"""
Connect to IRC and return the IRC connection.
"""
irc = socket.socket() # Create the socket.
irc.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Try to keep the session with TCP keep alives.
irc.connect((config['HOST'], config['HOSTPORT'])) # Connect to server.
irc.send('NICK '+ config['NICK'] +'\r\n') # Send the nick to server.
irc.send('USER '+ config['IDENT'] + ' ' + config['HOST'] + ' bla :' + config['REALNAME'] + '\r\n') # Identify to server.
time.sleep(7)
if config['PASSWORD']:
irc.send('PRIVMSG nickserv :IDENTIFY %s\r\n'%config['PASSWORD'])
time.sleep(2)
irc.send('JOIN ' + config['CHANNEL'] + '\r\n')
readFromIRC(irc)
return irc
def readFromIRC(irc):
saveTimeOut = irc.gettimeout()
irc.settimeout(2)
savedMsg = ''
msg = ' '
while len(msg) > 0:
try:
msg = ''
msg = irc.recv(4096)
except Exception, msgError:
if msgError.args[0] == 'timed out':
continue
else:
raise msgError
if msg != '':
savedMsg += msg
if len(msg) < 4096:
msg = ''
irc.settimeout(saveTimeOut)
# we will have read a chunk of text, perhaps many lines long.
# now we have to split it in lines, and look at each line
for line in savedMsg.splitlines():
if config['verbose']:
printMsg(line)
cmd, sep, operand = line.partition(' ')
if cmd == 'PING':
answer = 'PONG ' + config['NICK'] + ' ' + operand.lstrip(':')
if config['verbose']:
printMsg('Received PING from ' + operand + '\n')
printMsg('Will answer with ' + answer + '\n')
irc.send(answer + '\n')
if cmd == "PRIVMSG":
printMsg('CTCPVersion?, so now what?\n')
if cmd == "ERROR":
printMsg('Oops...\n')
# we should really verify what type of error this is...
# instead, let's just make it re-open the connection
irc.close()
raise ValueError, "Error on IRC, " + line
def numsFromTup(tup):
"""
Given a bug tuple, just return a list of the bug numbers.
"""
return [x[0] for x in tup]
def getLatestBugs(num=5):
"""
Get the num latest bugs in the defined project, as defined by launchpad's atom feed.
Returns a list of tuples in the form of (bugNumber, bugDescription, bugUrl).
"""
d = feedparser.parse("http://feeds.launchpad.net/" + config['project'] + "/latest-bugs.atom")
bugs = []
for i in range(num):
entry = d.entries[i]
bugUrl = entry.link
title = entry.title
numEnd = title.find(']')
bugNum = title[1:numEnd]
bugDesc = title[numEnd+2:]
html = entry.content[0].value
# attempt to grab the package, status, and importance from the HTML of the feed entry
try:
package, status, importance = [tag.contents[0] for tag in BeautifulSoup(html).div.table.findAll('tr')[1].findAll('td')[1:4]]
except:
printMsg("Error souping\n")
printMsg(traceback.print_exc())
package = status = importance = '???'
bugs.insert(0, (bugNum, bugDesc, bugUrl, package, status, importance)) # make the list in chronological order
return bugs
def getBugLocation(bugUrl):
"""
Get the location of a package (universe/main).
"""
import re, urllib2
HTML = urllib2.urlopen(bugUrl).read()
# clean up the HTML a little first
for char in ['\n', '\t']:
HTML = HTML.replace(char, '')
#component = re.findall('<b>Component:</b>([^<]+)<br/>', HTML)
component = re.findall(', uploaded to ([a-z]+) on', HTML)
if component is None:
component = '?'
else:
component = component[0]
bugLocation = " (%s)"%component
return bugLocation
def trackBugs(irc=None):
"""
This is the main loop, which checks for new bugs every minute, and reports any new ones to the IRC channel.
"""
# Initially consider the 2nd-5th most recent bugs as announced, so it just announces the newest the first time.
recentlyAnnounced = numsFromTup(getLatestBugs())[:4] + [0 for i in range(16)]
while True:
try:
bugs = getLatestBugs()
except Exception:
printMsg('Failed to fetch latest bugs from Launchpad, will retry next iteration.\n')
printMsg(traceback.print_exc())
time.sleep(60)
continue
newBugs = [bug for bug in bugs if bug[0] not in recentlyAnnounced]
for [bugNum, bugDesc, bugUrl, bugPackage, bugStat, bugImp] in newBugs:
if bugPackage.lower() == config['project']:
# there is no location since there is no specific package
bugLocation = ""
else:
# try to find the location (main/universe)
try:
bugLocation = getBugLocation(bugUrl)
except:
bugLocation = " (?)"
msg = "New bug #%s in %s%s: \"%s\" [%s, %s] %s"%(bugNum, bugPackage, bugLocation, bugDesc.replace('<', '<').replace('>', '>').replace('&', '&'), bugImp, bugStat, bugUrl)
newMsg = ""
for c in msg:
try:
newMsg += c.encode('utf-8')
except UnicodeError:
newMsg += '?'
if irc is None:
printMsg(newMsg + '\n')
else:
printMsg('sending to IRC:' + newMsg + '\n')
irc.send('PRIVMSG ' + config['CHANNEL'] + ' :%s\r\n'%newMsg)
# Update the queue, putting the bug in front, and popping the last off the back.
recentlyAnnounced.insert(0, bugNum)
recentlyAnnounced.pop()
# try to read something posted on IRC for us
readFromIRC(irc)
time.sleep(60)
if __name__ == "__main__":
while True:
try:
irc = getIRC()
#irc = None
trackBugs(irc)
except KeyboardInterrupt:
printMsg('Requested to stop\n')
irc.send('QUIT\r\n')
irc.close()
break
except Exception:
printMsg('Main loop failed, restarting...\n')
printMsg(traceback.print_exc())
irc.close()
|