]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/arte.py
[arte] Add support for multiple formats
[yt-dlp.git] / youtube_dl / extractor / arte.py
CommitLineData
69a0c470 1# encoding: utf-8
3798eadc
PH
2from __future__ import unicode_literals
3
d5822b96 4import re
75c94812 5import json
d5822b96
PH
6
7from .common import InfoExtractor
8from ..utils import (
d5822b96 9 ExtractorError,
df50a412 10 find_xpath_attr,
d5822b96 11 unified_strdate,
c40f5cf4 12 determine_ext,
69a0c470 13 get_element_by_id,
566d4e04 14 compat_str,
56a8ab7d 15 get_element_by_attribute,
d5822b96
PH
16)
17
c40f5cf4
JMF
18# There are different sources of video in arte.tv, the extraction process
19# is different for each one. The videos usually expire in 7 days, so we can't
20# add tests.
21
d5822b96 22
515bbe4b 23class ArteTvIE(InfoExtractor):
878d11ec 24 _VALID_URL = r'http://videos\.arte\.tv/(?P<lang>fr|de)/.*-(?P<id>.*?)\.html'
3798eadc 25 IE_NAME = 'arte.tv'
d5822b96 26
d5822b96 27 def _real_extract(self, url):
8de64cac
PH
28 ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
29 ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
a4ff6c47
PH
30 ref_xml_doc = self._download_xml(
31 ref_xml_url, video_id, note='Downloading metadata')
df50a412 32 config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
8de64cac 33 config_xml_url = config_node.attrib['ref']
878d11ec 34 config = self._download_xml(
a4ff6c47 35 config_xml_url, video_id, note='Downloading configuration')
37b6a661 36
878d11ec
PH
37 formats = [{
38 'forma_id': q.attrib['quality'],
39 'url': q.text,
40 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
41 } for q in config.findall('.//quality')]
42 self._sort_formats(formats)
43
44 title = config.find('.//name').text
45 thumbnail = config.find('.//firstThumbnailUrl').text
46 return {
47 'id': video_id,
48 'title': title,
49 'thumbnail': thumbnail,
50 'url': video_url,
51 'ext': 'flv',
515bbe4b 52 }
c40f5cf4
JMF
53
54
55class ArteTVPlus7IE(InfoExtractor):
3798eadc 56 IE_NAME = 'arte.tv:+7'
f66ede43 57 _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
c40f5cf4 58
69a0c470
JMF
59 @classmethod
60 def _extract_url_info(cls, url):
61 mobj = re.match(cls._VALID_URL, url)
c40f5cf4
JMF
62 lang = mobj.group('lang')
63 # This is not a real id, it can be for example AJT for the news
64 # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
65 video_id = mobj.group('id')
69a0c470 66 return video_id, lang
c40f5cf4 67
69a0c470
JMF
68 def _real_extract(self, url):
69 video_id, lang = self._extract_url_info(url)
c40f5cf4 70 webpage = self._download_webpage(url, video_id)
69a0c470
JMF
71 return self._extract_from_webpage(webpage, video_id, lang)
72
73 def _extract_from_webpage(self, webpage, video_id, lang):
c40f5cf4 74 json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
56a8ab7d 75 return self._extract_from_json_url(json_url, video_id, lang)
c40f5cf4 76
56a8ab7d 77 def _extract_from_json_url(self, json_url, video_id, lang):
c40f5cf4
JMF
78 json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
79 self.report_extraction(video_id)
80 info = json.loads(json_info)
81 player_info = info['videoJsonPlayer']
82
83 info_dict = {
84 'id': player_info['VID'],
85 'title': player_info['VTI'],
86 'description': player_info.get('VDE'),
87 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
88 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
89 }
90
21c924f4
JMF
91 all_formats = player_info['VSR'].values()
92 # Some formats use the m3u8 protocol
93 all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
c40f5cf4
JMF
94 def _match_lang(f):
95 if f.get('versionCode') is None:
96 return True
97 # Return true if that format is in the language of the url
98 if lang == 'fr':
99 l = 'F'
100 elif lang == 'de':
101 l = 'A'
102 regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
103 return any(re.match(r, f['versionCode']) for r in regexes)
104 # Some formats may not be in the same language as the url
21c924f4 105 formats = filter(_match_lang, all_formats)
182a1078 106 formats = list(formats) # in python3 filter returns an iterator
21c924f4
JMF
107 if not formats:
108 # Some videos are only available in the 'Originalversion'
109 # they aren't tagged as being in French or German
110 if all(f['versionCode'] == 'VO' for f in all_formats):
111 formats = all_formats
112 else:
113 raise ExtractorError(u'The formats list is empty')
f470c6c8 114
182a1078 115 if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
f470c6c8
JMF
116 def sort_key(f):
117 return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
182a1078 118 else:
f470c6c8
JMF
119 def sort_key(f):
120 return (
121 # Sort first by quality
122 int(f.get('height',-1)),
123 int(f.get('bitrate',-1)),
124 # The original version with subtitles has lower relevance
125 re.match(r'VO-ST(F|A)', f.get('versionCode', '')) is None,
126 # The version with sourds/mal subtitles has also lower relevance
127 re.match(r'VO?(F|A)-STM\1', f.get('versionCode', '')) is None,
4966a0b2
JMF
128 # Prefer http downloads over m3u8
129 0 if f['url'].endswith('m3u8') else 1,
f470c6c8 130 )
182a1078 131 formats = sorted(formats, key=sort_key)
c40f5cf4 132 def _format(format_info):
566d4e04
JMF
133 quality = ''
134 height = format_info.get('height')
135 if height is not None:
136 quality = compat_str(height)
137 bitrate = format_info.get('bitrate')
138 if bitrate is not None:
139 quality += '-%d' % bitrate
182a1078 140 if format_info.get('versionCode') is not None:
3798eadc 141 format_id = '%s-%s' % (quality, format_info['versionCode'])
182a1078
JMF
142 else:
143 format_id = quality
c40f5cf4 144 info = {
182a1078
JMF
145 'format_id': format_id,
146 'format_note': format_info.get('versionLibelle'),
c40f5cf4 147 'width': format_info.get('width'),
566d4e04 148 'height': height,
c40f5cf4 149 }
3798eadc 150 if format_info['mediaType'] == 'rtmp':
c40f5cf4
JMF
151 info['url'] = format_info['streamer']
152 info['play_path'] = 'mp4:' + format_info['url']
153 info['ext'] = 'flv'
154 else:
155 info['url'] = format_info['url']
156 info['ext'] = determine_ext(info['url'])
157 return info
158 info_dict['formats'] = [_format(f) for f in formats]
c40f5cf4
JMF
159
160 return info_dict
161
162
163# It also uses the arte_vp_url url from the webpage to extract the information
164class ArteTVCreativeIE(ArteTVPlus7IE):
3798eadc 165 IE_NAME = 'arte.tv:creative'
c40f5cf4
JMF
166 _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
167
168 _TEST = {
3798eadc 169 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
3798eadc 170 'info_dict': {
39a743fb
JMF
171 'id': '050489-002',
172 'ext': 'mp4',
3798eadc 173 'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
c40f5cf4
JMF
174 },
175 }
176
69a0c470
JMF
177
178class ArteTVFutureIE(ArteTVPlus7IE):
3798eadc 179 IE_NAME = 'arte.tv:future'
69a0c470
JMF
180 _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
181
182 _TEST = {
3798eadc 183 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
3798eadc 184 'info_dict': {
39a743fb
JMF
185 'id': '050940-003',
186 'ext': 'mp4',
3798eadc 187 'title': 'Les champignons au secours de la planète',
69a0c470
JMF
188 },
189 }
190
191 def _real_extract(self, url):
192 anchor_id, lang = self._extract_url_info(url)
193 webpage = self._download_webpage(url, anchor_id)
194 row = get_element_by_id(anchor_id, webpage)
195 return self._extract_from_webpage(row, anchor_id, lang)
56a8ab7d 196
ac5118bc 197
56a8ab7d 198class ArteTVDDCIE(ArteTVPlus7IE):
3798eadc 199 IE_NAME = 'arte.tv:ddc'
39a743fb 200 _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
56a8ab7d 201
56a8ab7d
CD
202 def _real_extract(self, url):
203 video_id, lang = self._extract_url_info(url)
204 if lang == 'folge':
205 lang = 'de'
206 elif lang == 'emission':
207 lang = 'fr'
208 webpage = self._download_webpage(url, video_id)
209 scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
210 script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
211 javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
212 json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
213 return self._extract_from_json_url(json_url, video_id, lang)
4966a0b2
JMF
214
215
216class ArteTVConcertIE(ArteTVPlus7IE):
217 IE_NAME = 'arte.tv:concert'
218 _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
219
220 _TEST = {
221 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
222 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
223 'info_dict': {
224 'id': '186',
225 'ext': 'mp4',
226 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
227 'upload_date': '20140128',
a9c2896e 228 'description': 'md5:486eb08f991552ade77439fe6d82c305',
4966a0b2
JMF
229 },
230 }