Merge remote-tracking branch 'rzhxeo/embedly'
[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 ..utils import (
7     ExtractorError,
8     compat_urlparse
9 )
10
11
12 class NovaMovIE(InfoExtractor):
13     IE_NAME = 'novamov'
14     IE_DESC = 'NovaMov'
15
16     _VALID_URL = r'http://(?:(?:www\.)?%(host)s/video/|(?:(?:embed|www)\.)%(host)s/embed\.php\?(?:.*?&)?v=)(?P<videoid>[a-z\d]{13})' % {'host': 'novamov\.com'}
17
18     _HOST = 'www.novamov.com'
19
20     _FILE_DELETED_REGEX = r'This file no longer exists on our servers!</h2>'
21     _FILEKEY_REGEX = r'flashvars\.filekey="(?P<filekey>[^"]+)";'
22     _TITLE_REGEX = r'(?s)<div class="v_tab blockborder rounded5" id="v_tab1">\s*<h3>([^<]+)</h3>'
23     _DESCRIPTION_REGEX = r'(?s)<div class="v_tab blockborder rounded5" id="v_tab1">\s*<h3>[^<]+</h3><p>([^<]+)</p>'
24
25     _TEST = {
26         'url': 'http://www.novamov.com/video/4rurhn9x446jj',
27         'md5': '7205f346a52bbeba427603ba10d4b935',
28         'info_dict': {
29             'id': '4rurhn9x446jj',
30             'ext': 'flv',
31             'title': 'search engine optimization',
32             'description': 'search engine optimization is used to rank the web page in the google search engine'
33         },
34         'skip': '"Invalid token" errors abound (in web interface as well as youtube-dl, there is nothing we can do about it.)'
35     }
36
37     def _real_extract(self, url):
38         mobj = re.match(self._VALID_URL, url)
39         video_id = mobj.group('videoid')
40
41         page = self._download_webpage(
42             'http://%s/video/%s' % (self._HOST, video_id), video_id, 'Downloading video page')
43
44         if re.search(self._FILE_DELETED_REGEX, page) is not None:
45             raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
46
47         filekey = self._search_regex(self._FILEKEY_REGEX, page, 'filekey')
48
49         title = self._html_search_regex(self._TITLE_REGEX, page, 'title', fatal=False)
50
51         description = self._html_search_regex(self._DESCRIPTION_REGEX, page, 'description', default='', fatal=False)
52
53         api_response = self._download_webpage(
54             'http://%s/api/player.api.php?key=%s&file=%s' % (self._HOST, filekey, video_id), video_id,
55             'Downloading video api response')
56
57         response = compat_urlparse.parse_qs(api_response)
58
59         if 'error_msg' in response:
60             raise ExtractorError('%s returned error: %s' % (self.IE_NAME, response['error_msg'][0]), expected=True)
61
62         video_url = response['url'][0]
63
64         return {
65             'id': video_id,
66             'url': video_url,
67             'title': title,
68             'description': description
69         }