]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/arte.py
Merge pull request #1678 from rzhxeo/keezmovies
[yt-dlp.git] / youtube_dl / extractor / arte.py
1 # encoding: utf-8
2 import re
3 import json
4 import xml.etree.ElementTree
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 )
14
15 # There are different sources of video in arte.tv, the extraction process
16 # is different for each one. The videos usually expire in 7 days, so we can't
17 # add tests.
18
19 class ArteTvIE(InfoExtractor):
20 _VIDEOS_URL = r'(?:http://)?videos.arte.tv/(?P<lang>fr|de)/.*-(?P<id>.*?).html'
21 _LIVEWEB_URL = r'(?:http://)?liveweb.arte.tv/(?P<lang>fr|de)/(?P<subpage>.+?)/(?P<name>.+)'
22 _LIVE_URL = r'index-[0-9]+\.html$'
23
24 IE_NAME = u'arte.tv'
25
26 @classmethod
27 def suitable(cls, url):
28 return any(re.match(regex, url) for regex in (cls._VIDEOS_URL, cls._LIVEWEB_URL))
29
30 # TODO implement Live Stream
31 # from ..utils import compat_urllib_parse
32 # def extractLiveStream(self, url):
33 # video_lang = url.split('/')[-4]
34 # info = self.grep_webpage(
35 # url,
36 # r'src="(.*?/videothek_js.*?\.js)',
37 # 0,
38 # [
39 # (1, 'url', u'Invalid URL: %s' % url)
40 # ]
41 # )
42 # http_host = url.split('/')[2]
43 # next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
44 # info = self.grep_webpage(
45 # next_url,
46 # r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
47 # '(http://.*?\.swf).*?' +
48 # '(rtmp://.*?)\'',
49 # re.DOTALL,
50 # [
51 # (1, 'path', u'could not extract video path: %s' % url),
52 # (2, 'player', u'could not extract video player: %s' % url),
53 # (3, 'url', u'could not extract video url: %s' % url)
54 # ]
55 # )
56 # video_url = u'%s/%s' % (info.get('url'), info.get('path'))
57
58 def _real_extract(self, url):
59 mobj = re.match(self._VIDEOS_URL, url)
60 if mobj is not None:
61 id = mobj.group('id')
62 lang = mobj.group('lang')
63 return self._extract_video(url, id, lang)
64
65 mobj = re.match(self._LIVEWEB_URL, url)
66 if mobj is not None:
67 name = mobj.group('name')
68 lang = mobj.group('lang')
69 return self._extract_liveweb(url, name, lang)
70
71 if re.search(self._LIVE_URL, video_id) is not None:
72 raise ExtractorError(u'Arte live streams are not yet supported, sorry')
73 # self.extractLiveStream(url)
74 # return
75
76 def _extract_video(self, url, video_id, lang):
77 """Extract from videos.arte.tv"""
78 ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
79 ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
80 ref_xml = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
81 ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
82 config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
83 config_xml_url = config_node.attrib['ref']
84 config_xml = self._download_webpage(config_xml_url, video_id, note=u'Downloading configuration')
85
86 video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
87 def _key(m):
88 quality = m.group('quality')
89 if quality == 'hd':
90 return 2
91 else:
92 return 1
93 # We pick the best quality
94 video_urls = sorted(video_urls, key=_key)
95 video_url = list(video_urls)[-1].group('url')
96
97 title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
98 thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
99 config_xml, 'thumbnail')
100 return {'id': video_id,
101 'title': title,
102 'thumbnail': thumbnail,
103 'url': video_url,
104 'ext': 'flv',
105 }
106
107 def _extract_liveweb(self, url, name, lang):
108 """Extract form http://liveweb.arte.tv/"""
109 webpage = self._download_webpage(url, name)
110 video_id = self._search_regex(r'eventId=(\d+?)("|&)', webpage, u'event id')
111 config_xml = self._download_webpage('http://download.liveweb.arte.tv/o21/liveweb/events/event-%s.xml' % video_id,
112 video_id, u'Downloading information')
113 config_doc = xml.etree.ElementTree.fromstring(config_xml.encode('utf-8'))
114 event_doc = config_doc.find('event')
115 url_node = event_doc.find('video').find('urlHd')
116 if url_node is None:
117 url_node = video_doc.find('urlSd')
118
119 return {'id': video_id,
120 'title': event_doc.find('name%s' % lang.capitalize()).text,
121 'url': url_node.text.replace('MP4', 'mp4'),
122 'ext': 'flv',
123 'thumbnail': self._og_search_thumbnail(webpage),
124 }
125
126
127 class ArteTVPlus7IE(InfoExtractor):
128 IE_NAME = u'arte.tv:+7'
129 _VALID_URL = r'https?://www\.arte.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
130
131 @classmethod
132 def _extract_url_info(cls, url):
133 mobj = re.match(cls._VALID_URL, url)
134 lang = mobj.group('lang')
135 # This is not a real id, it can be for example AJT for the news
136 # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
137 video_id = mobj.group('id')
138 return video_id, lang
139
140 def _real_extract(self, url):
141 video_id, lang = self._extract_url_info(url)
142 webpage = self._download_webpage(url, video_id)
143 return self._extract_from_webpage(webpage, video_id, lang)
144
145 def _extract_from_webpage(self, webpage, video_id, lang):
146 json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
147
148 json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
149 self.report_extraction(video_id)
150 info = json.loads(json_info)
151 player_info = info['videoJsonPlayer']
152
153 info_dict = {
154 'id': player_info['VID'],
155 'title': player_info['VTI'],
156 'description': player_info.get('VDE'),
157 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
158 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
159 }
160
161 all_formats = player_info['VSR'].values()
162 # Some formats use the m3u8 protocol
163 all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
164 def _match_lang(f):
165 if f.get('versionCode') is None:
166 return True
167 # Return true if that format is in the language of the url
168 if lang == 'fr':
169 l = 'F'
170 elif lang == 'de':
171 l = 'A'
172 regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
173 return any(re.match(r, f['versionCode']) for r in regexes)
174 # Some formats may not be in the same language as the url
175 formats = filter(_match_lang, all_formats)
176 formats = list(formats) # in python3 filter returns an iterator
177 if not formats:
178 # Some videos are only available in the 'Originalversion'
179 # they aren't tagged as being in French or German
180 if all(f['versionCode'] == 'VO' for f in all_formats):
181 formats = all_formats
182 else:
183 raise ExtractorError(u'The formats list is empty')
184 # We order the formats by quality
185 if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
186 sort_key = lambda f: ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
187 else:
188 sort_key = lambda f: int(f.get('height',-1))
189 formats = sorted(formats, key=sort_key)
190 # Prefer videos without subtitles in the same language
191 formats = sorted(formats, key=lambda f: re.match(r'VO(F|A)-STM\1', f.get('versionCode', '')) is None)
192 # Pick the best quality
193 def _format(format_info):
194 quality = format_info['quality']
195 m_quality = re.match(r'\w*? - (\d*)p', quality)
196 if m_quality is not None:
197 quality = m_quality.group(1)
198 if format_info.get('versionCode') is not None:
199 format_id = u'%s-%s' % (quality, format_info['versionCode'])
200 else:
201 format_id = quality
202 info = {
203 'format_id': format_id,
204 'format_note': format_info.get('versionLibelle'),
205 'width': format_info.get('width'),
206 'height': format_info.get('height'),
207 }
208 if format_info['mediaType'] == u'rtmp':
209 info['url'] = format_info['streamer']
210 info['play_path'] = 'mp4:' + format_info['url']
211 info['ext'] = 'flv'
212 else:
213 info['url'] = format_info['url']
214 info['ext'] = determine_ext(info['url'])
215 return info
216 info_dict['formats'] = [_format(f) for f in formats]
217
218 return info_dict
219
220
221 # It also uses the arte_vp_url url from the webpage to extract the information
222 class ArteTVCreativeIE(ArteTVPlus7IE):
223 IE_NAME = u'arte.tv:creative'
224 _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
225
226 _TEST = {
227 u'url': u'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
228 u'file': u'050489-002.mp4',
229 u'info_dict': {
230 u'title': u'Agentur Amateur / Agence Amateur #2 : Corporate Design',
231 },
232 }
233
234
235 class ArteTVFutureIE(ArteTVPlus7IE):
236 IE_NAME = u'arte.tv:future'
237 _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
238
239 _TEST = {
240 u'url': u'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
241 u'file': u'050940-003.mp4',
242 u'info_dict': {
243 u'title': u'Les champignons au secours de la planète',
244 },
245 }
246
247 def _real_extract(self, url):
248 anchor_id, lang = self._extract_url_info(url)
249 webpage = self._download_webpage(url, anchor_id)
250 row = get_element_by_id(anchor_id, webpage)
251 return self._extract_from_webpage(row, anchor_id, lang)