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