]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/arte.py
b9a9440c09b85365a2997bd5feddbae017601c2d
[youtubedl] / youtube_dl / extractor / arte.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 find_xpath_attr,
10 unified_strdate,
11 determine_ext,
12 get_element_by_id,
13 get_element_by_attribute,
14 int_or_none,
15 )
16
17 # There are different sources of video in arte.tv, the extraction process
18 # is different for each one. The videos usually expire in 7 days, so we can't
19 # add tests.
20
21
22 class ArteTvIE(InfoExtractor):
23 _VALID_URL = r'http://videos\.arte\.tv/(?P<lang>fr|de)/.*-(?P<id>.*?)\.html'
24 IE_NAME = 'arte.tv'
25
26 def _real_extract(self, url):
27 mobj = re.match(self._VALID_URL, url)
28 lang = mobj.group('lang')
29 video_id = mobj.group('id')
30
31 ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
32 ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
33 ref_xml_doc = self._download_xml(
34 ref_xml_url, video_id, note='Downloading metadata')
35 config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
36 config_xml_url = config_node.attrib['ref']
37 config = self._download_xml(
38 config_xml_url, video_id, note='Downloading configuration')
39
40 formats = [{
41 'forma_id': q.attrib['quality'],
42 # The playpath starts at 'mp4:', if we don't manually
43 # split the url, rtmpdump will incorrectly parse them
44 'url': q.text.split('mp4:', 1)[0],
45 'play_path': 'mp4:' + q.text.split('mp4:', 1)[1],
46 'ext': 'flv',
47 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
48 } for q in config.findall('./urls/url')]
49 self._sort_formats(formats)
50
51 title = config.find('.//name').text
52 thumbnail = config.find('.//firstThumbnailUrl').text
53 return {
54 'id': video_id,
55 'title': title,
56 'thumbnail': thumbnail,
57 'formats': formats,
58 }
59
60
61 class ArteTVPlus7IE(InfoExtractor):
62 IE_NAME = 'arte.tv:+7'
63 _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
64
65 @classmethod
66 def _extract_url_info(cls, url):
67 mobj = re.match(cls._VALID_URL, url)
68 lang = mobj.group('lang')
69 # This is not a real id, it can be for example AJT for the news
70 # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
71 video_id = mobj.group('id')
72 return video_id, lang
73
74 def _real_extract(self, url):
75 video_id, lang = self._extract_url_info(url)
76 webpage = self._download_webpage(url, video_id)
77 return self._extract_from_webpage(webpage, video_id, lang)
78
79 def _extract_from_webpage(self, webpage, video_id, lang):
80 json_url = self._html_search_regex(
81 [r'arte_vp_url=["\'](.*?)["\']', r'data-url=["\']([^"]+)["\']'],
82 webpage, 'json vp url')
83 return self._extract_from_json_url(json_url, video_id, lang)
84
85 def _extract_from_json_url(self, json_url, video_id, lang):
86 info = self._download_json(json_url, video_id)
87 player_info = info['videoJsonPlayer']
88
89 upload_date_str = player_info.get('shootingDate')
90 if not upload_date_str:
91 upload_date_str = player_info.get('VDA', '').split(' ')[0]
92
93 title = player_info['VTI'].strip()
94 subtitle = player_info.get('VSU', '').strip()
95 if subtitle:
96 title += ' - %s' % subtitle
97
98 info_dict = {
99 'id': player_info['VID'],
100 'title': title,
101 'description': player_info.get('VDE'),
102 'upload_date': unified_strdate(upload_date_str),
103 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
104 }
105
106 all_formats = []
107 for format_id, format_dict in player_info['VSR'].items():
108 fmt = dict(format_dict)
109 fmt['format_id'] = format_id
110 all_formats.append(fmt)
111 # Some formats use the m3u8 protocol
112 all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
113 def _match_lang(f):
114 if f.get('versionCode') is None:
115 return True
116 # Return true if that format is in the language of the url
117 if lang == 'fr':
118 l = 'F'
119 elif lang == 'de':
120 l = 'A'
121 else:
122 l = lang
123 regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
124 return any(re.match(r, f['versionCode']) for r in regexes)
125 # Some formats may not be in the same language as the url
126 # TODO: Might want not to drop videos that does not match requested language
127 # but to process those formats with lower precedence
128 formats = filter(_match_lang, all_formats)
129 formats = list(formats) # in python3 filter returns an iterator
130 if not formats:
131 # Some videos are only available in the 'Originalversion'
132 # they aren't tagged as being in French or German
133 # Sometimes there are neither videos of requested lang code
134 # nor original version videos available
135 # For such cases we just take all_formats as is
136 formats = all_formats
137 if not formats:
138 raise ExtractorError('The formats list is empty')
139
140 if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
141 def sort_key(f):
142 return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
143 else:
144 def sort_key(f):
145 versionCode = f.get('versionCode')
146 if versionCode is None:
147 versionCode = ''
148 return (
149 # Sort first by quality
150 int(f.get('height', -1)),
151 int(f.get('bitrate', -1)),
152 # The original version with subtitles has lower relevance
153 re.match(r'VO-ST(F|A)', versionCode) is None,
154 # The version with sourds/mal subtitles has also lower relevance
155 re.match(r'VO?(F|A)-STM\1', versionCode) is None,
156 # Prefer http downloads over m3u8
157 0 if f['url'].endswith('m3u8') else 1,
158 )
159 formats = sorted(formats, key=sort_key)
160 def _format(format_info):
161 info = {
162 'format_id': format_info['format_id'],
163 'format_note': '%s, %s' % (format_info.get('versionCode'), format_info.get('versionLibelle')),
164 'width': int_or_none(format_info.get('width')),
165 'height': int_or_none(format_info.get('height')),
166 'tbr': int_or_none(format_info.get('bitrate')),
167 }
168 if format_info['mediaType'] == 'rtmp':
169 info['url'] = format_info['streamer']
170 info['play_path'] = 'mp4:' + format_info['url']
171 info['ext'] = 'flv'
172 else:
173 info['url'] = format_info['url']
174 info['ext'] = determine_ext(info['url'])
175 return info
176 info_dict['formats'] = [_format(f) for f in formats]
177
178 return info_dict
179
180
181 # It also uses the arte_vp_url url from the webpage to extract the information
182 class ArteTVCreativeIE(ArteTVPlus7IE):
183 IE_NAME = 'arte.tv:creative'
184 _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/(?:magazine?/)?(?P<id>[^?#]+)'
185
186 _TESTS = [{
187 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
188 'info_dict': {
189 'id': '72176',
190 'ext': 'mp4',
191 'title': 'Folge 2 - Corporate Design',
192 'upload_date': '20131004',
193 },
194 }, {
195 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
196 'info_dict': {
197 'id': '160676',
198 'ext': 'mp4',
199 'title': 'Monty Python live (mostly)',
200 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
201 'upload_date': '20140805',
202 }
203 }]
204
205
206 class ArteTVFutureIE(ArteTVPlus7IE):
207 IE_NAME = 'arte.tv:future'
208 _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
209
210 _TEST = {
211 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
212 'info_dict': {
213 'id': '5201',
214 'ext': 'mp4',
215 'title': 'Les champignons au secours de la planète',
216 'upload_date': '20131101',
217 },
218 }
219
220 def _real_extract(self, url):
221 anchor_id, lang = self._extract_url_info(url)
222 webpage = self._download_webpage(url, anchor_id)
223 row = get_element_by_id(anchor_id, webpage)
224 return self._extract_from_webpage(row, anchor_id, lang)
225
226
227 class ArteTVDDCIE(ArteTVPlus7IE):
228 IE_NAME = 'arte.tv:ddc'
229 _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
230
231 def _real_extract(self, url):
232 video_id, lang = self._extract_url_info(url)
233 if lang == 'folge':
234 lang = 'de'
235 elif lang == 'emission':
236 lang = 'fr'
237 webpage = self._download_webpage(url, video_id)
238 scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
239 script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
240 javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
241 json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
242 return self._extract_from_json_url(json_url, video_id, lang)
243
244
245 class ArteTVConcertIE(ArteTVPlus7IE):
246 IE_NAME = 'arte.tv:concert'
247 _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
248
249 _TEST = {
250 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
251 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
252 'info_dict': {
253 'id': '186',
254 'ext': 'mp4',
255 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
256 'upload_date': '20140128',
257 'description': 'md5:486eb08f991552ade77439fe6d82c305',
258 },
259 }
260
261
262 class ArteTVEmbedIE(ArteTVPlus7IE):
263 IE_NAME = 'arte.tv:embed'
264 _VALID_URL = r'''(?x)
265 http://www\.arte\.tv
266 /playerv2/embed\.php\?json_url=
267 (?P<json_url>
268 http://arte\.tv/papi/tvguide/videos/stream/player/
269 (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
270 )
271 '''
272
273 def _real_extract(self, url):
274 mobj = re.match(self._VALID_URL, url)
275 video_id = mobj.group('id')
276 lang = mobj.group('lang')
277 json_url = mobj.group('json_url')
278 return self._extract_from_json_url(json_url, video_id, lang)