]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dailymotion.py
Prepare for release.
[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 from .subtitles import SubtitlesInfoExtractor
10
11 from ..compat import (
12 compat_str,
13 compat_urllib_request,
14 )
15 from ..utils import (
16 ExtractorError,
17 int_or_none,
18 orderedSet,
19 str_to_int,
20 unescapeHTML,
21 )
22
23
24 class DailymotionBaseInfoExtractor(InfoExtractor):
25 @staticmethod
26 def _build_request(url):
27 """Build a request with the family filter disabled"""
28 request = compat_urllib_request.Request(url)
29 request.add_header('Cookie', 'family_filter=off')
30 request.add_header('Cookie', 'ff=off')
31 return request
32
33
34 class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
35 """Information Extractor for Dailymotion"""
36
37 _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
38 IE_NAME = 'dailymotion'
39
40 _FORMATS = [
41 ('stream_h264_ld_url', 'ld'),
42 ('stream_h264_url', 'standard'),
43 ('stream_h264_hq_url', 'hq'),
44 ('stream_h264_hd_url', 'hd'),
45 ('stream_h264_hd1080_url', 'hd180'),
46 ]
47
48 _TESTS = [
49 {
50 'url': 'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
51 'md5': '392c4b85a60a90dc4792da41ce3144eb',
52 'info_dict': {
53 'id': 'x33vw9',
54 'ext': 'mp4',
55 'uploader': 'Amphora Alex and Van .',
56 'title': 'Tutoriel de Youtubeur"DL DES VIDEO DE YOUTUBE"',
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 = 'http://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'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
112 if mobj is not None:
113 video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
114
115 embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
116 embed_page = self._download_webpage(embed_url, video_id,
117 'Downloading embed page')
118 info = self._search_regex(r'var info = ({.*?}),$', embed_page,
119 'video info', flags=re.MULTILINE)
120 info = json.loads(info)
121 if info.get('error') is not None:
122 msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
123 raise ExtractorError(msg, expected=True)
124
125 formats = []
126 for (key, format_id) in self._FORMATS:
127 video_url = info.get(key)
128 if video_url is not None:
129 m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
130 if m_size is not None:
131 width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
132 else:
133 width, height = None, None
134 formats.append({
135 'url': video_url,
136 'ext': 'mp4',
137 'format_id': format_id,
138 'width': width,
139 'height': height,
140 })
141 if not formats:
142 raise ExtractorError('Unable to extract video URL')
143
144 # subtitles
145 video_subtitles = self.extract_subtitles(video_id, webpage)
146 if self._downloader.params.get('listsubtitles', False):
147 self._list_available_subtitles(video_id, webpage)
148 return
149
150 view_count = str_to_int(self._search_regex(
151 r'video_views_count[^>]+>\s+([\d\.,]+)',
152 webpage, 'view count', fatal=False))
153
154 title = self._og_search_title(webpage, default=None)
155 if title is None:
156 title = self._html_search_regex(
157 r'(?s)<span\s+id="video_title"[^>]*>(.*?)</span>', webpage,
158 'title')
159
160 return {
161 'id': video_id,
162 'formats': formats,
163 'uploader': info['owner.screenname'],
164 'upload_date': video_upload_date,
165 'title': title,
166 'subtitles': video_subtitles,
167 'thumbnail': info['thumbnail_url'],
168 'age_limit': age_limit,
169 'view_count': view_count,
170 }
171
172 def _get_available_subtitles(self, video_id, webpage):
173 try:
174 sub_list = self._download_webpage(
175 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
176 video_id, note=False)
177 except ExtractorError as err:
178 self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
179 return {}
180 info = json.loads(sub_list)
181 if (info['total'] > 0):
182 sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
183 return sub_lang_list
184 self._downloader.report_warning('video doesn\'t have subtitles')
185 return {}
186
187
188 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
189 IE_NAME = 'dailymotion:playlist'
190 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
191 _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
192 _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
193 _TESTS = [{
194 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
195 'info_dict': {
196 'title': '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}/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(url, user)
245 full_user = unescapeHTML(self._html_search_regex(
246 r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
247 webpage, 'user'))
248
249 return {
250 '_type': 'playlist',
251 'id': user,
252 'title': full_user,
253 'entries': self._extract_entries(user),
254 }