head: fix .head and add response time

master
mutantmonkey 2011-12-28 17:45:11 -05:00
parent 9594c33266
commit 92ea5b5310
1 changed files with 143 additions and 136 deletions

View File

@ -7,7 +7,13 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import re, urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.client, urllib.parse, time, http.cookiejar
import re
import urllib.request
import urllib.parse
import urllib.error
import http.client
import http.cookiejar
import time
from html.entities import name2codepoint
import web
from tools import deprecated
@ -32,19 +38,19 @@ def head(phenny, input):
uri = 'http://' + uri
# uri = uri.replace('#!', '?_escaped_fragment_=')
try: info = web.head(uri)
except IOError: return phenny.say("Can't connect to %s" % uri)
except http.client.InvalidURL: return phenny.say("Not a valid URI, sorry.")
start = time.time()
if not isinstance(info, list):
try: info = dict(info)
except TypeError:
return phenny.reply('Try .head http://example.org/ [optional header]')
info['Status'] = '200'
else:
newInfo = dict(info[0])
newInfo['Status'] = str(info[1])
info = newInfo
try:
info = web.head(uri)
info['status'] = '200'
except urllib.error.HTTPError as e:
return phenny.say(str(e.code))
except http.client.InvalidURL:
return phenny.say("Not a valid URI, sorry.")
except IOError:
return phenny.say("Can't connect to %s" % uri)
resptime = time.time() - start
if header is None:
data = []
@ -58,6 +64,7 @@ def head(phenny, input):
data.append(time.strftime('%Y-%m-%d %H:%M:%S UTC', modified))
if 'content-length' in info:
data.append(info['content-length'] + ' bytes')
data.append('{0:1.2f} s'.format(resptime))
phenny.reply(', '.join(data))
else:
headerlower = header.lower()