[novamov] Fix filekey extraction (closes #7764)
[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 compat_urlparse
7 from ..utils import (
8     ExtractorError,
9     NO_DEFAULT,
10     encode_dict,
11     sanitized_Request,
12     urlencode_postdata,
13 )
14
15
16 class NovaMovIE(InfoExtractor):
17     IE_NAME = 'novamov'
18     IE_DESC = 'NovaMov'
19
20     _VALID_URL_TEMPLATE = r'http://(?:(?:www\.)?%(host)s/(?:file|video)/|(?:(?:embed|www)\.)%(host)s/embed\.php\?(?:.*?&)?v=)(?P<id>[a-z\d]{13})'
21     _VALID_URL = _VALID_URL_TEMPLATE % {'host': 'novamov\.com'}
22
23     _HOST = 'www.novamov.com'
24
25     _FILE_DELETED_REGEX = r'This file no longer exists on our servers!</h2>'
26     _FILEKEY_REGEX = r'flashvars\.filekey=(?P<filekey>"?[^"]+"?);'
27     _TITLE_REGEX = r'(?s)<div class="v_tab blockborder rounded5" id="v_tab1">\s*<h3>([^<]+)</h3>'
28     _DESCRIPTION_REGEX = r'(?s)<div class="v_tab blockborder rounded5" id="v_tab1">\s*<h3>[^<]+</h3><p>([^<]+)</p>'
29     _URL_TEMPLATE = 'http://%s/video/%s'
30
31     _TEST = {
32         'url': 'http://www.novamov.com/video/4rurhn9x446jj',
33         'md5': '7205f346a52bbeba427603ba10d4b935',
34         'info_dict': {
35             'id': '4rurhn9x446jj',
36             'ext': 'flv',
37             'title': 'search engine optimization',
38             'description': 'search engine optimization is used to rank the web page in the google search engine'
39         },
40         'skip': '"Invalid token" errors abound (in web interface as well as youtube-dl, there is nothing we can do about it.)'
41     }
42
43     def _real_extract(self, url):
44         video_id = self._match_id(url)
45
46         url = self._URL_TEMPLATE % (self._HOST, video_id)
47
48         webpage = self._download_webpage(
49             url, video_id, 'Downloading video page')
50
51         if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
52             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
53
54         def extract_filekey(default=NO_DEFAULT):
55             filekey = self._search_regex(
56                 self._FILEKEY_REGEX, webpage, 'filekey', default=default)
57             if filekey is not default and (filekey[0] != '"' or filekey[-1] != '"'):
58                 return self._search_regex(
59                     r'var\s*%s\s*=\s*"([^"]+)"', webpage, 'filekey', default=default)
60             else:
61                 return filekey
62
63         filekey = extract_filekey(default=None)
64
65         if not filekey:
66             fields = self._hidden_inputs(webpage)
67             post_url = self._search_regex(
68                 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', webpage,
69                 'post url', default=url, group='url')
70             if not post_url.startswith('http'):
71                 post_url = compat_urlparse.urljoin(url, post_url)
72             request = sanitized_Request(
73                 post_url, urlencode_postdata(encode_dict(fields)))
74             request.add_header('Content-Type', 'application/x-www-form-urlencoded')
75             request.add_header('Referer', post_url)
76             webpage = self._download_webpage(
77                 request, video_id, 'Downloading continue to the video page')
78
79         filekey = extract_filekey()
80
81         title = self._html_search_regex(self._TITLE_REGEX, webpage, 'title', fatal=False)
82         description = self._html_search_regex(self._DESCRIPTION_REGEX, webpage, 'description', default='', fatal=False)
83
84         api_response = self._download_webpage(
85             'http://%s/api/player.api.php?key=%s&file=%s' % (self._HOST, filekey, video_id), video_id,
86             'Downloading video api response')
87
88         response = compat_urlparse.parse_qs(api_response)
89
90         if 'error_msg' in response:
91             raise ExtractorError('%s returned error: %s' % (self.IE_NAME, response['error_msg'][0]), expected=True)
92
93         video_url = response['url'][0]
94
95         return {
96             'id': video_id,
97             'url': video_url,
98             'title': title,
99             'description': description
100         }
101
102
103 class WholeCloudIE(NovaMovIE):
104     IE_NAME = 'wholecloud'
105     IE_DESC = 'WholeCloud'
106
107     _VALID_URL = NovaMovIE._VALID_URL_TEMPLATE % {'host': '(?:wholecloud\.net|movshare\.(?:net|sx|ag))'}
108
109     _HOST = 'www.wholecloud.net'
110
111     _FILE_DELETED_REGEX = r'>This file no longer exists on our servers.<'
112     _TITLE_REGEX = r'<strong>Title:</strong> ([^<]+)</p>'
113     _DESCRIPTION_REGEX = r'<strong>Description:</strong> ([^<]+)</p>'
114
115     _TEST = {
116         'url': 'http://www.wholecloud.net/video/559e28be54d96',
117         'md5': 'abd31a2132947262c50429e1d16c1bfd',
118         'info_dict': {
119             'id': '559e28be54d96',
120             'ext': 'flv',
121             'title': 'dissapeared image',
122             'description': 'optical illusion  dissapeared image  magic illusion',
123         }
124     }
125
126
127 class NowVideoIE(NovaMovIE):
128     IE_NAME = 'nowvideo'
129     IE_DESC = 'NowVideo'
130
131     _VALID_URL = NovaMovIE._VALID_URL_TEMPLATE % {'host': 'nowvideo\.(?:to|ch|ec|sx|eu|at|ag|co|li)'}
132
133     _HOST = 'www.nowvideo.to'
134
135     _FILE_DELETED_REGEX = r'>This file no longer exists on our servers.<'
136     _TITLE_REGEX = r'<h4>([^<]+)</h4>'
137     _DESCRIPTION_REGEX = r'</h4>\s*<p>([^<]+)</p>'
138
139     _TEST = {
140         'url': 'http://www.nowvideo.to/video/0mw0yow7b6dxa',
141         'md5': 'f8fbbc8add72bd95b7850c6a02fc8817',
142         'info_dict': {
143             'id': '0mw0yow7b6dxa',
144             'ext': 'flv',
145             'title': 'youtubedl test video _BaW_jenozKc.mp4',
146             'description': 'Description',
147         }
148     }
149
150
151 class VideoWeedIE(NovaMovIE):
152     IE_NAME = 'videoweed'
153     IE_DESC = 'VideoWeed'
154
155     _VALID_URL = NovaMovIE._VALID_URL_TEMPLATE % {'host': 'videoweed\.(?:es|com)'}
156
157     _HOST = 'www.videoweed.es'
158
159     _FILE_DELETED_REGEX = r'>This file no longer exists on our servers.<'
160     _TITLE_REGEX = r'<h1 class="text_shadow">([^<]+)</h1>'
161     _URL_TEMPLATE = 'http://%s/file/%s'
162
163     _TEST = {
164         'url': 'http://www.videoweed.es/file/b42178afbea14',
165         'md5': 'abd31a2132947262c50429e1d16c1bfd',
166         'info_dict': {
167             'id': 'b42178afbea14',
168             'ext': 'flv',
169             'title': 'optical illusion  dissapeared image magic illusion',
170             'description': ''
171         },
172     }
173
174
175 class CloudTimeIE(NovaMovIE):
176     IE_NAME = 'cloudtime'
177     IE_DESC = 'CloudTime'
178
179     _VALID_URL = NovaMovIE._VALID_URL_TEMPLATE % {'host': 'cloudtime\.to'}
180
181     _HOST = 'www.cloudtime.to'
182
183     _FILE_DELETED_REGEX = r'>This file no longer exists on our servers.<'
184     _TITLE_REGEX = r'<div[^>]+class=["\']video_det["\'][^>]*>\s*<strong>([^<]+)</strong>'
185
186     _TEST = None