-
Notifications
You must be signed in to change notification settings - Fork 84
/
flashscraper.py
407 lines (354 loc) · 15.5 KB
/
flashscraper.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Miro - an RSS based video player application
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
# Participatory Culture Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
#
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
"""``miro.flashscraper`` -- functions for converting a web-page url
to a media url.
"""
import logging
import re
from miro import httpclient
import urlparse
import cgi
from xml.dom import minidom
from urllib import unquote_plus, urlencode
from miro.util import check_u
try:
import simplejson as json
except ImportError:
import json
def is_maybe_flashscrapable(url):
"""Returns whether or not the given url is possibly handled by one
of the flash url converters we have.
Example:
>>> is_maybe_flashscrapable(u"http://www.youtube.com/watch?v=qRuNxHqwazs")
True
"""
return _get_scrape_function_for(url) is not None
def try_scraping_url(url, callback):
check_u(url)
scrape = _get_scrape_function_for(url)
if scrape is not None:
scrape(url,
lambda newurl, content_type=u"video/x-flv", title=None: _actual_url_callback(url, callback, newurl, content_type, title))
else:
callback(url)
# =============================================================================
# The callback is wrapped in this for flv videos
def _actual_url_callback(url, callback, new_url, content_type, title):
if new_url:
check_u(new_url)
callback(new_url, content_type=content_type, title=title)
def _get_scrape_function_for(url):
check_u(url)
for scrape_info in SCRAPER_INFO_MAP:
if scrape_info['pattern'].match(url) is not None:
return scrape_info['func']
return None
def _scrape_youtube_url(url, callback):
check_u(url)
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
video_id = None
if components[2] == u'/watch' and 'v' in params:
try:
video_id = params['v'][0]
except IndexError:
pass
elif components[2].startswith('/v/'):
m = re.compile(r'/v/([\w-]+)').match(components[2])
if m is not None:
video_id = m.group(1)
if video_id is None:
logging.warning('_scrape_youtube_url: unable to scrape YouTube Video URL')
callback(None)
return
try:
url = u"http://www.youtube.com/get_video_info?video_id=%s&el=embedded&ps=default&eurl=" % video_id
httpclient.grab_url(
url,
lambda x: _youtube_callback_step2(x, video_id, callback),
lambda x: _youtube_errback(x, callback))
except StandardError:
logging.exception("youtube_callback: unable to scrape YouTube Video URL")
callback(None)
def _youtube_callback_step2(info, video_id, callback):
try:
body = info['body']
params = cgi.parse_qs(body)
if params.get("status", [""])[0] == "fail":
logging.warning("youtube download failed because: %s",
params.get("reason", ["unknown"])[0])
callback(None)
return
# fmt_url_map is a comma separated list of pipe separated
# pairs of fmt, url
# build the format codes.
fmt_list = [x.split('/')[0] for x in params['fmt_list'][0].split(',')]
# build the list of available urls.
stream_map = params["url_encoded_fmt_stream_map"][0].split(",")
fmt_url_map = dict()
# strip url= from url=xxxxxx, strip trailer. Strip duplicate params.
for fmt, stream_map_data in zip(fmt_list, stream_map):
stream_map = cgi.parse_qs(stream_map_data)
fmt_url_map[fmt] = stream_map['url'][0]
title = params.get("title", ["No title"])[0]
try:
title = title.decode("utf-8")
except UnicodeDecodeError:
title = title.decode("ascii", "ignore")
logging.debug("fmt_url_map keys: %s", fmt_url_map.keys())
# http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs
for fmt, content_type in [("22", u"video/mp4"),
("18", u"video/mp4"),
("5", u"video/x-flv")]:
if fmt in fmt_url_map:
new_url = fmt_url_map[fmt]
logging.debug("youtube download: trying %s %s", fmt, new_url)
callback(
unicode(new_url), content_type=content_type,
title=title)
return
_youtube_errback(info, callback)
except StandardError:
logging.exception("youtube_callback_step2: unable to scrape YouTube URL")
callback(None)
def _youtube_errback(err, callback):
logging.warning("youtube_errback: network error scraping YouTube url %s", err)
callback(None)
def _scrape_google_video_url(url, callback):
try:
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
doc_id = params['docId'][0]
url = (u"http://video.google.com/videofile/%s.flv?docid=%s&itag=5" %
(doc_id, doc_id))
callback(url)
except StandardError:
logging.warning("unable to scrape Google Video URL: %s", url)
callback(None)
def _scrape_lulu_video_url(url, callback):
try:
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
url = unquote_plus(params['file'][0]).decode('ascii', 'replace')
callback(url)
except StandardError:
logging.warning("unable to scrape LuLu.tv Video URL: %s", url)
callback(None)
def _scrape_vmix_video_url(url, callback):
try:
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
type_ = params['type'][0]
id_ = params['id'][0]
l = params['l'][0]
url = (u"http://sdstage01.vmix.com/videos.php?type=%s&id=%s&l=%s" %
(type_, id_, l))
httpclient.grab_url(url, lambda x: _scrape_vmix_callback(x, callback),
lambda x: _scrape_vmix_errback(x, callback))
except StandardError:
logging.warning("unable to scrape VMix Video URL: %s", url)
callback(None)
def _scrape_vmix_callback(info, callback):
try:
doc = minidom.parseString(info['body'])
url = doc.getElementsByTagName('file').item(0).firstChild.data.decode('ascii', 'replace')
callback(url)
except StandardError:
logging.warning("unsable to scrape XML for VMix Video URL %s",
info['redirected-url'])
callback(None)
def _scrape_vmix_errback(err, callback):
logging.warning("network error scraping VMix Video URL")
callback(None)
def _scrape_vsocial_video_url(url, callback):
try:
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
v = params['v'][0]
url = u'http://static.vsocial.com/varmedia/vsocial/flv/%s_out.flv' % v
callback(url)
except StandardError:
logging.warning("unable to scrape VSocial URL: %s", url)
callback(None)
def _scrape_veohtv_video_url(url, callback):
try:
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
t = params['type'][0]
permalink_id = params['permalinkId'][0]
url = u'http://www.veoh.com/movieList.html?type=%s&permalinkId=%s&numResults=45' % (t, permalink_id)
httpclient.grab_url(url, lambda x: _scrape_veohtv_callback(x, callback),
lambda x: _scrape_veohtv_errback(x, callback))
except StandardError:
logging.warning("unable to scrape Veoh URL: %s", url)
callback(None)
def _scrape_veohtv_callback(info, callback):
url = info['redirected-url']
try:
params = cgi.parse_qs(info['body'])
file_hash = params['previewHashLow'][0]
if file_hash.endswith(","):
file_hash = file_hash[:-1]
url = (u'http://ll-previews.veoh.com/previews/get.jsp?fileHash=%s' %
file_hash)
callback(url)
except StandardError:
logging.warning("unable to scrape Veoh URL data: %s", url)
callback(None)
def _scrape_veohtv_errback(err, callback):
logging.warning("network error scraping Veoh TV Video URL")
callback(None)
def _scrape_break_video_url(url, callback):
httpclient.grab_headers(url, lambda x: _scrape_break_callback(x, callback),
lambda x: _scrape_break_errback(x, callback))
def _scrape_break_callback(info, callback):
url = info['redirected-url']
try:
components = urlparse.urlsplit(url)
params = cgi.parse_qs(components[3])
url = unquote_plus(params['sVidLoc'][0]).decode('ascii', 'replace')
callback(url)
except StandardError:
logging.warning("unable to scrape Break URL: %s", url)
callback(None)
def _scrape_break_errback(info, callback):
logging.warning("network error scraping Break Video URL")
callback(None)
def _scrape_green_peace_video_url(url, callback):
logging.warning("unable to scrape Green peace Video URL %s", url)
callback(None)
VIMEO_RE = re.compile(r'http://([^/]+\.)?vimeo.com/[^\d]*(\d+)')
def _scrape_vimeo_video_url(url, callback, countdown=10):
try:
id_ = VIMEO_RE.match(url).group(2)
download_url = 'http://vimeo.com/%s?action=download' % id_
httpclient.grab_url(
download_url,
lambda x: _scrape_vimeo_download_callback(x, callback),
lambda x: _scrape_vimeo_video_url_try_2(url, callback, id_),
extra_headers={
'Referer': 'http://vimeo.com/%s' % id_,
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': ('Mozilla/5.0 (X11; Linux x86_64) '
'AppleWebKit/536.11 (KHTML, like Gecko) '
'Chrome/20.0.1132.8 Safari/536.11')
})
except StandardError:
logging.exception("Unable to scrape vimeo.com video URL: %s", url)
callback(None)
VIMEO_LINK_RE = re.compile('<a href="/(.*?)" download=".*?_(\d+)x(\d+).mp4"')
def _scrape_vimeo_download_callback(info, callback):
"""
Currently, Vimeo returns links like this:
* Mobile HD
* HD
* SD
* Original
We grab them all, and callback the one with the largest width by height.
"""
largest_url, size = None, 0
try:
for url, width, height in VIMEO_LINK_RE.findall(info['body']):
trial_size = int(width) * int(height)
if int(width) * int(height) > size:
largest_url, size = url, trial_size
except:
logging.exception('during parse of Vimeo response for %r',
info['original-url'])
callback(None)
if largest_url is not None:
callback(u'http://vimeo.com/%s' % largest_url,
content_type=u'video/mp4')
else:
_scrape_vimeo_download_errback("no largest url", callback,
info['original-url'])
def _scrape_vimeo_video_url_try_2(url, callback, vimeo_id):
"""Try scraping vimeo URLs by scraping the javascript code.
This method seems less reliable than the regular method, but it works for
private videos. See #19305
"""
video_url = u'http://vimeo.com/%s' % vimeo_id
httpclient.grab_url(
video_url,
lambda x: _scrape_vimeo_download_try_2_callback(x, callback,
vimeo_id),
lambda x: _scrape_vimeo_download_errback(x, callback, url))
VIMEO_JS_DATA_SCRAPE_RE = re.compile(r'clip[0-9_]+\s*=\s*(.*}});')
VIMEO_SCRAPE_SIG_RE = re.compile(r'"signature":"([0-9a-fA-F]+)"')
VIMEO_SCRAPE_TIMESTAMP_RE = re.compile(r'"timestamp":([0-9]+)')
VIMEO_SCRAPE_FILES_RE = re.compile(r'"files":({[^}]+})')
def _scrape_vimeo_download_try_2_callback(info, callback, vimeo_id):
# first step is to find the javascript code that we care about in the HTML
# page
m = VIMEO_JS_DATA_SCRAPE_RE.search(info['body'])
if m is None:
logging.warn("Unable to scrape %s for JSON", info['original-url'])
callback(None)
return
json_data = m.group(1)
try:
signature = VIMEO_SCRAPE_SIG_RE.search(json_data).group(1)
timestamp = VIMEO_SCRAPE_TIMESTAMP_RE.search(json_data).group(1)
files_str = VIMEO_SCRAPE_FILES_RE.search(json_data).group(1)
except AttributeError:
# one of the RE's retured None
logging.warn("Unable to scrape %s", info['original-url'])
callback(None)
return
try:
files_data = json.loads(files_str)
codec = files_data.keys()[0]
quality = files_data[codec][0]
except StandardError:
logging.warn("Unable to scrape vimeo files variable (%s)",
files_match.group(1))
callback(None)
url = ('http://player.vimeo.com/play_redirect?'
'clip_id=%s&quality=%s&codecs=%s&time=%s'
'&sig=%s&type=html5_desktop_local' %
(vimeo_id, quality, codec, timestamp, signature))
logging.debug("_scrape_vimeo_download_try_2_callback scraped URL: %s",
url)
callback(url)
def _scrape_vimeo_download_errback(err, callback, url):
logging.warning("Unable to scrape %r\nerror: %s", url, err)
callback(None)
# =============================================================================
SCRAPER_INFO_MAP = [
{'pattern': re.compile(r'https?://([^/]+\.)?youtube.com/(watch|v)'), 'func': _scrape_youtube_url},
{'pattern': re.compile(r'http://video.google.com/googleplayer.swf'), 'func': _scrape_google_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?lulu.tv/wp-content/flash_play/flvplayer'), 'func': _scrape_lulu_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?vmix.com/flash/super_player.swf'), 'func': _scrape_vmix_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?vsocial.com/flash/vp.swf'), 'func': _scrape_vsocial_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?veoh.com/multiplayer.swf'), 'func': _scrape_veohtv_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?greenpeaceweb.org/GreenpeaceTV1Col.swf'), 'func': _scrape_green_peace_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?break.com/'), 'func': _scrape_break_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?vimeo.com/\d+'), 'func': _scrape_vimeo_video_url},
{'pattern': re.compile(r'http://([^/]+\.)?vimeo.com/moogaloop.swf'), 'func': _scrape_vimeo_video_url},
]