]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dailymotion.py
Update 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 _TEST = {
31 u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
32 u'file': u'x33vw9.mp4',
33 u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
34 u'info_dict': {
35 u"uploader": u"Amphora Alex and Van .",
36 u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
37 }
38 }
39
40 def _real_extract(self, url):
41 # Extract id and simplified title from URL
42 mobj = re.match(self._VALID_URL, url)
43
44 video_id = mobj.group(1).split('_')[0].split('?')[0]
45
46 video_extension = 'mp4'
47 url = 'http://www.dailymotion.com/video/%s' % video_id
48
49 # Retrieve video webpage to extract further information
50 request = self._build_request(url)
51 webpage = self._download_webpage(request, video_id)
52
53 # Extract URL, uploader and title from webpage
54 self.report_extraction(video_id)
55
56 video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
57 # Looking for official user
58 r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
59 webpage, 'video uploader')
60
61 video_upload_date = None
62 mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
63 if mobj is not None:
64 video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
65
66 embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
67 embed_page = self._download_webpage(embed_url, video_id,
68 u'Downloading embed page')
69 info = self._search_regex(r'var info = ({.*?}),$', embed_page,
70 'video info', flags=re.MULTILINE)
71 info = json.loads(info)
72 if info.get('error') is not None:
73 msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
74 raise ExtractorError(msg, expected=True)
75
76 # TODO: support choosing qualities
77
78 for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
79 'stream_h264_hq_url','stream_h264_url',
80 'stream_h264_ld_url']:
81 if info.get(key):#key in info and info[key]:
82 max_quality = key
83 self.to_screen(u'Using %s' % key)
84 break
85 else:
86 raise ExtractorError(u'Unable to extract video URL')
87 video_url = info[max_quality]
88
89 # subtitles
90 video_subtitles = self.extract_subtitles(video_id)
91 if self._downloader.params.get('listsubtitles', False):
92 self._list_available_subtitles(video_id)
93 return
94
95 return [{
96 'id': video_id,
97 'url': video_url,
98 'uploader': video_uploader,
99 'upload_date': video_upload_date,
100 'title': self._og_search_title(webpage),
101 'ext': video_extension,
102 'subtitles': video_subtitles,
103 'thumbnail': info['thumbnail_url']
104 }]
105
106 def _get_available_subtitles(self, video_id):
107 try:
108 sub_list = self._download_webpage(
109 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
110 video_id, note=False)
111 except ExtractorError as err:
112 self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
113 return {}
114 info = json.loads(sub_list)
115 if (info['total'] > 0):
116 sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
117 return sub_lang_list
118 self._downloader.report_warning(u'video doesn\'t have subtitles')
119 return {}
120
121
122 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
123 IE_NAME = u'dailymotion:playlist'
124 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
125 _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
126 _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
127
128 def _extract_entries(self, id):
129 video_ids = []
130 for pagenum in itertools.count(1):
131 request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
132 webpage = self._download_webpage(request,
133 id, u'Downloading page %s' % pagenum)
134
135 playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
136 video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
137
138 if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
139 break
140 return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
141 for video_id in video_ids]
142
143 def _real_extract(self, url):
144 mobj = re.match(self._VALID_URL, url)
145 playlist_id = mobj.group('id')
146 webpage = self._download_webpage(url, playlist_id)
147
148 return {'_type': 'playlist',
149 'id': playlist_id,
150 'title': get_element_by_id(u'playlist_name', webpage),
151 'entries': self._extract_entries(playlist_id),
152 }
153
154
155 class DailymotionUserIE(DailymotionPlaylistIE):
156 IE_NAME = u'dailymotion:user'
157 _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
158 _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/user/.+?".*?>.*?</a>.*?</div>'
159 _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
160
161 def _real_extract(self, url):
162 mobj = re.match(self._VALID_URL, url)
163 user = mobj.group('user')
164 webpage = self._download_webpage(url, user)
165 full_user = self._html_search_regex(
166 r'<a class="label" href="/%s".*?>(.*?)</' % re.escape(user),
167 webpage, u'user', flags=re.DOTALL)
168
169 return {
170 '_type': 'playlist',
171 'id': user,
172 'title': full_user,
173 'entries': self._extract_entries(user),
174 }