]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dailymotion.py
Finalize changelog.
[youtubedl] / youtube_dl / extractor / dailymotion.py
1 import re
2 import json
3 import itertools
4
5 from .common import InfoExtractor
6 from .subtitles import SubtitlesInfoExtractor
7
8 from ..utils import (
9 compat_urllib_request,
10 compat_str,
11 get_element_by_attribute,
12 get_element_by_id,
13
14 ExtractorError,
15 )
16
17 class DailymotionBaseInfoExtractor(InfoExtractor):
18 @staticmethod
19 def _build_request(url):
20 """Build a request with the family filter disabled"""
21 request = compat_urllib_request.Request(url)
22 request.add_header('Cookie', 'family_filter=off')
23 return request
24
25 class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
26 """Information Extractor for Dailymotion"""
27
28 _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)'
29 IE_NAME = u'dailymotion'
30 _TESTS = [
31 {
32 u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
33 u'file': u'x33vw9.mp4',
34 u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
35 u'info_dict': {
36 u"uploader": u"Amphora Alex and Van .",
37 u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
38 }
39 },
40 # Vevo video
41 {
42 u'url': u'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
43 u'file': u'USUV71301934.mp4',
44 u'info_dict': {
45 u'title': u'Roar (Official)',
46 u'uploader': u'Katy Perry',
47 u'upload_date': u'20130905',
48 },
49 u'params': {
50 u'skip_download': True,
51 },
52 u'skip': u'VEVO is only available in some countries',
53 },
54 ]
55
56 def _real_extract(self, url):
57 # Extract id and simplified title from URL
58 mobj = re.match(self._VALID_URL, url)
59
60 video_id = mobj.group(1).split('_')[0].split('?')[0]
61
62 video_extension = 'mp4'
63 url = 'http://www.dailymotion.com/video/%s' % video_id
64
65 # Retrieve video webpage to extract further information
66 request = self._build_request(url)
67 webpage = self._download_webpage(request, video_id)
68
69 # Extract URL, uploader and title from webpage
70 self.report_extraction(video_id)
71
72 # It may just embed a vevo video:
73 m_vevo = re.search(
74 r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?videoId=(?P<id>[\w]*)',
75 webpage)
76 if m_vevo is not None:
77 vevo_id = m_vevo.group('id')
78 self.to_screen(u'Vevo video detected: %s' % vevo_id)
79 return self.url_result(u'vevo:%s' % vevo_id, ie='Vevo')
80
81 video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
82 # Looking for official user
83 r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
84 webpage, 'video uploader')
85
86 video_upload_date = None
87 mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
88 if mobj is not None:
89 video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
90
91 embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
92 embed_page = self._download_webpage(embed_url, video_id,
93 u'Downloading embed page')
94 info = self._search_regex(r'var info = ({.*?}),$', embed_page,
95 'video info', flags=re.MULTILINE)
96 info = json.loads(info)
97 if info.get('error') is not None:
98 msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
99 raise ExtractorError(msg, expected=True)
100
101 # TODO: support choosing qualities
102
103 for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
104 'stream_h264_hq_url','stream_h264_url',
105 'stream_h264_ld_url']:
106 if info.get(key):#key in info and info[key]:
107 max_quality = key
108 self.to_screen(u'Using %s' % key)
109 break
110 else:
111 raise ExtractorError(u'Unable to extract video URL')
112 video_url = info[max_quality]
113
114 # subtitles
115 video_subtitles = self.extract_subtitles(video_id)
116 if self._downloader.params.get('listsubtitles', False):
117 self._list_available_subtitles(video_id)
118 return
119
120 return [{
121 'id': video_id,
122 'url': video_url,
123 'uploader': video_uploader,
124 'upload_date': video_upload_date,
125 'title': self._og_search_title(webpage),
126 'ext': video_extension,
127 'subtitles': video_subtitles,
128 'thumbnail': info['thumbnail_url']
129 }]
130
131 def _get_available_subtitles(self, video_id):
132 try:
133 sub_list = self._download_webpage(
134 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
135 video_id, note=False)
136 except ExtractorError as err:
137 self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
138 return {}
139 info = json.loads(sub_list)
140 if (info['total'] > 0):
141 sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
142 return sub_lang_list
143 self._downloader.report_warning(u'video doesn\'t have subtitles')
144 return {}
145
146
147 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
148 IE_NAME = u'dailymotion:playlist'
149 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
150 _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
151 _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
152
153 def _extract_entries(self, id):
154 video_ids = []
155 for pagenum in itertools.count(1):
156 request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
157 webpage = self._download_webpage(request,
158 id, u'Downloading page %s' % pagenum)
159
160 playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
161 video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
162
163 if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
164 break
165 return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
166 for video_id in video_ids]
167
168 def _real_extract(self, url):
169 mobj = re.match(self._VALID_URL, url)
170 playlist_id = mobj.group('id')
171 webpage = self._download_webpage(url, playlist_id)
172
173 return {'_type': 'playlist',
174 'id': playlist_id,
175 'title': get_element_by_id(u'playlist_name', webpage),
176 'entries': self._extract_entries(playlist_id),
177 }
178
179
180 class DailymotionUserIE(DailymotionPlaylistIE):
181 IE_NAME = u'dailymotion:user'
182 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
183 _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/user/.+?".*?>.*?</a>.*?</div>'
184 _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
185
186 def _real_extract(self, url):
187 mobj = re.match(self._VALID_URL, url)
188 user = mobj.group('user')
189 webpage = self._download_webpage(url, user)
190 full_user = self._html_search_regex(
191 r'<a class="label" href="/%s".*?>(.*?)</' % re.escape(user),
192 webpage, u'user', flags=re.DOTALL)
193
194 return {
195 '_type': 'playlist',
196 'id': user,
197 'title': full_user,
198 'entries': self._extract_entries(user),
199 }