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