Fixed various non-closed urllib2.urlopens; changed gkstats to respond appropriately if there's no such user.

This commit is contained in:
Jeremy Fincher 2003-03-15 11:11:01 +00:00
parent 25d4f1238e
commit 7e5162d8e4
1 changed files with 15 additions and 7 deletions

View File

@ -61,6 +61,7 @@ class Http(callbacks.Privmsg):
try: try:
fd = urllib2.urlopen(url) fd = urllib2.urlopen(url)
text = fd.read() 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)
@ -88,6 +89,7 @@ class Http(callbacks.Privmsg):
except Exception, e: except Exception, e:
irc.error(msg, debug.exnToString(e)) irc.error(msg, debug.exnToString(e))
text = fd.read() text = fd.read()
fd.close()
text = self._htmlTag.sub('', text) text = self._htmlTag.sub('', text)
lines = text.splitlines() lines = text.splitlines()
sent = False sent = False
@ -105,7 +107,9 @@ class Http(callbacks.Privmsg):
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: try:
quote = urllib2.urlopen(url).read() fd = urllib2.urlopen(url)
quote = fd.read()
fd.close()
except Exception, e: except Exception, e:
irc.reply(msg, debug.exnToString(e)) irc.reply(msg, debug.exnToString(e))
return return
@ -127,7 +131,9 @@ class Http(callbacks.Privmsg):
search = '+'.join(args) search = '+'.join(args)
url = 'http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=%s' % search url = 'http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=%s' % search
try: try:
html = urllib2.urlopen(url).read() fd = urllib2.urlopen(url)
html = fd.read()
fd.close()
except Exception, e: except Exception, e:
irc.error(msg, debug.exnToString(e)) irc.error(msg, debug.exnToString(e))
text = html.split('<P>\n', 2)[1] text = html.split('<P>\n', 2)[1]
@ -144,7 +150,9 @@ class Http(callbacks.Privmsg):
name = privmsgs.getArgs(args) name = privmsgs.getArgs(args)
gkprofile = 'http://www.gameknot.com/stats.pl?%s' % name gkprofile = 'http://www.gameknot.com/stats.pl?%s' % name
try: try:
profile = urllib2.urlopen(gkprofile).read() fd = urllib2.urlopen(gkprofile)
profile = fd.read()
fd.close()
rating = self._gkrating.search(profile).group(1) rating = self._gkrating.search(profile).group(1)
games = self._gkgames.search(profile).group(1) games = self._gkgames.search(profile).group(1)
profile = stripHtml(profile) profile = stripHtml(profile)
@ -152,10 +160,10 @@ class Http(callbacks.Privmsg):
irc.reply(msg, '%s is rated %s and has %s active games; ' irc.reply(msg, '%s is rated %s and has %s active games; '
'W-%s, L-%s, D-%s' % (name, rating, games, w, l, d)) 'W-%s, L-%s, D-%s' % (name, rating, games, w, l, d))
except AttributeError: except AttributeError:
if profile.find('User %s not found!' % name) != -1: if profile.find('User %s not found!' % name) != -1:
irc.error(msg, 'No user %s exists.') irc.error(msg, 'No user %s exists.')
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: except urllib2.URLError:
irc.error(msg, 'Couldn\'t connect to gameknot.com.') irc.error(msg, 'Couldn\'t connect to gameknot.com.')