]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/arte.py
9591bad8a66254e90247a204b43b10b6db4f6406
[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 compat_str,
14 get_element_by_attribute,
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="(.*?)"', webpage, 'json vp url')
82 return self._extract_from_json_url(json_url, video_id, lang)
83
84 def _extract_from_json_url(self, json_url, video_id, lang):
85 info = self._download_json(json_url, video_id)
86 player_info = info['videoJsonPlayer']
87
88 info_dict = {
89 'id': player_info['VID'],
90 'title': player_info['VTI'],
91 'description': player_info.get('VDE'),
92 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
93 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
94 }
95
96 all_formats = player_info['VSR'].values()
97 # Some formats use the m3u8 protocol
98 all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
99 def _match_lang(f):
100 if f.get('versionCode') is None:
101 return True
102 # Return true if that format is in the language of the url
103 if lang == 'fr':
104 l = 'F'
105 elif lang == 'de':
106 l = 'A'
107 else:
108 l = lang
109 regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
110 return any(re.match(r, f['versionCode']) for r in regexes)
111 # Some formats may not be in the same language as the url
112 formats = filter(_match_lang, all_formats)
113 formats = list(formats) # in python3 filter returns an iterator
114 if not formats:
115 # Some videos are only available in the 'Originalversion'
116 # they aren't tagged as being in French or German
117 if all(f['versionCode'] == 'VO' or f['versionCode'] == 'VA' for f in all_formats):
118 formats = all_formats
119 else:
120 raise ExtractorError(u'The formats list is empty')
121
122 if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
123 def sort_key(f):
124 return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
125 else:
126 def sort_key(f):
127 versionCode = f.get('versionCode')
128 if versionCode is None:
129 versionCode = ''
130 return (
131 # Sort first by quality
132 int(f.get('height', -1)),
133 int(f.get('bitrate', -1)),
134 # The original version with subtitles has lower relevance
135 re.match(r'VO-ST(F|A)', versionCode) is None,
136 # The version with sourds/mal subtitles has also lower relevance
137 re.match(r'VO?(F|A)-STM\1', versionCode) is None,
138 # Prefer http downloads over m3u8
139 0 if f['url'].endswith('m3u8') else 1,
140 )
141 formats = sorted(formats, key=sort_key)
142 def _format(format_info):
143 quality = ''
144 height = format_info.get('height')
145 if height is not None:
146 quality = compat_str(height)
147 bitrate = format_info.get('bitrate')
148 if bitrate is not None:
149 quality += '-%d' % bitrate
150 if format_info.get('versionCode') is not None:
151 format_id = '%s-%s' % (quality, format_info['versionCode'])
152 else:
153 format_id = quality
154 info = {
155 'format_id': format_id,
156 'format_note': format_info.get('versionLibelle'),
157 'width': format_info.get('width'),
158 'height': height,
159 }
160 if format_info['mediaType'] == 'rtmp':
161 info['url'] = format_info['streamer']
162 info['play_path'] = 'mp4:' + format_info['url']
163 info['ext'] = 'flv'
164 else:
165 info['url'] = format_info['url']
166 info['ext'] = determine_ext(info['url'])
167 return info
168 info_dict['formats'] = [_format(f) for f in formats]
169
170 return info_dict
171
172
173 # It also uses the arte_vp_url url from the webpage to extract the information
174 class ArteTVCreativeIE(ArteTVPlus7IE):
175 IE_NAME = 'arte.tv:creative'
176 _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
177
178 _TEST = {
179 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
180 'info_dict': {
181 'id': '050489-002',
182 'ext': 'mp4',
183 'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
184 },
185 }
186
187
188 class ArteTVFutureIE(ArteTVPlus7IE):
189 IE_NAME = 'arte.tv:future'
190 _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
191
192 _TEST = {
193 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
194 'info_dict': {
195 'id': '5201',
196 'ext': 'mp4',
197 'title': 'Les champignons au secours de la planète',
198 'upload_date': '20131101',
199 },
200 }
201
202 def _real_extract(self, url):
203 anchor_id, lang = self._extract_url_info(url)
204 webpage = self._download_webpage(url, anchor_id)
205 row = get_element_by_id(anchor_id, webpage)
206 return self._extract_from_webpage(row, anchor_id, lang)
207
208
209 class ArteTVDDCIE(ArteTVPlus7IE):
210 IE_NAME = 'arte.tv:ddc'
211 _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
212
213 def _real_extract(self, url):
214 video_id, lang = self._extract_url_info(url)
215 if lang == 'folge':
216 lang = 'de'
217 elif lang == 'emission':
218 lang = 'fr'
219 webpage = self._download_webpage(url, video_id)
220 scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
221 script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
222 javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
223 json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
224 return self._extract_from_json_url(json_url, video_id, lang)
225
226
227 class ArteTVConcertIE(ArteTVPlus7IE):
228 IE_NAME = 'arte.tv:concert'
229 _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
230
231 _TEST = {
232 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
233 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
234 'info_dict': {
235 'id': '186',
236 'ext': 'mp4',
237 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
238 'upload_date': '20140128',
239 'description': 'md5:486eb08f991552ade77439fe6d82c305',
240 },
241 }
242
243
244 class ArteTVEmbedIE(ArteTVPlus7IE):
245 IE_NAME = 'arte.tv:embed'
246 _VALID_URL = r'''(?x)
247 http://www\.arte\.tv
248 /playerv2/embed\.php\?json_url=
249 (?P<json_url>
250 http://arte\.tv/papi/tvguide/videos/stream/player/
251 (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
252 )
253 '''
254
255 def _real_extract(self, url):
256 mobj = re.match(self._VALID_URL, url)
257 video_id = mobj.group('id')
258 lang = mobj.group('lang')
259 json_url = mobj.group('json_url')
260 return self._extract_from_json_url(json_url, video_id, lang)