Merge branch 'master' into subtitles_rework
[youtube-dl] / youtube_dl / extractor / dailymotion.py
1 import re
2 import json
3 import socket
4
5 from .common import InfoExtractor
6 from .subtitles import NoAutoSubtitlesIE
7
8 from ..utils import (
9     compat_http_client,
10     compat_urllib_error,
11     compat_urllib_request,
12     compat_str,
13     get_element_by_attribute,
14     get_element_by_id,
15
16     ExtractorError,
17 )
18
19
20 class DailyMotionSubtitlesIE(NoAutoSubtitlesIE):
21
22     def _get_available_subtitles(self, video_id):
23         request = compat_urllib_request.Request('https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id)
24         try:
25             sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
26         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
27             self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
28             return {}
29         info = json.loads(sub_list)
30         if (info['total'] > 0):
31             sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
32             return sub_lang_list
33         self._downloader.report_warning(u'video doesn\'t have subtitles')
34         return {}
35
36 class DailymotionIE(DailyMotionSubtitlesIE):
37     """Information Extractor for Dailymotion"""
38
39     _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
40     IE_NAME = u'dailymotion'
41     _TEST = {
42         u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
43         u'file': u'x33vw9.mp4',
44         u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
45         u'info_dict': {
46             u"uploader": u"Alex and Van .",
47             u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
48         }
49     }
50
51     def _real_extract(self, url):
52         # Extract id and simplified title from URL
53         mobj = re.match(self._VALID_URL, url)
54
55         video_id = mobj.group(1).split('_')[0].split('?')[0]
56
57         video_extension = 'mp4'
58
59         # Retrieve video webpage to extract further information
60         request = compat_urllib_request.Request(url)
61         request.add_header('Cookie', 'family_filter=off')
62         webpage = self._download_webpage(request, video_id)
63
64         # Extract URL, uploader and title from webpage
65         self.report_extraction(video_id)
66
67         video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
68                                              # Looking for official user
69                                              r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
70                                             webpage, 'video uploader')
71
72         video_upload_date = None
73         mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
74         if mobj is not None:
75             video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
76
77         embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
78         embed_page = self._download_webpage(embed_url, video_id,
79                                             u'Downloading embed page')
80         info = self._search_regex(r'var info = ({.*?}),', embed_page, 'video info')
81         info = json.loads(info)
82
83         # TODO: support choosing qualities
84
85         for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
86                     'stream_h264_hq_url','stream_h264_url',
87                     'stream_h264_ld_url']:
88             if info.get(key):  # key in info and info[key]:
89                 max_quality = key
90                 self.to_screen(u'%s: Using %s' % (video_id, key))
91                 break
92         else:
93             raise ExtractorError(u'Unable to extract video URL')
94         video_url = info[max_quality]
95
96         # subtitles
97         video_subtitles = None
98         video_webpage = None
99
100         if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
101             video_subtitles = self._extract_subtitles(video_id)
102         elif self._downloader.params.get('writeautomaticsub', False):
103             video_subtitles = self._request_automatic_caption(video_id, video_webpage)
104
105         if self._downloader.params.get('listsubtitles', False):
106             self._list_available_subtitles(video_id)
107             return
108
109         return [{
110             'id':       video_id,
111             'url':      video_url,
112             'uploader': video_uploader,
113             'upload_date':  video_upload_date,
114             'title':    self._og_search_title(webpage),
115             'ext':      video_extension,
116             'subtitles':    video_subtitles,
117             'thumbnail': info['thumbnail_url']
118         }]
119
120
121 class DailymotionPlaylistIE(InfoExtractor):
122     _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
123     _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
124
125     def _real_extract(self, url):
126         mobj = re.match(self._VALID_URL, url)
127         playlist_id =  mobj.group('id')
128         video_ids = []
129
130         for pagenum in itertools.count(1):
131             webpage = self._download_webpage('https://www.dailymotion.com/playlist/%s/%s' % (playlist_id, pagenum),
132                                              playlist_id, u'Downloading page %s' % pagenum)
133
134             playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
135             video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
136
137             if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
138                 break
139
140         entries = [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
141                    for video_id in video_ids]
142         return {'_type': 'playlist',
143                 'id': playlist_id,
144                 'title': get_element_by_id(u'playlist_name', webpage),
145                 'entries': entries,
146                 }