]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dailymotion.py
Prepare for upload.
[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 ..utils import (
12 compat_urllib_request,
13 compat_str,
14 orderedSet,
15 str_to_int,
16 int_or_none,
17 ExtractorError,
18 unescapeHTML,
19 )
20
21 class DailymotionBaseInfoExtractor(InfoExtractor):
22 @staticmethod
23 def _build_request(url):
24 """Build a request with the family filter disabled"""
25 request = compat_urllib_request.Request(url)
26 request.add_header('Cookie', 'family_filter=off')
27 request.add_header('Cookie', 'ff=off')
28 return request
29
30 class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
31 """Information Extractor for Dailymotion"""
32
33 _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
34 IE_NAME = 'dailymotion'
35
36 _FORMATS = [
37 ('stream_h264_ld_url', 'ld'),
38 ('stream_h264_url', 'standard'),
39 ('stream_h264_hq_url', 'hq'),
40 ('stream_h264_hd_url', 'hd'),
41 ('stream_h264_hd1080_url', 'hd180'),
42 ]
43
44 _TESTS = [
45 {
46 'url': 'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
47 'md5': '392c4b85a60a90dc4792da41ce3144eb',
48 'info_dict': {
49 'id': 'x33vw9',
50 'ext': 'mp4',
51 'uploader': 'Amphora Alex and Van .',
52 'title': 'Tutoriel de Youtubeur"DL DES VIDEO DE YOUTUBE"',
53 }
54 },
55 # Vevo video
56 {
57 'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
58 'info_dict': {
59 'title': 'Roar (Official)',
60 'id': 'USUV71301934',
61 'ext': 'mp4',
62 'uploader': 'Katy Perry',
63 'upload_date': '20130905',
64 },
65 'params': {
66 'skip_download': True,
67 },
68 'skip': 'VEVO is only available in some countries',
69 },
70 # age-restricted video
71 {
72 'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband',
73 'md5': '0d667a7b9cebecc3c89ee93099c4159d',
74 'info_dict': {
75 'id': 'xyh2zz',
76 'ext': 'mp4',
77 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]',
78 'uploader': 'HotWaves1012',
79 'age_limit': 18,
80 }
81 }
82 ]
83
84 def _real_extract(self, url):
85 video_id = self._match_id(url)
86 url = 'http://www.dailymotion.com/video/%s' % video_id
87
88 # Retrieve video webpage to extract further information
89 request = self._build_request(url)
90 webpage = self._download_webpage(request, video_id)
91
92 # Extract URL, uploader and title from webpage
93 self.report_extraction(video_id)
94
95 # It may just embed a vevo video:
96 m_vevo = re.search(
97 r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?videoId=(?P<id>[\w]*)',
98 webpage)
99 if m_vevo is not None:
100 vevo_id = m_vevo.group('id')
101 self.to_screen('Vevo video detected: %s' % vevo_id)
102 return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
103
104 age_limit = self._rta_search(webpage)
105
106 video_upload_date = None
107 mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
108 if mobj is not None:
109 video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
110
111 embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
112 embed_page = self._download_webpage(embed_url, video_id,
113 'Downloading embed page')
114 info = self._search_regex(r'var info = ({.*?}),$', embed_page,
115 'video info', flags=re.MULTILINE)
116 info = json.loads(info)
117 if info.get('error') is not None:
118 msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
119 raise ExtractorError(msg, expected=True)
120
121 formats = []
122 for (key, format_id) in self._FORMATS:
123 video_url = info.get(key)
124 if video_url is not None:
125 m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
126 if m_size is not None:
127 width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
128 else:
129 width, height = None, None
130 formats.append({
131 'url': video_url,
132 'ext': 'mp4',
133 'format_id': format_id,
134 'width': width,
135 'height': height,
136 })
137 if not formats:
138 raise ExtractorError('Unable to extract video URL')
139
140 # subtitles
141 video_subtitles = self.extract_subtitles(video_id, webpage)
142 if self._downloader.params.get('listsubtitles', False):
143 self._list_available_subtitles(video_id, webpage)
144 return
145
146 view_count = str_to_int(self._search_regex(
147 r'video_views_count[^>]+>\s+([\d\.,]+)',
148 webpage, 'view count', fatal=False))
149
150 title = self._og_search_title(webpage, default=None)
151 if title is None:
152 title = self._html_search_regex(
153 r'(?s)<span\s+id="video_title"[^>]*>(.*?)</span>', webpage,
154 'title')
155
156 return {
157 'id': video_id,
158 'formats': formats,
159 'uploader': info['owner.screenname'],
160 'upload_date': video_upload_date,
161 'title': title,
162 'subtitles': video_subtitles,
163 'thumbnail': info['thumbnail_url'],
164 'age_limit': age_limit,
165 'view_count': view_count,
166 }
167
168 def _get_available_subtitles(self, video_id, webpage):
169 try:
170 sub_list = self._download_webpage(
171 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
172 video_id, note=False)
173 except ExtractorError as err:
174 self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
175 return {}
176 info = json.loads(sub_list)
177 if (info['total'] > 0):
178 sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
179 return sub_lang_list
180 self._downloader.report_warning('video doesn\'t have subtitles')
181 return {}
182
183
184 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
185 IE_NAME = 'dailymotion:playlist'
186 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
187 _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
188 _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
189 _TESTS = [{
190 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
191 'info_dict': {
192 'title': 'SPORT',
193 },
194 'playlist_mincount': 20,
195 }]
196
197 def _extract_entries(self, id):
198 video_ids = []
199 for pagenum in itertools.count(1):
200 request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
201 webpage = self._download_webpage(request,
202 id, 'Downloading page %s' % pagenum)
203
204 video_ids.extend(re.findall(r'data-xid="(.+?)"', webpage))
205
206 if re.search(self._MORE_PAGES_INDICATOR, webpage) is None:
207 break
208 return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
209 for video_id in orderedSet(video_ids)]
210
211 def _real_extract(self, url):
212 mobj = re.match(self._VALID_URL, url)
213 playlist_id = mobj.group('id')
214 webpage = self._download_webpage(url, playlist_id)
215
216 return {
217 '_type': 'playlist',
218 'id': playlist_id,
219 'title': self._og_search_title(webpage),
220 'entries': self._extract_entries(playlist_id),
221 }
222
223
224 class DailymotionUserIE(DailymotionPlaylistIE):
225 IE_NAME = 'dailymotion:user'
226 _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
227 _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
228 _TESTS = [{
229 'url': 'https://www.dailymotion.com/user/nqtv',
230 'info_dict': {
231 'id': 'nqtv',
232 'title': 'Rémi Gaillard',
233 },
234 'playlist_mincount': 100,
235 }]
236
237 def _real_extract(self, url):
238 mobj = re.match(self._VALID_URL, url)
239 user = mobj.group('user')
240 webpage = self._download_webpage(url, user)
241 full_user = unescapeHTML(self._html_search_regex(
242 r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
243 webpage, 'user'))
244
245 return {
246 '_type': 'playlist',
247 'id': user,
248 'title': full_user,
249 'entries': self._extract_entries(user),
250 }