[dailymotion] Fix subtitles extraction
[youtube-dl] / youtube_dl / extractor / dailymotion.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6 import itertools
7
8 from .common import InfoExtractor
9
10 from ..compat import compat_str
11 from ..utils import (
12     ExtractorError,
13     determine_ext,
14     int_or_none,
15     parse_iso8601,
16     sanitized_Request,
17     str_to_int,
18     unescapeHTML,
19 )
20
21
22 class DailymotionBaseInfoExtractor(InfoExtractor):
23     @staticmethod
24     def _build_request(url):
25         """Build a request with the family filter disabled"""
26         request = sanitized_Request(url)
27         request.add_header('Cookie', 'family_filter=off; ff=off')
28         return request
29
30     def _download_webpage_handle_no_ff(self, url, *args, **kwargs):
31         request = self._build_request(url)
32         return self._download_webpage_handle(request, *args, **kwargs)
33
34     def _download_webpage_no_ff(self, url, *args, **kwargs):
35         request = self._build_request(url)
36         return self._download_webpage(request, *args, **kwargs)
37
38
39 class DailymotionIE(DailymotionBaseInfoExtractor):
40     _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
41     IE_NAME = 'dailymotion'
42
43     _FORMATS = [
44         ('stream_h264_ld_url', 'ld'),
45         ('stream_h264_url', 'standard'),
46         ('stream_h264_hq_url', 'hq'),
47         ('stream_h264_hd_url', 'hd'),
48         ('stream_h264_hd1080_url', 'hd180'),
49     ]
50
51     _TESTS = [
52         {
53             'url': 'https://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames',
54             'md5': '2137c41a8e78554bb09225b8eb322406',
55             'info_dict': {
56                 'id': 'x2iuewm',
57                 'ext': 'mp4',
58                 'title': 'Steam Machine Models, Pricing Listed on Steam Store - IGN News',
59                 'description': 'Several come bundled with the Steam Controller.',
60                 'thumbnail': 're:^https?:.*\.(?:jpg|png)$',
61                 'duration': 74,
62                 'timestamp': 1425657362,
63                 'upload_date': '20150306',
64                 'uploader': 'IGN',
65                 'uploader_id': 'xijv66',
66                 'age_limit': 0,
67                 'view_count': int,
68                 'comment_count': int,
69             }
70         },
71         # Vevo video
72         {
73             'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
74             'info_dict': {
75                 'title': 'Roar (Official)',
76                 'id': 'USUV71301934',
77                 'ext': 'mp4',
78                 'uploader': 'Katy Perry',
79                 'upload_date': '20130905',
80             },
81             'params': {
82                 'skip_download': True,
83             },
84             'skip': 'VEVO is only available in some countries',
85         },
86         # age-restricted video
87         {
88             'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband',
89             'md5': '0d667a7b9cebecc3c89ee93099c4159d',
90             'info_dict': {
91                 'id': 'xyh2zz',
92                 'ext': 'mp4',
93                 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]',
94                 'uploader': 'HotWaves1012',
95                 'age_limit': 18,
96             }
97         },
98         # geo-restricted, player v5
99         {
100             'url': 'http://www.dailymotion.com/video/xhza0o',
101             'only_matching': True,
102         }
103     ]
104
105     def _real_extract(self, url):
106         video_id = self._match_id(url)
107
108         webpage = self._download_webpage_no_ff(
109             'https://www.dailymotion.com/video/%s' % video_id, video_id)
110
111         age_limit = self._rta_search(webpage)
112
113         description = self._og_search_description(webpage) or self._html_search_meta(
114             'description', webpage, 'description')
115
116         view_count = str_to_int(self._search_regex(
117             [r'<meta[^>]+itemprop="interactionCount"[^>]+content="UserPlays:(\d+)"',
118              r'video_views_count[^>]+>\s+([\d\.,]+)'],
119             webpage, 'view count', fatal=False))
120         comment_count = int_or_none(self._search_regex(
121             r'<meta[^>]+itemprop="interactionCount"[^>]+content="UserComments:(\d+)"',
122             webpage, 'comment count', fatal=False))
123
124         player_v5 = self._search_regex(
125             [r'buildPlayer\(({.+?})\);\n',  # See https://github.com/rg3/youtube-dl/issues/7826
126              r'playerV5\s*=\s*dmp\.create\([^,]+?,\s*({.+?})\);',
127              r'buildPlayer\(({.+?})\);'],
128             webpage, 'player v5', default=None)
129         if player_v5:
130             player = self._parse_json(player_v5, video_id)
131             metadata = player['metadata']
132
133             self._check_error(metadata)
134
135             formats = []
136             for quality, media_list in metadata['qualities'].items():
137                 for media in media_list:
138                     media_url = media.get('url')
139                     if not media_url:
140                         continue
141                     type_ = media.get('type')
142                     if type_ == 'application/vnd.lumberjack.manifest':
143                         continue
144                     ext = determine_ext(media_url)
145                     if type_ == 'application/x-mpegURL' or ext == 'm3u8':
146                         m3u8_formats = self._extract_m3u8_formats(
147                             media_url, video_id, 'mp4', m3u8_id='hls', fatal=False)
148                         if m3u8_formats:
149                             formats.extend(m3u8_formats)
150                     elif type_ == 'application/f4m' or ext == 'f4m':
151                         f4m_formats = self._extract_f4m_formats(
152                             media_url, video_id, preference=-1, f4m_id='hds', fatal=False)
153                         if f4m_formats:
154                             formats.extend(f4m_formats)
155                     else:
156                         f = {
157                             'url': media_url,
158                             'format_id': quality,
159                         }
160                         m = re.search(r'H264-(?P<width>\d+)x(?P<height>\d+)', media_url)
161                         if m:
162                             f.update({
163                                 'width': int(m.group('width')),
164                                 'height': int(m.group('height')),
165                             })
166                         formats.append(f)
167             self._sort_formats(formats)
168
169             title = metadata['title']
170             duration = int_or_none(metadata.get('duration'))
171             timestamp = int_or_none(metadata.get('created_time'))
172             thumbnail = metadata.get('poster_url')
173             uploader = metadata.get('owner', {}).get('screenname')
174             uploader_id = metadata.get('owner', {}).get('id')
175
176             subtitles = {}
177             subtitles_data = metadata.get('subtitles', {}).get('data', {})
178             if subtitles_data and isinstance(subtitles_data, dict):
179                 for subtitle_lang, subtitle in subtitles_data.items():
180                     subtitles[subtitle_lang] = [{
181                         'ext': determine_ext(subtitle_url),
182                         'url': subtitle_url,
183                     } for subtitle_url in subtitle.get('urls', [])]
184
185             return {
186                 'id': video_id,
187                 'title': title,
188                 'description': description,
189                 'thumbnail': thumbnail,
190                 'duration': duration,
191                 'timestamp': timestamp,
192                 'uploader': uploader,
193                 'uploader_id': uploader_id,
194                 'age_limit': age_limit,
195                 'view_count': view_count,
196                 'comment_count': comment_count,
197                 'formats': formats,
198                 'subtitles': subtitles,
199             }
200
201         # vevo embed
202         vevo_id = self._search_regex(
203             r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?video=(?P<id>[\w]*)',
204             webpage, 'vevo embed', default=None)
205         if vevo_id:
206             return self.url_result('vevo:%s' % vevo_id, 'Vevo')
207
208         # fallback old player
209         embed_page = self._download_webpage_no_ff(
210             'https://www.dailymotion.com/embed/video/%s' % video_id,
211             video_id, 'Downloading embed page')
212
213         timestamp = parse_iso8601(self._html_search_meta(
214             'video:release_date', webpage, 'upload date'))
215
216         info = self._parse_json(
217             self._search_regex(
218                 r'var info = ({.*?}),$', embed_page,
219                 'video info', flags=re.MULTILINE),
220             video_id)
221
222         self._check_error(info)
223
224         formats = []
225         for (key, format_id) in self._FORMATS:
226             video_url = info.get(key)
227             if video_url is not None:
228                 m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
229                 if m_size is not None:
230                     width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
231                 else:
232                     width, height = None, None
233                 formats.append({
234                     'url': video_url,
235                     'ext': 'mp4',
236                     'format_id': format_id,
237                     'width': width,
238                     'height': height,
239                 })
240         self._sort_formats(formats)
241
242         # subtitles
243         video_subtitles = self.extract_subtitles(video_id, webpage)
244
245         title = self._og_search_title(webpage, default=None)
246         if title is None:
247             title = self._html_search_regex(
248                 r'(?s)<span\s+id="video_title"[^>]*>(.*?)</span>', webpage,
249                 'title')
250
251         return {
252             'id': video_id,
253             'formats': formats,
254             'uploader': info['owner.screenname'],
255             'timestamp': timestamp,
256             'title': title,
257             'description': description,
258             'subtitles': video_subtitles,
259             'thumbnail': info['thumbnail_url'],
260             'age_limit': age_limit,
261             'view_count': view_count,
262             'duration': info['duration']
263         }
264
265     def _check_error(self, info):
266         if info.get('error') is not None:
267             raise ExtractorError(
268                 '%s said: %s' % (self.IE_NAME, info['error']['title']), expected=True)
269
270     def _get_subtitles(self, video_id, webpage):
271         try:
272             sub_list = self._download_webpage(
273                 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
274                 video_id, note=False)
275         except ExtractorError as err:
276             self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
277             return {}
278         info = json.loads(sub_list)
279         if (info['total'] > 0):
280             sub_lang_list = dict((l['language'], [{'url': l['url'], 'ext': 'srt'}]) for l in info['list'])
281             return sub_lang_list
282         self._downloader.report_warning('video doesn\'t have subtitles')
283         return {}
284
285
286 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
287     IE_NAME = 'dailymotion:playlist'
288     _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
289     _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
290     _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
291     _TESTS = [{
292         'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
293         'info_dict': {
294             'title': 'SPORT',
295             'id': 'xv4bw_nqtv_sport',
296         },
297         'playlist_mincount': 20,
298     }]
299
300     def _extract_entries(self, id):
301         video_ids = set()
302         processed_urls = set()
303         for pagenum in itertools.count(1):
304             page_url = self._PAGE_TEMPLATE % (id, pagenum)
305             webpage, urlh = self._download_webpage_handle_no_ff(
306                 page_url, id, 'Downloading page %s' % pagenum)
307             if urlh.geturl() in processed_urls:
308                 self.report_warning('Stopped at duplicated page %s, which is the same as %s' % (
309                     page_url, urlh.geturl()), id)
310                 break
311
312             processed_urls.add(urlh.geturl())
313
314             for video_id in re.findall(r'data-xid="(.+?)"', webpage):
315                 if video_id not in video_ids:
316                     yield self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
317                     video_ids.add(video_id)
318
319             if re.search(self._MORE_PAGES_INDICATOR, webpage) is None:
320                 break
321
322     def _real_extract(self, url):
323         mobj = re.match(self._VALID_URL, url)
324         playlist_id = mobj.group('id')
325         webpage = self._download_webpage(url, playlist_id)
326
327         return {
328             '_type': 'playlist',
329             'id': playlist_id,
330             'title': self._og_search_title(webpage),
331             'entries': self._extract_entries(playlist_id),
332         }
333
334
335 class DailymotionUserIE(DailymotionPlaylistIE):
336     IE_NAME = 'dailymotion:user'
337     _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?!(?:embed|#|video|playlist)/)(?:(?:old/)?user/)?(?P<user>[^/]+)'
338     _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
339     _TESTS = [{
340         'url': 'https://www.dailymotion.com/user/nqtv',
341         'info_dict': {
342             'id': 'nqtv',
343             'title': 'RĂ©mi Gaillard',
344         },
345         'playlist_mincount': 100,
346     }, {
347         'url': 'http://www.dailymotion.com/user/UnderProject',
348         'info_dict': {
349             'id': 'UnderProject',
350             'title': 'UnderProject',
351         },
352         'playlist_mincount': 1800,
353         'expected_warnings': [
354             'Stopped at duplicated page',
355         ],
356         'skip': 'Takes too long time',
357     }]
358
359     def _real_extract(self, url):
360         mobj = re.match(self._VALID_URL, url)
361         user = mobj.group('user')
362         webpage = self._download_webpage(
363             'https://www.dailymotion.com/user/%s' % user, user)
364         full_user = unescapeHTML(self._html_search_regex(
365             r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
366             webpage, 'user'))
367
368         return {
369             '_type': 'playlist',
370             'id': user,
371             'title': full_user,
372             'entries': self._extract_entries(user),
373         }
374
375
376 class DailymotionCloudIE(DailymotionBaseInfoExtractor):
377     _VALID_URL_PREFIX = r'http://api\.dmcloud\.net/(?:player/)?embed/'
378     _VALID_URL = r'%s[^/]+/(?P<id>[^/?]+)' % _VALID_URL_PREFIX
379     _VALID_EMBED_URL = r'%s[^/]+/[^\'"]+' % _VALID_URL_PREFIX
380
381     _TESTS = [{
382         # From http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html
383         # Tested at FranceTvInfo_2
384         'url': 'http://api.dmcloud.net/embed/4e7343f894a6f677b10006b4/556e03339473995ee145930c?auth=1464865870-0-jyhsm84b-ead4c701fb750cf9367bf4447167a3db&autoplay=1',
385         'only_matching': True,
386     }, {
387         # http://www.francetvinfo.fr/societe/larguez-les-amarres-le-cobaturage-se-developpe_980101.html
388         'url': 'http://api.dmcloud.net/player/embed/4e7343f894a6f677b10006b4/559545469473996d31429f06?auth=1467430263-0-90tglw2l-a3a4b64ed41efe48d7fccad85b8b8fda&autoplay=1',
389         'only_matching': True,
390     }]
391
392     @classmethod
393     def _extract_dmcloud_url(self, webpage):
394         mobj = re.search(r'<iframe[^>]+src=[\'"](%s)[\'"]' % self._VALID_EMBED_URL, webpage)
395         if mobj:
396             return mobj.group(1)
397
398         mobj = re.search(
399             r'<input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^>]+value=[\'"](%s)[\'"]' % self._VALID_EMBED_URL,
400             webpage)
401         if mobj:
402             return mobj.group(1)
403
404     def _real_extract(self, url):
405         video_id = self._match_id(url)
406
407         webpage = self._download_webpage_no_ff(url, video_id)
408
409         title = self._html_search_regex(r'<title>([^>]+)</title>', webpage, 'title')
410
411         video_info = self._parse_json(self._search_regex(
412             r'var\s+info\s*=\s*([^;]+);', webpage, 'video info'), video_id)
413
414         # TODO: parse ios_url, which is in fact a manifest
415         video_url = video_info['mp4_url']
416
417         return {
418             'id': video_id,
419             'url': video_url,
420             'title': title,
421             'thumbnail': video_info.get('thumbnail_url'),
422         }