This commit is contained in:
Jeremy Fincher 2004-01-03 06:26:39 +00:00
parent 7e4cab7c7b
commit c4a122da5a
2 changed files with 34 additions and 50 deletions

View File

@ -75,12 +75,9 @@ class Http(callbacks.Privmsg):
if not url.startswith('http://'): if not url.startswith('http://'):
irc.error(msg, 'Only HTTP urls are valid.') irc.error(msg, 'Only HTTP urls are valid.')
return return
try: fd = webutils.getUrlFd(url)
fd = webutils.getUrlFd(url) s = ', '.join(['%s: %s' % (k, v) for (k, v) in fd.headers.items()])
s = ', '.join(['%s: %s' % (k, v) for (k, v) in fd.headers.items()]) irc.reply(msg, s)
irc.reply(msg, s)
except webutils.WebError, e:
irc.error(msg, str(e))
_doctypeRe = re.compile(r'(<!DOCTYPE[^>]+>)', re.M) _doctypeRe = re.compile(r'(<!DOCTYPE[^>]+>)', re.M)
def doctype(self, irc, msg, args): def doctype(self, irc, msg, args):
@ -93,16 +90,13 @@ class Http(callbacks.Privmsg):
if not url.startswith('http://'): if not url.startswith('http://'):
irc.error(msg, 'Only HTTP urls are valid.') irc.error(msg, 'Only HTTP urls are valid.')
return return
try: s = webutils.getUrl(url, size=self.maxSize)
s = webutils.getUrl(url, size=self.maxSize) m = self._doctypeRe.search(s)
m = self._doctypeRe.search(s) if m:
if m: s = utils.normalizeWhitespace(m.group(0))
s = utils.normalizeWhitespace(m.group(0)) irc.reply(msg, '%s has the following doctype: %s' % (url, s))
irc.reply(msg, '%s has the following doctype: %s' % (url, s)) else:
else: irc.reply(msg, '%s has no specified doctype.' % url)
irc.reply(msg, '%s has no specified doctype.' % url)
except webutils.WebError, e:
irc.error(msg, str(e))
def size(self, irc, msg, args): def size(self, irc, msg, args):
"""<url> """<url>
@ -114,21 +108,18 @@ class Http(callbacks.Privmsg):
if not url.startswith('http://'): if not url.startswith('http://'):
irc.error(msg, 'Only HTTP urls are valid.') irc.error(msg, 'Only HTTP urls are valid.')
return return
fd = webutils.getUrlFd(url)
try: try:
fd = webutils.getUrlFd(url) size = fd.headers['Content-Length']
try: irc.reply(msg, '%s is %s bytes long.' % (url, size))
size = fd.headers['Content-Length'] except KeyError:
irc.reply(msg, '%s is %s bytes long.' % (url, size)) s = fd.read(self.maxSize)
except KeyError: if len(s) != self.maxSize:
s = fd.read(self.maxSize) irc.reply(msg, '%s is %s bytes long.' % (url, len(s)))
if len(s) != self.maxSize: else:
irc.reply(msg, '%s is %s bytes long.' % (url, len(s))) irc.reply(msg, 'The server didn\'t tell me how long %s is '
else: 'but it\'s longer than %s bytes.' %
irc.reply(msg, 'The server didn\'t tell me how long %s is ' (url,self.maxSize))
'but it\'s longer than %s bytes.' %
(url,self.maxSize))
except webutils.WebError, e:
irc.error(msg, str(e))
def title(self, irc, msg, args): def title(self, irc, msg, args):
"""<url> """<url>
@ -138,16 +129,13 @@ class Http(callbacks.Privmsg):
url = privmsgs.getArgs(args) url = privmsgs.getArgs(args)
if '://' not in url: if '://' not in url:
url = 'http://%s' % url url = 'http://%s' % url
try: text = webutils.getUrl(url, size=self.maxSize)
text = webutils.getUrl(url, size=self.maxSize) 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())) else:
else: irc.reply(msg, 'That URL appears to have no HTML title '
irc.reply(msg, 'That URL appears to have no HTML title ' 'within the first %s bytes.' % self.maxSize)
'within the first %s bytes.' % self.maxSize)
except ValueError, e:
irc.error(msg, str(e))
def freshmeat(self, irc, msg, args): def freshmeat(self, irc, msg, args):
"""<project name> """<project name>
@ -160,6 +148,7 @@ class Http(callbacks.Privmsg):
try: try:
text = webutils.getUrl(url) text = webutils.getUrl(url)
if text.startswith('Error'): if text.startswith('Error'):
text = text.split(None, 1)[1]
raise FreshmeatException, text raise FreshmeatException, text
dom = xml.dom.minidom.parseString(text) dom = xml.dom.minidom.parseString(text)
def getNode(name): def getNode(name):
@ -175,7 +164,7 @@ class Http(callbacks.Privmsg):
'and a popularity of %s, is in version %s.' % \ 'and a popularity of %s, is in version %s.' % \
(project, lastupdated, vitality, popularity, version)) (project, lastupdated, vitality, popularity, version))
except FreshmeatException, e: except FreshmeatException, e:
irc.error(msg, utils.exnToString(e)) irc.error(msg, str(e))
def stockquote(self, irc, msg, args): def stockquote(self, irc, msg, args):
"""<company symbol> """<company symbol>
@ -401,11 +390,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: try:
try: fd = webutils.getUrlFd('http://kernel.org/kdist/finger_banner')
fd = webutils.getUrlFd('http://kernel.org/kdist/finger_banner')
except webutils.WebError, e:
irc.error(msg, str(e))
return
stable = 'unknown' stable = 'unknown'
beta = 'unknown' beta = 'unknown'
for line in fd: for line in fd:
@ -464,10 +449,7 @@ class Http(callbacks.Privmsg):
irc.error(msg, '\'%s\' is an invalid extension character' % c) irc.error(msg, '\'%s\' is an invalid extension character' % c)
return return
s = 'http://www.filext.com/detaillist.php?extdetail=%s&goButton=Go' s = 'http://www.filext.com/detaillist.php?extdetail=%s&goButton=Go'
try: text = webutils.getUrl(s % ext)
text = webutils.getUrl(s % ext)
except webutils.WebError, e:
irc.error(msg, str(e))
matches = self._filextre.findall(text) matches = self._filextre.findall(text)
#print matches #print matches
res = [] res = []

View File

@ -62,6 +62,8 @@ if network:
self.assertNotError('freshmeat supybot') self.assertNotError('freshmeat supybot')
self.assertNotError('freshmeat My Classifieds') self.assertNotError('freshmeat My Classifieds')
self.assertNotRegexp('freshmeat supybot', 'DOM Element') self.assertNotRegexp('freshmeat supybot', 'DOM Element')
m = self.assertNotRegexp('freshmeat asdlfkasjdf','Exception')
self.failIf(m.args[1].count('Error') > 1)
def testTitle(self): def testTitle(self):
self.assertResponse('title slashdot.org', self.assertResponse('title slashdot.org',