[vuclip] Fix extraction
[youtube-dl] / youtube_dl / extractor / vuclip.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urllib_parse_urlparse,
8     ExtractorError,
9     parse_duration,
10     qualities,
11 )
12
13
14 class VuClipIE(InfoExtractor):
15     _VALID_URL = r'http://(?:m\.)?vuclip\.com/w\?.*?cid=(?P<id>[0-9]+)'
16
17     _TEST = {
18         'url': 'http://m.vuclip.com/w?cid=922692425&fid=70295&z=1010&nvar&frm=index.html',
19         'info_dict': {
20             'id': '922692425',
21             'ext': '3gp',
22             'title': 'The Toy Soldiers - Hollywood Movie Trailer',
23             'duration': 180,
24         }
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.group('id')
30
31         #webpage = self._download_webpage(url, video_id)
32         import io
33         webpage = io.open('922692425_http_-_m.vuclip.com_wcid=922692425_fid=70295_z=1010_nvar_frm=index.html.dump', encoding='utf-8').read()
34         ad_m = re.search(
35             r'''value="No.*?" onClick="location.href='([^"']+)'"''', webpage)
36         if ad_m:
37             urlr = compat_urllib_parse_urlparse(url)
38             adfree_url = urlr.scheme + '://' + urlr.netloc + ad_m.group(1)
39             webpage = self._download_webpage(
40                 adfree_url, video_id, note='Download post-ad page')
41
42         error_msg = self._html_search_regex(
43             r'<p class="message">(.*?)</p>', webpage, 'error message',
44             default=None)
45         if error_msg:
46             raise ExtractorError(
47                 '%s said: %s' % (self.IE_NAME, error_msg), expected=True)
48
49         # These clowns alternate between two page types
50         links_code = self._search_regex(
51             r'''(?xs)
52                 (?:
53                     <img\s+src="/im/play.gif".*?>|
54                     <!--\ player\ end\ -->\s*</div><!--\ thumb\ end-->
55                 )
56                 (.*?)
57                 (?:
58                     <a\s+href="fblike'|<div\s+class="social">
59                 )
60             ''', webpage, 'links')
61         title = self._html_search_regex(
62             r'<title>(.*?)-\s*Vuclip</title>', webpage, 'title').strip()
63
64         quality_order = qualities(['Reg', 'Hi'])
65         formats = []
66         for url, q in re.findall(
67                 r'<a\s+href="(?P<url>[^"]+)".*?>(?:<button[^>]*>)?(?P<q>[^<]+)(?:</button>)?</a>', links_code):
68             format_id = compat_urllib_parse_urlparse(url).scheme + '-' + q
69             formats.append({
70                 'format_id': format_id,
71                 'url': url,
72                 'quality': quality_order(q),
73             })
74         self._sort_formats(formats)
75
76         duration = parse_duration(self._search_regex(
77             r'\(([0-9:]+)\)</span>', webpage, 'duration', fatal=False))
78
79         return {
80             'id': video_id,
81             'formats': formats,
82             'title': title,
83             'duration': duration,
84         }