2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
head.py - Phenny HTTP Metadata Utilities
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2011-12-28 17:45:11 -05:00
|
|
|
import re
|
|
|
|
import urllib.request
|
|
|
|
import urllib.parse
|
|
|
|
import urllib.error
|
|
|
|
import http.client
|
|
|
|
import http.cookiejar
|
|
|
|
import time
|
2008-02-21 07:06:33 -05:00
|
|
|
import web
|
|
|
|
from tools import deprecated
|
2012-09-24 21:55:58 -04:00
|
|
|
from modules.linx import get_title
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
cj = http.cookiejar.LWPCookieJar()
|
|
|
|
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
|
|
urllib.request.install_opener(opener)
|
2011-03-05 16:02:22 -05:00
|
|
|
|
2012-09-24 21:55:58 -04:00
|
|
|
|
2008-02-29 10:36:18 -05:00
|
|
|
def head(phenny, input):
|
2011-12-28 17:45:11 -05:00
|
|
|
"""Provide HTTP HEAD information."""
|
|
|
|
uri = input.group(2)
|
|
|
|
uri = (uri or '')
|
|
|
|
if ' ' in uri:
|
|
|
|
uri, header = uri.rsplit(' ', 1)
|
|
|
|
else: uri, header = uri, None
|
|
|
|
|
|
|
|
if not uri and hasattr(phenny, 'last_seen_uri'):
|
|
|
|
try: uri = phenny.last_seen_uri[input.sender]
|
|
|
|
except KeyError: return phenny.say('?')
|
|
|
|
|
|
|
|
if not uri.startswith('htt'):
|
|
|
|
uri = 'http://' + uri
|
|
|
|
# uri = uri.replace('#!', '?_escaped_fragment_=')
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
|
|
|
|
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 = []
|
|
|
|
if 'Status' in info:
|
|
|
|
data.append(info['Status'])
|
|
|
|
if 'content-type' in info:
|
|
|
|
data.append(info['content-type'].replace('; charset=', ', '))
|
|
|
|
if 'last-modified' in info:
|
|
|
|
modified = info['last-modified']
|
|
|
|
modified = time.strptime(modified, '%a, %d %b %Y %H:%M:%S %Z')
|
|
|
|
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()
|
|
|
|
if headerlower in info:
|
|
|
|
phenny.say(header + ': ' + info.get(headerlower))
|
|
|
|
else:
|
|
|
|
msg = 'There was no %s header in the response.' % header
|
|
|
|
phenny.say(msg)
|
2008-02-29 10:36:18 -05:00
|
|
|
head.commands = ['head']
|
|
|
|
head.example = '.head http://www.w3.org/'
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
@deprecated
|
|
|
|
def f_title(self, origin, match, args):
|
2011-12-28 17:45:11 -05:00
|
|
|
""".title <URI> - Return the title of URI."""
|
|
|
|
uri = match.group(2)
|
|
|
|
uri = (uri or '')
|
|
|
|
|
|
|
|
if not uri and hasattr(self, 'last_seen_uri'):
|
|
|
|
uri = self.last_seen_uri.get(origin.sender)
|
|
|
|
if not uri:
|
|
|
|
return self.msg(origin.sender, 'I need a URI to give the title of...')
|
2012-09-24 21:55:58 -04:00
|
|
|
title = get_title(uri)
|
2011-12-28 17:45:11 -05:00
|
|
|
if title:
|
|
|
|
self.msg(origin.sender, origin.nick + ': ' + title)
|
|
|
|
else: self.msg(origin.sender, origin.nick + ': No title found')
|
2011-03-05 20:51:52 -05:00
|
|
|
f_title.commands = ['title']
|
|
|
|
|
2012-06-27 19:33:09 -04:00
|
|
|
r_title = re.compile(r'(?ims)<title[^>]*>(.*?)</title\s*>')
|
|
|
|
r_entity = re.compile(r'&[A-Za-z0-9#]+;')
|
|
|
|
|
2011-03-05 20:51:52 -05:00
|
|
|
def noteuri(phenny, input):
|
2011-12-28 17:45:11 -05:00
|
|
|
uri = input.group(1)
|
|
|
|
if not hasattr(phenny.bot, 'last_seen_uri'):
|
|
|
|
phenny.bot.last_seen_uri = {}
|
|
|
|
phenny.bot.last_seen_uri[input.sender] = uri
|
2011-03-05 20:51:52 -05:00
|
|
|
noteuri.rule = r'.*(http[s]?://[^<> "\x01]+)[,.]?'
|
|
|
|
noteuri.priority = 'low'
|
|
|
|
|
2011-09-22 16:07:26 -04:00
|
|
|
titlecommands = r'(?:' + r'|'.join(f_title.commands) + r')'
|
2011-03-05 20:51:52 -05:00
|
|
|
def snarfuri(phenny, input):
|
2011-12-28 17:45:11 -05:00
|
|
|
if re.match(r'(?i)' + phenny.config.prefix + titlecommands, input.group()):
|
|
|
|
return
|
|
|
|
uri = input.group(1)
|
2012-09-24 21:55:58 -04:00
|
|
|
title = get_title(uri, input.sender)
|
2011-12-28 17:45:11 -05:00
|
|
|
if title:
|
2012-06-27 19:33:09 -04:00
|
|
|
phenny.msg(input.sender, title)
|
2011-03-05 20:51:52 -05:00
|
|
|
snarfuri.rule = r'.*(http[s]?://[^<> "\x01]+)[,.]?'
|
|
|
|
snarfuri.priority = 'low'
|
2012-09-24 23:03:15 -04:00
|
|
|
snarfuri.thread = True
|
2008-02-29 10:36:18 -05:00
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-12-28 17:45:11 -05:00
|
|
|
print(__doc__.strip())
|