summaryrefslogtreecommitdiff
path: root/modules/url_titles.py
blob: fc750f86df30a303b602bfde05eb1de84e4ff49d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
info = {
	'author': 'Jon Bergli Heier',
	'title': 'URL Titles',
	'description': 'Fetches the title tags off of URLs.',
}

cfg_section = 'module/url_titles'

import re, urllib2, htmlentitydefs, gzip, cStringIO, time
from PIL import ImageFile

class Module:
	re_http = re.compile(r'(https?://[^\ #]+)')
	re_title = re.compile(r'<title[^>]*?>(.*?)</title>', re.S | re.I)
	re_meta = re.compile(r'<meta\s+http-equiv="content-type"\s+content="[^"]+charset=([^"]+)"', re.I)

	def __init__(self, bot):
		self.irc = bot
		if bot:
			self.irc.register_callback(self)

	def get_titles(self, s):
		def parse_url(url):
			s = url[7:].split('/', 1)[0:]
			host = s[0]
			path = '/' if len(s) == 1 else '/' + s[1]
			return host, path

		def unescape(text):
			def fixup(m):
				text = m.group(0)
				if text[:2] == "&#":
					# character reference
					try:
						if text[:3] == "&#x":
							return unichr(int(text[3:-1], 16)).encode('utf-8')
						else:
							return unichr(int(text[2:-1])).encode('utf-8')
					except ValueError:
						pass
				else:
					# named entity
					try:
						text = unichr(htmlentitydefs.name2codepoint[text[1:-1]]).encode('utf-8')
					except KeyError:
						pass
				return text # leave as is
			return re.sub("&#?\w+;", fixup, text)

		def format_text(s):
			s = s.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ')
			while '  ' in s:
				s = s.replace('  ', ' ')
			s = unescape(s)
			return s

		m = self.re_http.findall(s)
		titles = []
		for url in m:
			# ignore spotify URLs
			if 'open.spotify.com' in url:
				continue
			
			# Run AniDB URLs through AniDB module.
			m = re.match('http://anidb.net/(perl-bin/animedb.pl\?show=anime&aid=|a)(\d+)', url)
			if m and 'anidb' in self.irc.modules:
				aid = int(m.groups()[1])
				titles.append(self.irc.modules['anidb'].get_info(aid))
				continue
			
			t = time.time()
			try:
				u = urllib2.urlopen(url, timeout = config.getfloat(cfg_section, 'timeout'))
			except:
				return
			if not 'content-type' in u.headers:
				u.close()
				continue
			if u.headers['content-type'].startswith('text/html'):
				#s = u.read()
				if 'content-encoding' in u.headers and u.headers['content-encoding'] == 'gzip':
					s = cStringIO.StringIO(u.read())
					s.seek(0)
					s = gzip.GzipFile(fileobj = s).read()
					m = self.re_title.search(s)
					meta_enc = self.re_meta.search(s)
				else:
					s = ''
					m = None
					meta_enc = None
					buf = u.read(1024)
					while buf and time.time() - t < 5.0:
						s += buf
						m = self.re_title.search(s)
						meta_enc = self.re_meta.search(s)
						if m and (meta_enc or '</head>' in s):
							break
						buf = u.read(1024)
				ct = u.headers['content-type']
				enc = ct.lower().split('charset=')
				if len(enc) == 2:
					enc = enc[1]
				elif meta_enc:
					enc = meta_enc.groups()[0]
				else:
					enc = None

				if m:
					s = m.groups()[0]
					if enc:
						s = s.decode(enc, 'replace').encode('utf8', 'replace')
					titles.append(s)
			elif u.headers['content-type'] in ('image/gif', 'image/png', 'image/jpeg'):
				def pretty_size(size):
					suffixes = (('B', 2**10), ('KiB', 2**20), ('MiB', 2**30), ('GiB', 2**40), ('TiB', 2**50))
					for suf, lim in suffixes:
						if size > lim:
							continue
						else:
							return '%s %s' % (str(round(size/float(lim/2**10), 2)), suf)
				p = ImageFile.Parser()
				size = 0
				while time.time() - t < 5.0:
					s = u.read(1024)
					size += len(s)
					if not s:
						break
					p.feed(s)
				try:
					im = None
					im = p.close()
					titles.append('%s image: %dx%d (%s)' % ((im.format,) + tuple(im.size) + (pretty_size(size),)))
				except:
					pass
				finally:
					del im
			u.close()
		if len(titles) == 0:
			return
		elif len(titles) == 1:
			s = format_text(titles[0])
		else:
			s = ''
			for i in range(len(titles)):
				s += '\002[%d]\002 %s ' % (i+1, format_text(titles[i]))
		return s.strip()

	def privmsg(self, nick, channel, msg):
		titles = self.get_titles(msg)
		if titles:
			self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], titles)

if __name__ == '__main__':
	import sys, ConfigParser, os
	config = ConfigParser.ConfigParser()
	config.read([os.path.expanduser('~/.fot')])
	m = Module(None)
	print m.get_titles(' '.join(sys.argv[1:]))