6b15fc2e530a84e99ed194fee0024f8ec6c4c2b8
[youtube-dl] / youtube_dl / extractor / novamov.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_request,
8     compat_urlparse,
9 )
10 from ..utils import (
11     ExtractorError,
12     NO_DEFAULT,
13     encode_dict,
14     urlencode_postdata,
15 )
16
17
18 class NovaMovIE(InfoExtractor):
19     IE_NAME = 'novamov'
20     IE_DESC = 'NovaMov'
21
22     _VALID_URL_TEMPLATE = r'http://(?:(?:www\.)?%(host)s/(?:file|video)/|(?:(?:embed|www)\.)%(host)s/embed\.php\?(?:.*?&)?v=)(?P<id>[a-z\d]{13})'
23     _VALID_URL = _VALID_URL_TEMPLATE % {'host': 'novamov\.com'}
24
25     _HOST = 'www.novamov.com'
26
27     _FILE_DELETED_REGEX = r'This file no longer exists on our servers!</h2>'
28     _FILEKEY_REGEX = r'flashvars\.filekey="(?P<filekey>[^"]+)";'
29     _TITLE_REGEX = r'(?s)<div class="v_tab blockborder rounded5" id="v_tab1">\s*<h3>([^<]+)</h3>'
30     _DESCRIPTION_REGEX = r'(?s)<div class="v_tab blockborder rounded5" id="v_tab1">\s*<h3>[^<]+</h3><p>([^<]+)</p>'
31
32     _TEST = {
33         'url': 'http://www.novamov.com/video/4rurhn9x446jj',
34         'md5': '7205f346a52bbeba427603ba10d4b935',
35         'info_dict': {
36             'id': '4rurhn9x446jj',
37             'ext': 'flv',
38             'title': 'search engine optimization',
39             'description': 'search engine optimization is used to rank the web page in the google search engine'
40         },
41         'skip': '"Invalid token" errors abound (in web interface as well as youtube-dl, there is nothing we can do about it.)'
42     }
43
44     def _real_extract(self, url):
45         video_id = self._match_id(url)
46
47         url = 'http://%s/video/%s' % (self._HOST, video_id)
48
49         webpage = self._download_webpage(
50             url, video_id, 'Downloading video page')
51
52         if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
53             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
54
55         def extract_filekey(default=NO_DEFAULT):
56             return self._search_regex(
57                 self._FILEKEY_REGEX, webpage, 'filekey', default=default)
58
59         filekey = extract_filekey(default=None)
60
61         if not filekey:
62             fields = self._hidden_inputs(webpage)
63             post_url = self._search_regex(
64                 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', webpage,
65                 'post url', default=url, group='url')
66             if not post_url.startswith('http'):
67                 post_url = compat_urlparse.urljoin(url, post_url)
68             request = compat_urllib_request.Request(
69                 post_url, urlencode_postdata(encode_dict(fields)))
70             request.add_header('Content-Type', 'application/x-www-form-urlencoded')
71             request.add_header('Referer', post_url)
72             webpage = self._download_webpage(
73                 request, video_id, 'Downloading continue to the video page')
74
75         filekey = extract_filekey()
76
77         title = self._html_search_regex(self._TITLE_REGEX, webpage, 'title', fatal=False)
78         description = self._html_search_regex(self._DESCRIPTION_REGEX, webpage, 'description', default='', fatal=False)
79
80         api_response = self._download_webpage(
81             'http://%s/api/player.api.php?key=%s&file=%s' % (self._HOST, filekey, video_id), video_id,
82             'Downloading video api response')
83
84         response = compat_urlparse.parse_qs(api_response)
85
86         if 'error_msg' in response:
87             raise ExtractorError('%s returned error: %s' % (self.IE_NAME, response['error_msg'][0]), expected=True)
88
89         video_url = response['url'][0]
90
91         return {
92             'id': video_id,
93             'url': video_url,
94             'title': title,
95             'description': description
96         }