[Rte] Improve extractor
[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         ad_m = re.search(
33             r'''value="No.*?" onClick="location.href='([^"']+)'"''', webpage)
34         if ad_m:
35             urlr = compat_urllib_parse_urlparse(url)
36             adfree_url = urlr.scheme + '://' + urlr.netloc + ad_m.group(1)
37             webpage = self._download_webpage(
38                 adfree_url, video_id, note='Download post-ad page')
39
40         error_msg = self._html_search_regex(
41             r'<p class="message">(.*?)</p>', webpage, 'error message',
42             default=None)
43         if error_msg:
44             raise ExtractorError(
45                 '%s said: %s' % (self.IE_NAME, error_msg), expected=True)
46
47         # These clowns alternate between two page types
48         links_code = self._search_regex(
49             r'''(?xs)
50                 (?:
51                     <img\s+src="/im/play.gif".*?>|
52                     <!--\ player\ end\ -->\s*</div><!--\ thumb\ end-->
53                 )
54                 (.*?)
55                 (?:
56                     <a\s+href="fblike|<div\s+class="social">
57                 )
58             ''', webpage, 'links')
59         title = self._html_search_regex(
60             r'<title>(.*?)-\s*Vuclip</title>', webpage, 'title').strip()
61
62         quality_order = qualities(['Reg', 'Hi'])
63         formats = []
64         for url, q in re.findall(
65                 r'<a\s+href="(?P<url>[^"]+)".*?>(?:<button[^>]*>)?(?P<q>[^<]+)(?:</button>)?</a>', links_code):
66             format_id = compat_urllib_parse_urlparse(url).scheme + '-' + q
67             formats.append({
68                 'format_id': format_id,
69                 'url': url,
70                 'quality': quality_order(q),
71             })
72         self._sort_formats(formats)
73
74         duration = parse_duration(self._search_regex(
75             r'\(([0-9:]+)\)</span>', webpage, 'duration', fatal=False))
76
77         return {
78             'id': video_id,
79             'formats': formats,
80             'title': title,
81             'duration': duration,
82         }