]>
Commit | Line | Data |
---|---|---|
ec240a43 | 1 | import functools |
f15f7a67 | 2 | import json |
f15f7a67 | 3 | import re |
219b8130 PH |
4 | |
5 | from .common import InfoExtractor | |
5ef62fc4 | 6 | from ..compat import compat_HTTPError |
1cc79574 | 7 | from ..utils import ( |
60ba603a H |
8 | ExtractorError, |
9 | OnDemandPagedList, | |
5ef62fc4 RA |
10 | age_restricted, |
11 | clean_html, | |
1cc79574 | 12 | int_or_none, |
60ba603a | 13 | traverse_obj, |
0082f44a | 14 | try_get, |
4b10aadf | 15 | unescapeHTML, |
60ba603a | 16 | unsmuggle_url, |
ec240a43 | 17 | urlencode_postdata, |
219b8130 PH |
18 | ) |
19 | ||
5f6a1245 | 20 | |
70922df8 | 21 | class DailymotionBaseInfoExtractor(InfoExtractor): |
5ef62fc4 RA |
22 | _FAMILY_FILTER = None |
23 | _HEADERS = { | |
24 | 'Content-Type': 'application/json', | |
25 | 'Origin': 'https://www.dailymotion.com', | |
26 | } | |
27 | _NETRC_MACHINE = 'dailymotion' | |
953e32b2 | 28 | |
5ef62fc4 RA |
29 | def _get_dailymotion_cookies(self): |
30 | return self._get_cookies('https://www.dailymotion.com/') | |
12434026 | 31 | |
5ef62fc4 RA |
32 | @staticmethod |
33 | def _get_cookie_value(cookies, name): | |
676723e0 | 34 | cookie = cookies.get(name) |
5ef62fc4 RA |
35 | if cookie: |
36 | return cookie.value | |
37 | ||
38 | def _set_dailymotion_cookie(self, name, value): | |
39 | self._set_cookie('www.dailymotion.com', name, value) | |
40 | ||
41 | def _real_initialize(self): | |
42 | cookies = self._get_dailymotion_cookies() | |
43 | ff = self._get_cookie_value(cookies, 'ff') | |
a06916d9 | 44 | self._FAMILY_FILTER = ff == 'on' if ff else age_restricted(18, self.get_param('age_limit')) |
5ef62fc4 RA |
45 | self._set_dailymotion_cookie('ff', 'on' if self._FAMILY_FILTER else 'off') |
46 | ||
47 | def _call_api(self, object_type, xid, object_fields, note, filter_extra=None): | |
48 | if not self._HEADERS.get('Authorization'): | |
49 | cookies = self._get_dailymotion_cookies() | |
50 | token = self._get_cookie_value(cookies, 'access_token') or self._get_cookie_value(cookies, 'client_token') | |
51 | if not token: | |
52 | data = { | |
53 | 'client_id': 'f1a362d288c1b98099c7', | |
54 | 'client_secret': 'eea605b96e01c796ff369935357eca920c5da4c5', | |
55 | } | |
56 | username, password = self._get_login_info() | |
57 | if username: | |
58 | data.update({ | |
59 | 'grant_type': 'password', | |
60 | 'password': password, | |
61 | 'username': username, | |
62 | }) | |
63 | else: | |
64 | data['grant_type'] = 'client_credentials' | |
65 | try: | |
66 | token = self._download_json( | |
67 | 'https://graphql.api.dailymotion.com/oauth/token', | |
68 | None, 'Downloading Access Token', | |
69 | data=urlencode_postdata(data))['access_token'] | |
70 | except ExtractorError as e: | |
71 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400: | |
72 | raise ExtractorError(self._parse_json( | |
73 | e.cause.read().decode(), xid)['error_description'], expected=True) | |
74 | raise | |
75 | self._set_dailymotion_cookie('access_token' if username else 'client_token', token) | |
76 | self._HEADERS['Authorization'] = 'Bearer ' + token | |
77 | ||
78 | resp = self._download_json( | |
79 | 'https://graphql.api.dailymotion.com/', xid, note, data=json.dumps({ | |
80 | 'query': '''{ | |
81 | %s(xid: "%s"%s) { | |
82 | %s | |
83 | } | |
84 | }''' % (object_type, xid, ', ' + filter_extra if filter_extra else '', object_fields), | |
85 | }).encode(), headers=self._HEADERS) | |
86 | obj = resp['data'][object_type] | |
87 | if not obj: | |
88 | raise ExtractorError(resp['errors'][0]['message'], expected=True) | |
89 | return obj | |
5f6a1245 | 90 | |
219b8130 | 91 | |
d3f007af | 92 | class DailymotionIE(DailymotionBaseInfoExtractor): |
9d058b32 S |
93 | _VALID_URL = r'''(?ix) |
94 | https?:// | |
95 | (?: | |
48e15bb6 | 96 | (?:(?:www|touch|geo)\.)?dailymotion\.[a-z]{2,3}/(?:(?:(?:(?:embed|swf|\#)/)|player\.html\?)?video|swf)| |
9d058b32 S |
97 | (?:www\.)?lequipe\.fr/video |
98 | ) | |
48e15bb6 | 99 | [/=](?P<id>[^/?_&]+)(?:.+?\bplaylist=(?P<playlist_id>x[0-9a-z]+))? |
9d058b32 | 100 | ''' |
ce6815aa | 101 | IE_NAME = 'dailymotion' |
bfd973ec | 102 | _EMBED_REGEX = [r'<(?:(?:embed|iframe)[^>]+?src=|input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^>]+value=)(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/(?:embed|swf)/video/.+?)\1'] |
c360e641 S |
103 | _TESTS = [{ |
104 | 'url': 'http://www.dailymotion.com/video/x5kesuj_office-christmas-party-review-jason-bateman-olivia-munn-t-j-miller_news', | |
105 | 'md5': '074b95bdee76b9e3654137aee9c79dfe', | |
106 | 'info_dict': { | |
107 | 'id': 'x5kesuj', | |
108 | 'ext': 'mp4', | |
109 | 'title': 'Office Christmas Party Review – Jason Bateman, Olivia Munn, T.J. Miller', | |
110 | 'description': 'Office Christmas Party Review - Jason Bateman, Olivia Munn, T.J. Miller', | |
c360e641 S |
111 | 'duration': 187, |
112 | 'timestamp': 1493651285, | |
113 | 'upload_date': '20170501', | |
114 | 'uploader': 'Deadline', | |
115 | 'uploader_id': 'x1xm8ri', | |
116 | 'age_limit': 0, | |
d86d169d | 117 | }, |
48e15bb6 HTL |
118 | }, { |
119 | 'url': 'https://geo.dailymotion.com/player.html?video=x89eyek&mute=true', | |
120 | 'md5': 'e2f9717c6604773f963f069ca53a07f8', | |
121 | 'info_dict': { | |
122 | 'id': 'x89eyek', | |
123 | 'ext': 'mp4', | |
124 | 'title': "En quête d'esprit du 27/03/2022", | |
125 | 'description': 'md5:66542b9f4df2eb23f314fc097488e553', | |
126 | 'duration': 2756, | |
127 | 'timestamp': 1648383669, | |
128 | 'upload_date': '20220327', | |
129 | 'uploader': 'CNEWS', | |
130 | 'uploader_id': 'x24vth', | |
131 | 'age_limit': 0, | |
132 | 'view_count': int, | |
133 | 'like_count': int, | |
134 | 'tags': ['en_quete_d_esprit'], | |
135 | 'thumbnail': 'https://s2.dmcdn.net/v/Tncwi1YGKdvFbDuDY/x1080', | |
136 | } | |
c360e641 S |
137 | }, { |
138 | 'url': 'https://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames', | |
139 | 'md5': '2137c41a8e78554bb09225b8eb322406', | |
140 | 'info_dict': { | |
141 | 'id': 'x2iuewm', | |
142 | 'ext': 'mp4', | |
143 | 'title': 'Steam Machine Models, Pricing Listed on Steam Store - IGN News', | |
144 | 'description': 'Several come bundled with the Steam Controller.', | |
145 | 'thumbnail': r're:^https?:.*\.(?:jpg|png)$', | |
146 | 'duration': 74, | |
147 | 'timestamp': 1425657362, | |
148 | 'upload_date': '20150306', | |
149 | 'uploader': 'IGN', | |
150 | 'uploader_id': 'xijv66', | |
151 | 'age_limit': 0, | |
152 | 'view_count': int, | |
c5428382 | 153 | }, |
c360e641 S |
154 | 'skip': 'video gone', |
155 | }, { | |
c5428382 | 156 | # Vevo video |
c360e641 S |
157 | 'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi', |
158 | 'info_dict': { | |
159 | 'title': 'Roar (Official)', | |
160 | 'id': 'USUV71301934', | |
161 | 'ext': 'mp4', | |
162 | 'uploader': 'Katy Perry', | |
163 | 'upload_date': '20130905', | |
c5428382 | 164 | }, |
c360e641 S |
165 | 'params': { |
166 | 'skip_download': True, | |
167 | }, | |
168 | 'skip': 'VEVO is only available in some countries', | |
169 | }, { | |
9f1109a5 | 170 | # age-restricted video |
c360e641 S |
171 | 'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband', |
172 | 'md5': '0d667a7b9cebecc3c89ee93099c4159d', | |
173 | 'info_dict': { | |
174 | 'id': 'xyh2zz', | |
175 | 'ext': 'mp4', | |
176 | 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]', | |
177 | 'uploader': 'HotWaves1012', | |
178 | 'age_limit': 18, | |
583882fd | 179 | }, |
c360e641 S |
180 | 'skip': 'video gone', |
181 | }, { | |
583882fd | 182 | # geo-restricted, player v5 |
c360e641 S |
183 | 'url': 'http://www.dailymotion.com/video/xhza0o', |
184 | 'only_matching': True, | |
185 | }, { | |
a8abf124 | 186 | # with subtitles |
c360e641 S |
187 | 'url': 'http://www.dailymotion.com/video/x20su5f_the-power-of-nightmares-1-the-rise-of-the-politics-of-fear-bbc-2004_news', |
188 | 'only_matching': True, | |
189 | }, { | |
190 | 'url': 'http://www.dailymotion.com/swf/video/x3n92nf', | |
191 | 'only_matching': True, | |
192 | }, { | |
193 | 'url': 'http://www.dailymotion.com/swf/x3ss1m_funny-magic-trick-barry-and-stuart_fun', | |
194 | 'only_matching': True, | |
9d058b32 S |
195 | }, { |
196 | 'url': 'https://www.lequipe.fr/video/x791mem', | |
197 | 'only_matching': True, | |
198 | }, { | |
199 | 'url': 'https://www.lequipe.fr/video/k7MtHciueyTcrFtFKA2', | |
200 | 'only_matching': True, | |
5ef62fc4 RA |
201 | }, { |
202 | 'url': 'https://www.dailymotion.com/video/x3z49k?playlist=xv4bw', | |
203 | 'only_matching': True, | |
c360e641 | 204 | }] |
5ef62fc4 RA |
205 | _GEO_BYPASS = False |
206 | _COMMON_MEDIA_FIELDS = '''description | |
207 | geoblockedCountries { | |
208 | allowed | |
209 | } | |
210 | xid''' | |
219b8130 | 211 | |
bfd973ec | 212 | @classmethod |
213 | def _extract_embed_urls(cls, url, webpage): | |
5e3da0d4 | 214 | # https://developer.dailymotion.com/player#player-parameters |
bfd973ec | 215 | yield from super()._extract_embed_urls(url, webpage) |
5e3da0d4 RA |
216 | for mobj in re.finditer( |
217 | r'(?s)DM\.player\([^,]+,\s*{.*?video[\'"]?\s*:\s*["\']?(?P<id>[0-9a-zA-Z]+).+?}\s*\);', webpage): | |
bfd973ec | 218 | yield from 'https://www.dailymotion.com/embed/video/' + mobj.group('id') |
ad213a1d | 219 | |
219b8130 | 220 | def _real_extract(self, url): |
60ba603a | 221 | url, smuggled_data = unsmuggle_url(url) |
5ad28e7f | 222 | video_id, playlist_id = self._match_valid_url(url).groups() |
5ef62fc4 RA |
223 | |
224 | if playlist_id: | |
f40ee5e9 | 225 | if self._yes_playlist(playlist_id, video_id): |
5ef62fc4 RA |
226 | return self.url_result( |
227 | 'http://www.dailymotion.com/playlist/' + playlist_id, | |
228 | 'DailymotionPlaylist', playlist_id) | |
5ef62fc4 | 229 | |
a06916d9 | 230 | password = self.get_param('videopassword') |
5ef62fc4 RA |
231 | media = self._call_api( |
232 | 'media', video_id, '''... on Video { | |
233 | %s | |
234 | stats { | |
235 | likes { | |
236 | total | |
237 | } | |
238 | views { | |
239 | total | |
240 | } | |
241 | } | |
242 | } | |
243 | ... on Live { | |
244 | %s | |
245 | audienceCount | |
246 | isOnAir | |
247 | }''' % (self._COMMON_MEDIA_FIELDS, self._COMMON_MEDIA_FIELDS), 'Downloading media JSON metadata', | |
a06916d9 | 248 | 'password: "%s"' % self.get_param('videopassword') if password else None) |
5ef62fc4 RA |
249 | xid = media['xid'] |
250 | ||
251 | metadata = self._download_json( | |
252 | 'https://www.dailymotion.com/player/metadata/video/' + xid, | |
253 | xid, 'Downloading metadata JSON', | |
60ba603a | 254 | query=traverse_obj(smuggled_data, 'query') or {'app': 'com.dailymotion.neon'}) |
5ef62fc4 RA |
255 | |
256 | error = metadata.get('error') | |
257 | if error: | |
258 | title = error.get('title') or error['raw_message'] | |
259 | # See https://developer.dailymotion.com/api#access-error | |
260 | if error.get('code') == 'DM007': | |
261 | allowed_countries = try_get(media, lambda x: x['geoblockedCountries']['allowed'], list) | |
262 | self.raise_geo_restricted(msg=title, countries=allowed_countries) | |
263 | raise ExtractorError( | |
264 | '%s said: %s' % (self.IE_NAME, title), expected=True) | |
b27c856f | 265 | |
5ef62fc4 RA |
266 | title = metadata['title'] |
267 | is_live = media.get('isOnAir') | |
cdec0190 | 268 | formats = [] |
5ef62fc4 RA |
269 | for quality, media_list in metadata['qualities'].items(): |
270 | for m in media_list: | |
271 | media_url = m.get('url') | |
272 | media_type = m.get('type') | |
273 | if not media_url or media_type == 'application/vnd.lumberjack.manifest': | |
274 | continue | |
275 | if media_type == 'application/x-mpegURL': | |
276 | formats.extend(self._extract_m3u8_formats( | |
a5c0c202 | 277 | media_url, video_id, 'mp4', live=is_live, m3u8_id='hls', fatal=False)) |
cdec0190 | 278 | else: |
5ef62fc4 RA |
279 | f = { |
280 | 'url': media_url, | |
281 | 'format_id': 'http-' + quality, | |
282 | } | |
283 | m = re.search(r'/H264-(\d+)x(\d+)(?:-(60)/)?', media_url) | |
284 | if m: | |
285 | width, height, fps = map(int_or_none, m.groups()) | |
286 | f.update({ | |
287 | 'fps': fps, | |
288 | 'height': height, | |
289 | 'width': width, | |
290 | }) | |
291 | formats.append(f) | |
292 | for f in formats: | |
293 | f['url'] = f['url'].split('#')[0] | |
294 | if not f.get('fps') and f['format_id'].endswith('@60'): | |
295 | f['fps'] = 60 | |
d3f007af | 296 | self._sort_formats(formats) |
b27c856f | 297 | |
5ef62fc4 RA |
298 | subtitles = {} |
299 | subtitles_data = try_get(metadata, lambda x: x['subtitles']['data'], dict) or {} | |
300 | for subtitle_lang, subtitle in subtitles_data.items(): | |
301 | subtitles[subtitle_lang] = [{ | |
302 | 'url': subtitle_url, | |
303 | } for subtitle_url in subtitle.get('urls', [])] | |
304 | ||
305 | thumbnails = [] | |
306 | for height, poster_url in metadata.get('posters', {}).items(): | |
307 | thumbnails.append({ | |
308 | 'height': int_or_none(height), | |
309 | 'id': height, | |
310 | 'url': poster_url, | |
311 | }) | |
312 | ||
313 | owner = metadata.get('owner') or {} | |
314 | stats = media.get('stats') or {} | |
315 | get_count = lambda x: int_or_none(try_get(stats, lambda y: y[x + 's']['total'])) | |
f53c966a | 316 | |
9f1109a5 | 317 | return { |
b10609d9 | 318 | 'id': video_id, |
39ca3b5c | 319 | 'title': title, |
5ef62fc4 RA |
320 | 'description': clean_html(media.get('description')), |
321 | 'thumbnails': thumbnails, | |
322 | 'duration': int_or_none(metadata.get('duration')) or None, | |
323 | 'timestamp': int_or_none(metadata.get('created_time')), | |
324 | 'uploader': owner.get('screenname'), | |
325 | 'uploader_id': owner.get('id') or metadata.get('screenname'), | |
326 | 'age_limit': 18 if metadata.get('explicit') else 0, | |
327 | 'tags': metadata.get('tags'), | |
328 | 'view_count': get_count('view') or int_or_none(media.get('audienceCount')), | |
329 | 'like_count': get_count('like'), | |
cdec0190 | 330 | 'formats': formats, |
5ef62fc4 RA |
331 | 'subtitles': subtitles, |
332 | 'is_live': is_live, | |
9f1109a5 | 333 | } |
a3c736de JMF |
334 | |
335 | ||
5ef62fc4 | 336 | class DailymotionPlaylistBaseIE(DailymotionBaseInfoExtractor): |
ec240a43 RA |
337 | _PAGE_SIZE = 100 |
338 | ||
5ef62fc4 | 339 | def _fetch_page(self, playlist_id, page): |
ec240a43 | 340 | page += 1 |
5ef62fc4 RA |
341 | videos = self._call_api( |
342 | self._OBJECT_TYPE, playlist_id, | |
343 | '''videos(allowExplicit: %s, first: %d, page: %d) { | |
ec240a43 RA |
344 | edges { |
345 | node { | |
346 | xid | |
347 | url | |
348 | } | |
349 | } | |
5ef62fc4 RA |
350 | }''' % ('false' if self._FAMILY_FILTER else 'true', self._PAGE_SIZE, page), |
351 | 'Downloading page %d' % page)['videos'] | |
ec240a43 RA |
352 | for edge in videos['edges']: |
353 | node = edge['node'] | |
354 | yield self.url_result( | |
355 | node['url'], DailymotionIE.ie_key(), node['xid']) | |
39baacc4 JMF |
356 | |
357 | def _real_extract(self, url): | |
ec240a43 | 358 | playlist_id = self._match_id(url) |
ec240a43 | 359 | entries = OnDemandPagedList(functools.partial( |
5ef62fc4 | 360 | self._fetch_page, playlist_id), self._PAGE_SIZE) |
ec240a43 | 361 | return self.playlist_result( |
5ef62fc4 RA |
362 | entries, playlist_id) |
363 | ||
364 | ||
365 | class DailymotionPlaylistIE(DailymotionPlaylistBaseIE): | |
366 | IE_NAME = 'dailymotion:playlist' | |
367 | _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>x[0-9a-z]+)' | |
368 | _TESTS = [{ | |
369 | 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q', | |
370 | 'info_dict': { | |
371 | 'id': 'xv4bw', | |
372 | }, | |
373 | 'playlist_mincount': 20, | |
374 | }] | |
375 | _OBJECT_TYPE = 'collection' | |
ec240a43 | 376 | |
bfd973ec | 377 | @classmethod |
378 | def _extract_embed_urls(cls, url, webpage): | |
379 | # Look for embedded Dailymotion playlist player (#3822) | |
380 | for mobj in re.finditer( | |
381 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.[a-z]{2,3}/widget/jukebox\?.+?)\1', | |
382 | webpage): | |
383 | for p in re.findall(r'list\[\]=/playlist/([^/]+)/', unescapeHTML(mobj.group('url'))): | |
384 | yield '//dailymotion.com/playlist/%s' % p | |
385 | ||
ec240a43 | 386 | |
5ef62fc4 | 387 | class DailymotionUserIE(DailymotionPlaylistBaseIE): |
22a6f150 | 388 | IE_NAME = 'dailymotion:user' |
5ef62fc4 | 389 | _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?!(?:embed|swf|#|video|playlist)/)(?:(?:old/)?user/)?(?P<id>[^/]+)' |
22a6f150 PH |
390 | _TESTS = [{ |
391 | 'url': 'https://www.dailymotion.com/user/nqtv', | |
392 | 'info_dict': { | |
393 | 'id': 'nqtv', | |
22a6f150 | 394 | }, |
5ef62fc4 | 395 | 'playlist_mincount': 152, |
12434026 YCH |
396 | }, { |
397 | 'url': 'http://www.dailymotion.com/user/UnderProject', | |
398 | 'info_dict': { | |
399 | 'id': 'UnderProject', | |
12434026 | 400 | }, |
5ef62fc4 | 401 | 'playlist_mincount': 1000, |
12434026 | 402 | 'skip': 'Takes too long time', |
5ef62fc4 RA |
403 | }, { |
404 | 'url': 'https://www.dailymotion.com/user/nqtv', | |
405 | 'info_dict': { | |
406 | 'id': 'nqtv', | |
407 | }, | |
408 | 'playlist_mincount': 148, | |
409 | 'params': { | |
410 | 'age_limit': 0, | |
411 | }, | |
22a6f150 | 412 | }] |
5ef62fc4 | 413 | _OBJECT_TYPE = 'channel' |