]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dailymotion.py
1a41c0db181c43c9c35d24988b9fd118c248f399
[youtubedl] / 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 (
11 compat_str,
12 compat_urllib_request,
13 )
14 from ..utils import (
15 ExtractorError,
16 int_or_none,
17 orderedSet,
18 str_to_int,
19 unescapeHTML,
20 )
21
22
23 class DailymotionBaseInfoExtractor(InfoExtractor):
24 @staticmethod
25 def _build_request(url):
26 """Build a request with the family filter disabled"""
27 request = compat_urllib_request.Request(url)
28 request.add_header('Cookie', 'family_filter=off; ff=off')
29 return request
30
31
32 class DailymotionIE(DailymotionBaseInfoExtractor):
33 """Information Extractor for Dailymotion"""
34
35 _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
36 IE_NAME = 'dailymotion'
37
38 _FORMATS = [
39 ('stream_h264_ld_url', 'ld'),
40 ('stream_h264_url', 'standard'),
41 ('stream_h264_hq_url', 'hq'),
42 ('stream_h264_hd_url', 'hd'),
43 ('stream_h264_hd1080_url', 'hd180'),
44 ]
45
46 _TESTS = [
47 {
48 'url': 'https://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames',
49 'md5': '2137c41a8e78554bb09225b8eb322406',
50 'info_dict': {
51 'id': 'x2iuewm',
52 'ext': 'mp4',
53 'uploader': 'IGN',
54 'title': 'Steam Machine Models, Pricing Listed on Steam Store - IGN News',
55 'upload_date': '20150306',
56 'duration': 74,
57 }
58 },
59 # Vevo video
60 {
61 'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
62 'info_dict': {
63 'title': 'Roar (Official)',
64 'id': 'USUV71301934',
65 'ext': 'mp4',
66 'uploader': 'Katy Perry',
67 'upload_date': '20130905',
68 },
69 'params': {
70 'skip_download': True,
71 },
72 'skip': 'VEVO is only available in some countries',
73 },
74 # age-restricted video
75 {
76 'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband',
77 'md5': '0d667a7b9cebecc3c89ee93099c4159d',
78 'info_dict': {
79 'id': 'xyh2zz',
80 'ext': 'mp4',
81 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]',
82 'uploader': 'HotWaves1012',
83 'age_limit': 18,
84 }
85 }
86 ]
87
88 def _real_extract(self, url):
89 video_id = self._match_id(url)
90 url = 'https://www.dailymotion.com/video/%s' % video_id
91
92 # Retrieve video webpage to extract further information
93 request = self._build_request(url)
94 webpage = self._download_webpage(request, video_id)
95
96 # Extract URL, uploader and title from webpage
97 self.report_extraction(video_id)
98
99 # It may just embed a vevo video:
100 m_vevo = re.search(
101 r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?video=(?P<id>[\w]*)',
102 webpage)
103 if m_vevo is not None:
104 vevo_id = m_vevo.group('id')
105 self.to_screen('Vevo video detected: %s' % vevo_id)
106 return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
107
108 age_limit = self._rta_search(webpage)
109
110 video_upload_date = None
111 mobj = re.search(r'<meta property="video:release_date" content="([0-9]{4})-([0-9]{2})-([0-9]{2}).+?"/>', webpage)
112 if mobj is not None:
113 video_upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3)
114
115 embed_url = 'https://www.dailymotion.com/embed/video/%s' % video_id
116 embed_request = self._build_request(embed_url)
117 embed_page = self._download_webpage(
118 embed_request, video_id, 'Downloading embed page')
119 info = self._search_regex(r'var info = ({.*?}),$', embed_page,
120 'video info', flags=re.MULTILINE)
121 info = json.loads(info)
122 if info.get('error') is not None:
123 msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
124 raise ExtractorError(msg, expected=True)
125
126 formats = []
127 for (key, format_id) in self._FORMATS:
128 video_url = info.get(key)
129 if video_url is not None:
130 m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
131 if m_size is not None:
132 width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
133 else:
134 width, height = None, None
135 formats.append({
136 'url': video_url,
137 'ext': 'mp4',
138 'format_id': format_id,
139 'width': width,
140 'height': height,
141 })
142 if not formats:
143 raise ExtractorError('Unable to extract video URL')
144
145 # subtitles
146 video_subtitles = self.extract_subtitles(video_id, webpage)
147
148 view_count = str_to_int(self._search_regex(
149 r'video_views_count[^>]+>\s+([\d\.,]+)',
150 webpage, 'view count', fatal=False))
151
152 title = self._og_search_title(webpage, default=None)
153 if title is None:
154 title = self._html_search_regex(
155 r'(?s)<span\s+id="video_title"[^>]*>(.*?)</span>', webpage,
156 'title')
157
158 return {
159 'id': video_id,
160 'formats': formats,
161 'uploader': info['owner.screenname'],
162 'upload_date': video_upload_date,
163 'title': title,
164 'subtitles': video_subtitles,
165 'thumbnail': info['thumbnail_url'],
166 'age_limit': age_limit,
167 'view_count': view_count,
168 'duration': info['duration']
169 }
170
171 def _get_subtitles(self, video_id, webpage):
172 try:
173 sub_list = self._download_webpage(
174 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
175 video_id, note=False)
176 except ExtractorError as err:
177 self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
178 return {}
179 info = json.loads(sub_list)
180 if (info['total'] > 0):
181 sub_lang_list = dict((l['language'], [{'url': l['url'], 'ext': 'srt'}]) for l in info['list'])
182 return sub_lang_list
183 self._downloader.report_warning('video doesn\'t have subtitles')
184 return {}
185
186
187 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
188 IE_NAME = 'dailymotion:playlist'
189 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
190 _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
191 _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
192 _TESTS = [{
193 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
194 'info_dict': {
195 'title': 'SPORT',
196 'id': 'xv4bw_nqtv_sport',
197 },
198 'playlist_mincount': 20,
199 }]
200
201 def _extract_entries(self, id):
202 video_ids = []
203 for pagenum in itertools.count(1):
204 request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
205 webpage = self._download_webpage(request,
206 id, 'Downloading page %s' % pagenum)
207
208 video_ids.extend(re.findall(r'data-xid="(.+?)"', webpage))
209
210 if re.search(self._MORE_PAGES_INDICATOR, webpage) is None:
211 break
212 return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
213 for video_id in orderedSet(video_ids)]
214
215 def _real_extract(self, url):
216 mobj = re.match(self._VALID_URL, url)
217 playlist_id = mobj.group('id')
218 webpage = self._download_webpage(url, playlist_id)
219
220 return {
221 '_type': 'playlist',
222 'id': playlist_id,
223 'title': self._og_search_title(webpage),
224 'entries': self._extract_entries(playlist_id),
225 }
226
227
228 class DailymotionUserIE(DailymotionPlaylistIE):
229 IE_NAME = 'dailymotion:user'
230 _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?:(?:old/)?user/)?(?P<user>[^/]+)$'
231 _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
232 _TESTS = [{
233 'url': 'https://www.dailymotion.com/user/nqtv',
234 'info_dict': {
235 'id': 'nqtv',
236 'title': 'Rémi Gaillard',
237 },
238 'playlist_mincount': 100,
239 }]
240
241 def _real_extract(self, url):
242 mobj = re.match(self._VALID_URL, url)
243 user = mobj.group('user')
244 webpage = self._download_webpage(
245 'https://www.dailymotion.com/user/%s' % user, user)
246 full_user = unescapeHTML(self._html_search_regex(
247 r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
248 webpage, 'user'))
249
250 return {
251 '_type': 'playlist',
252 'id': user,
253 'title': full_user,
254 'entries': self._extract_entries(user),
255 }
256
257
258 class DailymotionCloudIE(DailymotionBaseInfoExtractor):
259 _VALID_URL_PREFIX = r'http://api\.dmcloud\.net/(?:player/)?embed/'
260 _VALID_URL = r'%s[^/]+/(?P<id>[^/?]+)' % _VALID_URL_PREFIX
261 _VALID_EMBED_URL = r'%s[^/]+/[^\'"]+' % _VALID_URL_PREFIX
262
263 _TESTS = [{
264 # From http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html
265 # Tested at FranceTvInfo_2
266 'url': 'http://api.dmcloud.net/embed/4e7343f894a6f677b10006b4/556e03339473995ee145930c?auth=1464865870-0-jyhsm84b-ead4c701fb750cf9367bf4447167a3db&autoplay=1',
267 'only_matching': True,
268 }, {
269 # http://www.francetvinfo.fr/societe/larguez-les-amarres-le-cobaturage-se-developpe_980101.html
270 'url': 'http://api.dmcloud.net/player/embed/4e7343f894a6f677b10006b4/559545469473996d31429f06?auth=1467430263-0-90tglw2l-a3a4b64ed41efe48d7fccad85b8b8fda&autoplay=1',
271 'only_matching': True,
272 }]
273
274 @classmethod
275 def _extract_dmcloud_url(self, webpage):
276 mobj = re.search(r'<iframe[^>]+src=[\'"](%s)[\'"]' % self._VALID_EMBED_URL, webpage)
277 if mobj:
278 return mobj.group(1)
279
280 mobj = re.search(
281 r'<input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^>]+value=[\'"](%s)[\'"]' % self._VALID_EMBED_URL,
282 webpage)
283 if mobj:
284 return mobj.group(1)
285
286 def _real_extract(self, url):
287 video_id = self._match_id(url)
288
289 request = self._build_request(url)
290 webpage = self._download_webpage(request, video_id)
291
292 title = self._html_search_regex(r'<title>([^>]+)</title>', webpage, 'title')
293
294 video_info = self._parse_json(self._search_regex(
295 r'var\s+info\s*=\s*([^;]+);', webpage, 'video info'), video_id)
296
297 # TODO: parse ios_url, which is in fact a manifest
298 video_url = video_info['mp4_url']
299
300 return {
301 'id': video_id,
302 'url': video_url,
303 'title': title,
304 'thumbnail': video_info.get('thumbnail_url'),
305 }