-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp_meta.py
More file actions
316 lines (245 loc) · 10.1 KB
/
Copy pathhttp_meta.py
File metadata and controls
316 lines (245 loc) · 10.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import re
import urllib.parse
import urllib.request
import urllib.parse
import urllib.error
import time
from http.server import BaseHTTPRequestHandler
from html.parser import HTMLParser
import requests
import chardet
import html.entities
from hurry import filesize
from twitter import Twitter, OAuth
import plugin
defaults = {
'http_url_blacklist': {
# '#we-hate-imgur': ['imgur\.com'],
},
# whether to print mime-type and other metadata if <title> is unavailable
'http_mime': True,
'twitter_oath': {'token': '',
'token_secret': '',
'consumer_key': '',
'consumer_secret': ''}
}
# HTML MIME types.
HTML_TYPES = ('text/html', 'application/xhtml+xml')
# HTTP status codes appropriate for detecting normal redirection.
REDIRECT_CODES = (301, 302, 303)
# A structure required by hurry.filesize for custom suffixes.
FILESIZES = [
(1024 ** 5, ' PiB'),
(1024 ** 4, ' TiB'),
(1024 ** 3, ' GiB'),
(1024 ** 2, ' MiB'),
(1024 ** 1, ' KiB'),
(1024 ** 0, ' B'),
]
# Regex range that matches all Unicode characters except the C0 (U+0000-U+001F)
# and C1 (U+007F-U+009F) control characters and the space (U+0020)
ALL_CONTROLS_AND_SPACE = '[^\u0000-\u0020\u007f-\u009f]'
YOUTUBE_API_URL = 'http://www.youtube.com/get_video_info?video_id=%s'
FOURCHAN_API_URL = 'http://api.4chan.org/'
FOURCHAN_POST_API = FOURCHAN_API_URL + '%s/res/%s'
def ajax_url(url):
"""
AJAX HTML snapshot URL parsing
Take a URL string, turn its #! fragment into the prescribed query, and
return a string.
https://developers.google.com/webmasters/ajax-crawling/docs/specification
"""
hashbang_index = url.find('#!')
if hashbang_index != -1:
base = url[:hashbang_index]
joiner = '?' if '?' not in base else '&'
url = ''.join((base, joiner, '_escaped_fragment_=',
urllib.parse.quote(url[hashbang_index+2:],
'!"$\'()*,/:;<=>?@[\\]^`{|}~')))
return url
def prettify_url(url):
"""
Remove URL baggage to display a clean hostname/path.
Returns a string when passed either a string or a urlparse.ParseResult.
"""
if not isinstance(url, urllib.parse.ParseResult):
url = urllib.parse.urlparse(url)
urlstr = url.hostname + url.path
return urlstr
class NoTitleError(Exception):
"""
This page has no title :<
"""
# http://effbot.org/zone/re-sub.htm#unescape-html
def html_unescape(text):
"""
Remove HTML or XML character references and entities from a text string.
"""
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return chr(int(text[3:-1], 16))
else:
return chr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
text = chr(html.entities.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
return re.sub("&#?\w+;", fixup, text)
class Plugin(plugin.Plugin):
""" Returns metadata about HTTP URLs. """
http_frame = r'(?i).*https?://(www\.)?%s'
def prepare(self):
self.handlers = [
(self.http_frame % 'twitter\.com/.*', self.twitter),
(self.http_frame % 'youtube\.com/.*', self.youtube),
(self.http_frame % 'youtu\.be/.*', self.youtube),
(self.http_frame % '(boards\.)?4chan.com/.*', self.fourchan),
]
def postfork(self):
self.tw_api = Twitter(auth=OAuth(**self.conf['twitter_oath']))
self.parser = HTMLParser()
def register_commands(self):
self.regexes = [
(self.http_frame % ALL_CONTROLS_AND_SPACE, self.httpmeta)
]
def url_list(self, message):
urls = re.findall('(?i)https?://%s+'
% ALL_CONTROLS_AND_SPACE, message.content)
try:
blacklist = '(?i)'
for b in self.conf['http_url_blacklist'][message.source]:
blacklist += b + '|'
blacklist = blacklist[0:-1]
urls = [url for url in urls if not re.search(blacklist, url)]
except KeyError:
pass
return urls
def httpmeta(self, message, args):
"""
$https://twitter.com/#!/camh/statuses/147449116551680001
>Twitter / Cameron Kenley Hunt: There are only three hard ...
>\x03#|\x03 \x02twitter.com\x02
"""
urls = self.url_list(message)
for url in urls:
for regex, handler in self.handlers:
if re.match(regex, url):
our_handler = handler
break
else:
our_handler = self.title
result = our_handler(url)
if (our_handler != self.title) and (not result):
result = self.title(url)
self.send_struc(message.source, result)
def twitter(self, url):
match = re.search(r'(?<=status/)\d+(?=/|$)', url)
if not match:
return None
tweet_id = match.group(0)
tweet = self.tw_api.statuses.show(id=tweet_id)
author = tweet['user']
return ((self.parser.unescape(tweet['text']),
'\x02%s\x02 (%s)' % (author['name'], author['screen_name'])))
def youtube(self, url):
match = re.search(r'\b[a-zA-Z0-9\-_]{11}\b', url)
if not match:
return None
video_id = match.group(0)
resource = requests.get(YOUTUBE_API_URL % video_id)
video = urllib.parse.parse_qs(resource.text)
if video['status'][0] == 'fail':
return None
total_seconds = int(video['length_seconds'][0])
seconds = total_seconds % 60
minutes = (total_seconds - seconds) / 60
return (video['title'][0], '%i:%02i' % (minutes, seconds),
'%s views' % video['view_count'][0])
def fourchan(self, url):
threadmatch = re.search(url, url)
def title(self, url):
url_parsed = urllib.parse.urlparse(url)
url_hostname = url_parsed.hostname
url = ajax_url(self.irc.strip_formatting(url))
request_headers = {
'User-Agent': ('Mozilla/5.0 (Windows; U; Windows NT 5.1; it; '
'rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')}
start_time = time.time()
try:
resource = requests.head(url, headers=request_headers,
allow_redirects=True)
if resource.status_code == 501:
resource = requests.get(url, headers=request_headers,
allow_redirects=True)
if resource.status_code == 405:
request_headers['Range'] = 'bytes=1-5'
resource = requests.get(url, headers=request_headers,
allow_redirects=True)
del request_headers['Range']
else:
resource.raise_for_status()
if resource.history != [] and (resource.history[-1].status_code
in REDIRECT_CODES):
url = resource.history[-1].headers['Location']
redirection_url = urllib.parse.urlparse(url)
if redirection_url.netloc == '':
url = ''.join((url_parsed.scheme, '://', url_parsed.netloc,
redirection_url.path))
elif redirection_url.hostname != url_hostname:
url_hostname = '%s \x03#->\x03 %s' % (url_hostname,
prettify_url(url))
url = ajax_url(url)
resource_type = resource.headers['Content-Type'].split(';')[0]
if resource_type in HTML_TYPES:
resource = requests.get(url, headers=request_headers)
resource.raise_for_status()
# RFC 2616 HTTP1.1__ discourages this, but then again it also
# doesn't require the charset to be specified.
# The requests library, in accordance with the RFC, falls back
# to Latin-1 if charset is not in the Content-Type header.
# This conditional at least ensures that the charset is
# checked, even if the result is incorrect.
# https://github.com/kennethreitz/requests/issues/592
if resource.encoding == 'ISO-8859-1':
resource.encoding = chardet.detect(resource.content
)['encoding']
try:
title = re.findall('(?si)(?<=<title).*?>.*?(?=</title>)',
resource.text)[0]
title = re.sub('.*?>', '', title)
except IndexError:
raise NoTitleError
title = re.sub('(?s)\s+', ' ', html_unescape(title).strip())
else:
# TODO: Make this feature togglable, since it can seem spammy
# for image dumps.
raise NoTitleError
except requests.exceptions.ConnectionError:
title = 'server connection error'
except requests.exceptions.HTTPError as httpe:
title = '%s %s'.lower() % (
httpe.response.status_code,
BaseHTTPRequestHandler.responses[httpe.response.status_code][0]
)
except NoTitleError:
if not self.conf.conf['http_mime']:
# stop here
return None
try:
data_length = filesize.size(float(
resource.headers['Content-Length']), FILESIZES)
except TypeError:
data_length = 'size unknown'
title = '%s \x03#|\x03 %s' % (resource_type, data_length)
end_time = time.time()
time_length = '%.2f seconds' % (end_time - start_time)
return ((title, time_length, '\x02%s\x02' % url_hostname))