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