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