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