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