Refactored some stuff to be a bit cleaner.

This commit is contained in:
Jeremy Fincher 2003-10-30 03:38:55 +00:00
parent 50c18a6727
commit 8d04bdbcb6

View File

@ -37,8 +37,9 @@ import plugins
import re import re
import sets import sets
import urllib2
import getopt import getopt
import socket
import urllib2
import utils import utils
import debug import debug
@ -76,8 +77,23 @@ example = utils.wrapLines("""
class FreshmeatException(Exception): class FreshmeatException(Exception):
pass pass
def getPage(url):
"""Gets a page. Returns a string that is the page gotten."""
fd = urllib2.urlopen(url)
text = fd.read()
fd.close()
return text
class Http(callbacks.Privmsg): class Http(callbacks.Privmsg):
threaded = True threaded = True
def callCommand(self, method, irc, msg, *L):
try:
callbacks.Privmsg.callCommand(self, method, irc, msg, *L)
except socket.gaierror, e:
irc.error(msg, e.args[1])
except urllib2.HTTPError:
irc.error(msg, str(e))
_titleRe = re.compile(r'<title>(.*?)</title>', re.I | re.S) _titleRe = re.compile(r'<title>(.*?)</title>', re.I | re.S)
def title(self, irc, msg, args): def title(self, irc, msg, args):
@ -89,8 +105,7 @@ class Http(callbacks.Privmsg):
if '://' not in url: if '://' not in url:
url = 'http://%s' % url url = 'http://%s' % url
try: try:
fd = urllib2.urlopen(url) text = getPage(url)
text = fd.read()
m = self._titleRe.search(text) m = self._titleRe.search(text)
if m is not None: if m is not None:
irc.reply(msg, utils.htmlToText(m.group(1).strip())) irc.reply(msg, utils.htmlToText(m.group(1).strip()))
@ -98,8 +113,6 @@ class Http(callbacks.Privmsg):
irc.reply(msg, 'That URL appears to have no HTML title.') irc.reply(msg, 'That URL appears to have no HTML title.')
except ValueError, e: except ValueError, e:
irc.error(msg, str(e)) irc.error(msg, str(e))
except Exception, e:
irc.error(msg, debug.exnToString(e))
_fmProject = re.compile('<projectname_full>([^<]+)</projectname_full>') _fmProject = re.compile('<projectname_full>([^<]+)</projectname_full>')
_fmVersion = re.compile('<latest_version>([^<]+)</latest_version>') _fmVersion = re.compile('<latest_version>([^<]+)</latest_version>')
@ -114,9 +127,7 @@ class Http(callbacks.Privmsg):
project = privmsgs.getArgs(args) project = privmsgs.getArgs(args)
url = 'http://www.freshmeat.net/projects-xml/%s' % project url = 'http://www.freshmeat.net/projects-xml/%s' % project
try: try:
fd = urllib2.urlopen(url) text = getPage(url)
text = fd.read()
fd.close()
if text.startswith('Error'): if text.startswith('Error'):
raise FreshmeatException, text raise FreshmeatException, text
project = self._fmProject.search(text).group(1) project = self._fmProject.search(text).group(1)
@ -130,9 +141,6 @@ class Http(callbacks.Privmsg):
(project, lastupdated, vitality, popularity, version)) (project, lastupdated, vitality, popularity, version))
except FreshmeatException, e: except FreshmeatException, e:
irc.error(msg, debug.exnToString(e)) irc.error(msg, debug.exnToString(e))
except Exception, e:
debug.recoverableException()
irc.error(msg, debug.exnToString(e))
def stockquote(self, irc, msg, args): def stockquote(self, irc, msg, args):
"""<company symbol> """<company symbol>
@ -143,25 +151,16 @@ class Http(callbacks.Privmsg):
symbol = privmsgs.getArgs(args) symbol = privmsgs.getArgs(args)
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s'\ url = 'http://finance.yahoo.com/d/quotes.csv?s=%s'\
'&f=sl1d1t1c1ohgv&e=.csv' % symbol '&f=sl1d1t1c1ohgv&e=.csv' % symbol
try: quote = getPage(url)
fd = urllib2.urlopen(url)
quote = fd.read()
fd.close()
except Exception, e:
irc.error(msg, debug.exnToString(e))
return
data = quote.split(',') data = quote.split(',')
#debug.printf(data) # debugging
if data[1] != '0.00': if data[1] != '0.00':
irc.reply(msg, irc.reply(msg,
'The current price of %s is %s, as of %s EST. '\ 'The current price of %s is %s, as of %s EST. '\
'A change of %s from the last business day.' %\ 'A change of %s from the last business day.' %\
(data[0][1:-1], data[1], data[3][1:-1], data[4])) (data[0][1:-1], data[1], data[3][1:-1], data[4]))
return
else: else:
m = 'I couldn\'t find a listing for %s' % symbol m = 'I couldn\'t find a listing for %s' % symbol
irc.error(msg, m) irc.error(msg, m)
return
_cityregex = re.compile( _cityregex = re.compile(
r'<td><font size="4" face="arial"><b>'\ r'<td><font size="4" face="arial"><b>'\
@ -230,10 +229,7 @@ class Http(callbacks.Privmsg):
'config=&forecast=zandh&pands=%s&Submit=GO' % args[0] 'config=&forecast=zandh&pands=%s&Submit=GO' % args[0]
#debug.printf(url) #debug.printf(url)
try: html = getPage(url)
fd = urllib2.urlopen(url)
html = fd.read()
fd.close()
if 'was not found' in html: if 'was not found' in html:
irc.error(msg, 'No such location could be found.') irc.error(msg, 'No such location could be found.')
return return
@ -254,11 +250,6 @@ class Http(callbacks.Privmsg):
irc.reply(msg, s) irc.reply(msg, s)
else: else:
irc.error(msg, 'The format of the page was odd.') irc.error(msg, 'The format of the page was odd.')
except urllib2.URLError:
irc.error(msg, 'I couldn\'t open the search page.')
except Exception, e:
debug.recoverableException()
irc.error(msg, debug.exnToString(e))
_mlgeekquotere = re.compile('<p class="qt">(.*?)</p>', re.M | re.DOTALL) _mlgeekquotere = re.compile('<p class="qt">(.*?)</p>', re.M | re.DOTALL)
def geekquote(self, irc, msg, args): def geekquote(self, irc, msg, args):
@ -277,13 +268,7 @@ class Http(callbacks.Privmsg):
irc.error(msg, 'Invalid id: %s' % e) irc.error(msg, 'Invalid id: %s' % e)
return return
try: html = getPage('http://bash.org/?%s' % id)
fd = urllib2.urlopen('http://bash.org/?%s' % id)
except urllib2.URLError:
irc.error(msg, 'Error connecting to geekquote server.')
return
html = fd.read()
fd.close()
m = self._mlgeekquotere.search(html) m = self._mlgeekquotere.search(html)
if m is None: if m is None:
irc.error(msg, 'No quote found.') irc.error(msg, 'No quote found.')
@ -299,17 +284,11 @@ class Http(callbacks.Privmsg):
Displays acronym matches from acronymfinder.com Displays acronym matches from acronymfinder.com
""" """
acronym = privmsgs.getArgs(args) acronym = privmsgs.getArgs(args)
try:
url = 'http://www.acronymfinder.com/' \ url = 'http://www.acronymfinder.com/' \
'af-query.asp?String=exact&Acronym=%s' % acronym 'af-query.asp?String=exact&Acronym=%s' % acronym
request = urllib2.Request(url, headers={'User-agent': request = urllib2.Request(url, headers={'User-agent':
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'}) 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'})
fd = urllib2.urlopen(request) html = getPage(request)
except urllib2.URLError:
irc.error(msg, 'Couldn\'t connect to acronymfinder.com')
return
html = fd.read()
fd.close()
# The following definitions are stripped and empties are removed. # The following definitions are stripped and empties are removed.
defs = filter(None, map(str.strip, self._acronymre.findall(html))) defs = filter(None, map(str.strip, self._acronymre.findall(html)))
utils.sortBy(lambda s: not s.startswith('[not an acronym]'), defs) utils.sortBy(lambda s: not s.startswith('[not an acronym]'), defs)
@ -332,9 +311,7 @@ class Http(callbacks.Privmsg):
""" """
hostname = privmsgs.getArgs(args) hostname = privmsgs.getArgs(args)
url = 'http://uptime.netcraft.com/up/graph/?host=%s' % hostname url = 'http://uptime.netcraft.com/up/graph/?host=%s' % hostname
fd = urllib2.urlopen(url) html = getPage(url)
html = fd.read()
fd.close()
m = self._netcraftre.search(html) m = self._netcraftre.search(html)
if m: if m:
html = m.group(1) html = m.group(1)
@ -350,11 +327,7 @@ class Http(callbacks.Privmsg):
Returns information about the current version of the Linux kernel. Returns information about the current version of the Linux kernel.
""" """
try:
fd = urllib2.urlopen('http://www.kernel.org/kdist/finger_banner') fd = urllib2.urlopen('http://www.kernel.org/kdist/finger_banner')
except urllib2.URLError:
irc.error(msg, 'Couldn\'t connect to kernel.org.')
return
for line in fd: for line in fd:
(name, version) = line.split(':') (name, version) = line.split(':')
if 'latest stable' in name: if 'latest stable' in name:
@ -377,20 +350,19 @@ class Http(callbacks.Privmsg):
urlClean = search.replace(' ', '+') urlClean = search.replace(' ', '+')
host = 'http://pgp.mit.edu:11371' host = 'http://pgp.mit.edu:11371'
url = '%s/pks/lookup?op=index&search=%s' % (host, urlClean) url = '%s/pks/lookup?op=index&search=%s' % (host, urlClean)
try:
L = []
fd = urllib2.urlopen(url) fd = urllib2.urlopen(url)
pgpkeys = '' for line in iter(fd.next, ''):
line = fd.readline()
while len(line) != 0:
info = self._pgpkeyre.search(line) info = self._pgpkeyre.search(line)
if info: if info:
pgpkeys += '%s <%s> :: ' % (info.group(3), '%s%s' % (host, L.append('%s <%s%s>' % (info.group(3),host,info.group(1)))
info.group(1))) if len(L) == 0:
line = fd.readline()
if len(pgpkeys) == 0:
irc.reply(msg, 'No results found for %s.' % search) irc.reply(msg, 'No results found for %s.' % search)
fd.close()
else: else:
irc.reply(msg, 'Matches found for %s: %s' % (search, pgpkeys[:-4])) s = 'Matches found for %s: %s' % (search, ' :: '.join(L))
irc.reply(msg, s)
finally:
fd.close() fd.close()
Class = Http Class = Http