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