[arte] Remove liveweb support
[youtube-dl] / youtube_dl / extractor / arte.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     find_xpath_attr,
11     unified_strdate,
12     determine_ext,
13     get_element_by_id,
14     compat_str,
15     get_element_by_attribute,
16 )
17
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
22
23 class ArteTvIE(InfoExtractor):
24     _VALID_URL = r'(?:http://)?videos\.arte\.tv/(?P<lang>fr|de)/.*-(?P<id>.*?)\.html'
25     IE_NAME = 'arte.tv'
26
27     def _real_extract(self, url):
28         ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
29         ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
30         ref_xml_doc = self._download_xml(
31             ref_xml_url, video_id, note='Downloading metadata')
32         config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
33         config_xml_url = config_node.attrib['ref']
34         config_xml = self._download_webpage(
35             config_xml_url, video_id, note='Downloading configuration')
36
37         video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
38         def _key(m):
39             quality = m.group('quality')
40             if quality == 'hd':
41                 return 2
42             else:
43                 return 1
44         # We pick the best quality
45         video_urls = sorted(video_urls, key=_key)
46         video_url = list(video_urls)[-1].group('url')
47         
48         title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
49         thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
50                                             config_xml, 'thumbnail')
51         return {'id': video_id,
52                 'title': title,
53                 'thumbnail': thumbnail,
54                 'url': video_url,
55                 'ext': 'flv',
56         }
57
58
59 class ArteTVPlus7IE(InfoExtractor):
60     IE_NAME = 'arte.tv:+7'
61     _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
62
63     @classmethod
64     def _extract_url_info(cls, url):
65         mobj = re.match(cls._VALID_URL, url)
66         lang = mobj.group('lang')
67         # This is not a real id, it can be for example AJT for the news
68         # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
69         video_id = mobj.group('id')
70         return video_id, lang
71
72     def _real_extract(self, url):
73         video_id, lang = self._extract_url_info(url)
74         webpage = self._download_webpage(url, video_id)
75         return self._extract_from_webpage(webpage, video_id, lang)
76
77     def _extract_from_webpage(self, webpage, video_id, lang):
78         json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json 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         json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
83         self.report_extraction(video_id)
84         info = json.loads(json_info)
85         player_info = info['videoJsonPlayer']
86
87         info_dict = {
88             'id': player_info['VID'],
89             'title': player_info['VTI'],
90             'description': player_info.get('VDE'),
91             'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
92             'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
93         }
94
95         all_formats = player_info['VSR'].values()
96         # Some formats use the m3u8 protocol
97         all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
98         def _match_lang(f):
99             if f.get('versionCode') is None:
100                 return True
101             # Return true if that format is in the language of the url
102             if lang == 'fr':
103                 l = 'F'
104             elif lang == 'de':
105                 l = 'A'
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                 return (
125                     # Sort first by quality
126                     int(f.get('height',-1)),
127                     int(f.get('bitrate',-1)),
128                     # The original version with subtitles has lower relevance
129                     re.match(r'VO-ST(F|A)', f.get('versionCode', '')) is None,
130                     # The version with sourds/mal subtitles has also lower relevance
131                     re.match(r'VO?(F|A)-STM\1', f.get('versionCode', '')) is None,
132                     # Prefer http downloads over m3u8
133                     0 if f['url'].endswith('m3u8') else 1,
134                 )
135         formats = sorted(formats, key=sort_key)
136         def _format(format_info):
137             quality = ''
138             height = format_info.get('height')
139             if height is not None:
140                 quality = compat_str(height)
141             bitrate = format_info.get('bitrate')
142             if bitrate is not None:
143                 quality += '-%d' % bitrate
144             if format_info.get('versionCode') is not None:
145                 format_id = '%s-%s' % (quality, format_info['versionCode'])
146             else:
147                 format_id = quality
148             info = {
149                 'format_id': format_id,
150                 'format_note': format_info.get('versionLibelle'),
151                 'width': format_info.get('width'),
152                 'height': height,
153             }
154             if format_info['mediaType'] == 'rtmp':
155                 info['url'] = format_info['streamer']
156                 info['play_path'] = 'mp4:' + format_info['url']
157                 info['ext'] = 'flv'
158             else:
159                 info['url'] = format_info['url']
160                 info['ext'] = determine_ext(info['url'])
161             return info
162         info_dict['formats'] = [_format(f) for f in formats]
163
164         return info_dict
165
166
167 # It also uses the arte_vp_url url from the webpage to extract the information
168 class ArteTVCreativeIE(ArteTVPlus7IE):
169     IE_NAME = 'arte.tv:creative'
170     _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
171
172     _TEST = {
173         'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
174         'info_dict': {
175             'id': '050489-002',
176             'ext': 'mp4',
177             'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
178         },
179     }
180
181
182 class ArteTVFutureIE(ArteTVPlus7IE):
183     IE_NAME = 'arte.tv:future'
184     _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
185
186     _TEST = {
187         'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
188         'info_dict': {
189             'id': '050940-003',
190             'ext': 'mp4',
191             'title': 'Les champignons au secours de la planète',
192         },
193     }
194
195     def _real_extract(self, url):
196         anchor_id, lang = self._extract_url_info(url)
197         webpage = self._download_webpage(url, anchor_id)
198         row = get_element_by_id(anchor_id, webpage)
199         return self._extract_from_webpage(row, anchor_id, lang)
200
201
202 class ArteTVDDCIE(ArteTVPlus7IE):
203     IE_NAME = 'arte.tv:ddc'
204     _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
205
206     def _real_extract(self, url):
207         video_id, lang = self._extract_url_info(url)
208         if lang == 'folge':
209             lang = 'de'
210         elif lang == 'emission':
211             lang = 'fr'
212         webpage = self._download_webpage(url, video_id)
213         scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
214         script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
215         javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
216         json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
217         return self._extract_from_json_url(json_url, video_id, lang)
218
219
220 class ArteTVConcertIE(ArteTVPlus7IE):
221     IE_NAME = 'arte.tv:concert'
222     _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
223
224     _TEST = {
225         'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
226         'md5': '9ea035b7bd69696b67aa2ccaaa218161',
227         'info_dict': {
228             'id': '186',
229             'ext': 'mp4',
230             'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
231             'upload_date': '20140128',
232             'description': 'md5:486eb08f991552ade77439fe6d82c305',
233         },
234     }