From d134a2d9acb39cf20e83336e2cf8da0363c49c9d Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 5 Aug 2004 18:29:26 +0000 Subject: [PATCH] Updated to use webutils throughout. --- plugins/Gameknot.py | 14 +++++--------- plugins/Google.py | 20 +++++++------------- plugins/Http.py | 3 +-- plugins/OSU.py | 6 ++---- plugins/__init__.py | 1 - src/webutils.py | 4 +++- 6 files changed, 18 insertions(+), 30 deletions(-) diff --git a/plugins/Gameknot.py b/plugins/Gameknot.py index 35ba118d5..d375a7ccd 100644 --- a/plugins/Gameknot.py +++ b/plugins/Gameknot.py @@ -39,7 +39,6 @@ import supybot.plugins as plugins import re import sets -import urllib2 import supybot.registry as registry @@ -47,6 +46,7 @@ import supybot.conf as conf import supybot.utils as utils import supybot.plugins as plugins import supybot.ircutils as ircutils +import supybot.webutils as webutils import supybot.privmsgs as privmsgs import supybot.callbacks as callbacks @@ -89,9 +89,7 @@ class Gameknot(callbacks.PrivmsgCommandAndRegexp): def getStats(self, name): gkprofile = 'http://www.gameknot.com/stats.pl?%s' % name try: - fd = urllib2.urlopen(gkprofile) - profile = fd.read() - fd.close() + profile = webutils.getUrl(gkprofile) rating = self._gkrating.search(profile).group(1) games = self._gkgames.search(profile).group(1) (w, l, d) = self._gkrecord.search(profile).groups() @@ -150,8 +148,8 @@ class Gameknot(callbacks.PrivmsgCommandAndRegexp): else: raise callbacks.Error,'The format of the page was odd. %s' % \ conf.supybot.replies.possibleBug() - except urllib2.URLError: - raise callbacks.Error, 'Couldn\'t connect to gameknot.com' + except webutils.WebError, e: + raise callbacks.Error, webutils.strError(e) def gkstats(self, irc, msg, args): @@ -174,9 +172,7 @@ class Gameknot(callbacks.PrivmsgCommandAndRegexp): if not self.registryValue('gameSnarfer', msg.args[0]): return url = match.group(0) - fd = urllib2.urlopen(url) - s = fd.read() - fd.close() + s = webutils.getUrl(url) try: if 'no longer available' in s: s = 'That game is no longer available.' diff --git a/plugins/Google.py b/plugins/Google.py index bf3a353a9..c7edb20c9 100644 --- a/plugins/Google.py +++ b/plugins/Google.py @@ -42,7 +42,6 @@ import sets import time import getopt import socket -import urllib2 import xml.sax import SOAP @@ -55,6 +54,7 @@ import supybot.utils as utils import supybot.ircmsgs as ircmsgs import supybot.plugins as plugins import supybot.ircutils as ircutils +import supybot.webutils as webutils import supybot.privmsgs as privmsgs import supybot.callbacks as callbacks import supybot.structures as structures @@ -381,10 +381,8 @@ class Google(callbacks.PrivmsgCommandAndRegexp): m = match.group(0) header = {'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; ' 'Windows NT 4.0)'} - request = urllib2.Request(m, headers=header) - fd = urllib2.urlopen(request) - text = fd.read() - fd.close() + request = webutils.Request(m, headers=header) + text = webutils.getUrl(request) mThread = None mGroup = None if 'threadm=' in m: @@ -392,19 +390,15 @@ class Google(callbacks.PrivmsgCommandAndRegexp): if path is None: return url = 'http://groups.google.com%s' % path.group(1) - request = urllib2.Request(url, headers=header) - fd = urllib2.urlopen(request) - text = fd.read() - fd.close() + request = webutils.Request(url, headers=header) + text = webutils.getUrl(request) elif 'selm=' in m: path = self._ggSelm.search(m) if m is None: return url = 'http://groups.google.com/groups?%s' % path.group(0) - request = urllib2.Request(url, headers=header) - fd = urllib2.urlopen(request) - text = fd.read() - fd.close() + request = webutils.Request(url, headers=header) + text = webutils.getUrl(request) else: pass mThread = self._ggThread.search(text) diff --git a/plugins/Http.py b/plugins/Http.py index e24a9dd37..ba225ab4a 100644 --- a/plugins/Http.py +++ b/plugins/Http.py @@ -42,7 +42,6 @@ import sets import getopt import socket import urllib -import urllib2 import xml.dom.minidom from itertools import imap, ifilter @@ -235,7 +234,7 @@ class Http(callbacks.Privmsg): acronym = privmsgs.getArgs(args) url = 'http://www.acronymfinder.com/' \ 'af-query.asp?String=exact&Acronym=%s' % urllib.quote(acronym) - request = urllib2.Request(url, headers={'User-agent': + request = webutils.Request(url, headers={'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'}) html = webutils.getUrl(request) if 'daily limit' in html: diff --git a/plugins/OSU.py b/plugins/OSU.py index aecaeda36..05536489f 100644 --- a/plugins/OSU.py +++ b/plugins/OSU.py @@ -38,10 +38,9 @@ __revision__ = "$Id$" import supybot.plugins as plugins -import urllib2 - import supybot.utils as utils import supybot.privmsgs as privmsgs +import supybot.webutils as webutils import supybot.callbacks as callbacks @@ -237,8 +236,7 @@ class OSU(callbacks.Privmsg): s = '.'.join(args) url = 'http://www.ohio-state.edu/cgi-bin/inquiry2.cgi?keyword=%s' % s try: - fd = urllib2.urlopen(url) - data = fd.read() + data = webutils.getUrl(url) emails = [] for line in data.splitlines(): line.strip() diff --git a/plugins/__init__.py b/plugins/__init__.py index 074bf9498..6be678777 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -43,7 +43,6 @@ import sets import time import random import os.path -import urllib2 import UserDict import threading diff --git a/src/webutils.py b/src/webutils.py index 61b5bc561..79d4cb722 100644 --- a/src/webutils.py +++ b/src/webutils.py @@ -34,11 +34,13 @@ __revision__ = "$Id$" import supybot.fix as fix import re -import supybot.conf as conf import socket import urllib2 import urlparse +import supybot.conf as conf + +Request = urllib2.Request class WebError(Exception): pass