]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
3798eadc PH |
2 | from __future__ import unicode_literals |
3 | ||
d5822b96 | 4 | import re |
d5822b96 PH |
5 | |
6 | from .common import InfoExtractor | |
8bdd16b4 | 7 | from ..compat import ( |
8 | compat_str, | |
9 | compat_urlparse, | |
10 | ) | |
d5822b96 | 11 | from ..utils import ( |
c0892b2b | 12 | ExtractorError, |
d24a2b20 | 13 | int_or_none, |
aff2f4f4 | 14 | qualities, |
8cc1840c | 15 | try_get, |
c0892b2b | 16 | unified_strdate, |
8bdd16b4 | 17 | url_or_none, |
d5822b96 PH |
18 | ) |
19 | ||
d5822b96 | 20 | |
6e6b9f60 | 21 | class ArteTVBaseIE(InfoExtractor): |
8bdd16b4 | 22 | _ARTE_LANGUAGES = 'fr|de|en|es|it|pl' |
23 | _API_BASE = 'https://api.arte.tv/api/player/v1' | |
24 | ||
25 | ||
26 | class ArteTVIE(ArteTVBaseIE): | |
27 | _VALID_URL = r'''(?x) | |
28 | https?:// | |
29 | (?: | |
30 | (?:www\.)?arte\.tv/(?P<lang>%(langs)s)/videos| | |
31 | api\.arte\.tv/api/player/v\d+/config/(?P<lang_2>%(langs)s) | |
32 | ) | |
33 | /(?P<id>\d{6}-\d{3}-[AF]) | |
34 | ''' % {'langs': ArteTVBaseIE._ARTE_LANGUAGES} | |
35 | _TESTS = [{ | |
36 | 'url': 'https://www.arte.tv/en/videos/088501-000-A/mexico-stealing-petrol-to-survive/', | |
37 | 'info_dict': { | |
38 | 'id': '088501-000-A', | |
39 | 'ext': 'mp4', | |
40 | 'title': 'Mexico: Stealing Petrol to Survive', | |
41 | 'upload_date': '20190628', | |
42 | }, | |
43 | }, { | |
44 | 'url': 'https://www.arte.tv/pl/videos/100103-000-A/usa-dyskryminacja-na-porodowce/', | |
45 | 'only_matching': True, | |
46 | }, { | |
47 | 'url': 'https://api.arte.tv/api/player/v2/config/de/100605-013-A', | |
48 | 'only_matching': True, | |
49 | }] | |
50 | ||
51 | def _real_extract(self, url): | |
52 | mobj = re.match(self._VALID_URL, url) | |
53 | video_id = mobj.group('id') | |
54 | lang = mobj.group('lang') or mobj.group('lang_2') | |
55 | ||
56 | info = self._download_json( | |
57 | '%s/config/%s/%s' % (self._API_BASE, lang, video_id), video_id) | |
c40f5cf4 JMF |
58 | player_info = info['videoJsonPlayer'] |
59 | ||
8cc1840c | 60 | vsr = try_get(player_info, lambda x: x['VSR'], dict) |
6348671c | 61 | if not vsr: |
8cc1840c S |
62 | error = None |
63 | if try_get(player_info, lambda x: x['custom_msg']['type']) == 'error': | |
64 | error = try_get( | |
65 | player_info, lambda x: x['custom_msg']['msg'], compat_str) | |
66 | if not error: | |
67 | error = 'Video %s is not available' % player_info.get('VID') or video_id | |
68 | raise ExtractorError(error, expected=True) | |
c0892b2b | 69 | |
99b67fec PH |
70 | upload_date_str = player_info.get('shootingDate') |
71 | if not upload_date_str: | |
8bbd3d14 | 72 | upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0] |
99b67fec | 73 | |
8bdd16b4 | 74 | title = (player_info.get('VTI') or player_info['VID']).strip() |
74214d35 S |
75 | subtitle = player_info.get('VSU', '').strip() |
76 | if subtitle: | |
77 | title += ' - %s' % subtitle | |
78 | ||
ff0f4cfe | 79 | qfunc = qualities(['MQ', 'HQ', 'EQ', 'SQ']) |
c40f5cf4 | 80 | |
08d65046 S |
81 | LANGS = { |
82 | 'fr': 'F', | |
83 | 'de': 'A', | |
84 | 'en': 'E[ANG]', | |
85 | 'es': 'E[ESP]', | |
ff0f4cfe RA |
86 | 'it': 'E[ITA]', |
87 | 'pl': 'E[POL]', | |
08d65046 S |
88 | } |
89 | ||
9c072d38 S |
90 | langcode = LANGS.get(lang, lang) |
91 | ||
aff2f4f4 | 92 | formats = [] |
c0892b2b | 93 | for format_id, format_dict in vsr.items(): |
aff2f4f4 | 94 | f = dict(format_dict) |
8bdd16b4 | 95 | format_url = url_or_none(f.get('url')) |
96 | streamer = f.get('streamer') | |
97 | if not format_url and not streamer: | |
98 | continue | |
aff2f4f4 | 99 | versionCode = f.get('versionCode') |
9c072d38 S |
100 | l = re.escape(langcode) |
101 | ||
102 | # Language preference from most to least priority | |
ff0f4cfe RA |
103 | # Reference: section 6.8 of |
104 | # https://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-07-1.pdf | |
9c072d38 S |
105 | PREFERENCES = ( |
106 | # original version in requested language, without subtitles | |
107 | r'VO{0}$'.format(l), | |
108 | # original version in requested language, with partial subtitles in requested language | |
109 | r'VO{0}-ST{0}$'.format(l), | |
110 | # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language | |
111 | r'VO{0}-STM{0}$'.format(l), | |
112 | # non-original (dubbed) version in requested language, without subtitles | |
113 | r'V{0}$'.format(l), | |
114 | # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language | |
115 | r'V{0}-ST{0}$'.format(l), | |
116 | # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language | |
117 | r'V{0}-STM{0}$'.format(l), | |
118 | # original version in requested language, with partial subtitles in different language | |
119 | r'VO{0}-ST(?!{0}).+?$'.format(l), | |
120 | # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language | |
121 | r'VO{0}-STM(?!{0}).+?$'.format(l), | |
122 | # original version in different language, with partial subtitles in requested language | |
123 | r'VO(?:(?!{0}).+?)?-ST{0}$'.format(l), | |
124 | # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language | |
125 | r'VO(?:(?!{0}).+?)?-STM{0}$'.format(l), | |
126 | # original version in different language, without subtitles | |
127 | r'VO(?:(?!{0}))?$'.format(l), | |
128 | # original version in different language, with partial subtitles in different language | |
129 | r'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l), | |
130 | # original version in different language, with subtitles for the deaf and hard-of-hearing in different language | |
131 | r'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l), | |
132 | ) | |
133 | ||
134 | for pref, p in enumerate(PREFERENCES): | |
135 | if re.match(p, versionCode): | |
136 | lang_pref = len(PREFERENCES) - pref | |
137 | break | |
138 | else: | |
139 | lang_pref = -1 | |
140 | ||
8bdd16b4 | 141 | media_type = f.get('mediaType') |
142 | if media_type == 'hls': | |
143 | m3u8_formats = self._extract_m3u8_formats( | |
144 | format_url, video_id, 'mp4', entry_protocol='m3u8_native', | |
145 | m3u8_id=format_id, fatal=False) | |
146 | for m3u8_format in m3u8_formats: | |
147 | m3u8_format['language_preference'] = lang_pref | |
148 | formats.extend(m3u8_formats) | |
149 | continue | |
150 | ||
aff2f4f4 PH |
151 | format = { |
152 | 'format_id': format_id, | |
153 | 'preference': -10 if f.get('videoFormat') == 'M3U8' else None, | |
154 | 'language_preference': lang_pref, | |
155 | 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')), | |
156 | 'width': int_or_none(f.get('width')), | |
157 | 'height': int_or_none(f.get('height')), | |
158 | 'tbr': int_or_none(f.get('bitrate')), | |
1b7b1d6e | 159 | 'quality': qfunc(f.get('quality')), |
c40f5cf4 | 160 | } |
aff2f4f4 | 161 | |
8bdd16b4 | 162 | if media_type == 'rtmp': |
aff2f4f4 PH |
163 | format['url'] = f['streamer'] |
164 | format['play_path'] = 'mp4:' + f['url'] | |
165 | format['ext'] = 'flv' | |
c40f5cf4 | 166 | else: |
aff2f4f4 PH |
167 | format['url'] = f['url'] |
168 | ||
169 | formats.append(format) | |
170 | ||
dca3ff4a | 171 | # For this extractor, quality only represents the relative quality |
172 | # with respect to other formats with the same resolution | |
173 | self._sort_formats(formats, ('res', 'quality')) | |
c40f5cf4 | 174 | |
8bdd16b4 | 175 | return { |
176 | 'id': player_info.get('VID') or video_id, | |
177 | 'title': title, | |
178 | 'description': player_info.get('VDE'), | |
179 | 'upload_date': unified_strdate(upload_date_str), | |
180 | 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'), | |
181 | 'formats': formats, | |
182 | } | |
c40f5cf4 | 183 | |
24114fee | 184 | |
8bdd16b4 | 185 | class ArteTVEmbedIE(InfoExtractor): |
186 | _VALID_URL = r'https?://(?:www\.)?arte\.tv/player/v\d+/index\.php\?.*?\bjson_url=.+' | |
9c54ae33 | 187 | _TESTS = [{ |
8bdd16b4 | 188 | 'url': 'https://www.arte.tv/player/v5/index.php?json_url=https%3A%2F%2Fapi.arte.tv%2Fapi%2Fplayer%2Fv2%2Fconfig%2Fde%2F100605-013-A&lang=de&autoplay=true&mute=0100605-013-A', |
9c54ae33 | 189 | 'info_dict': { |
8bdd16b4 | 190 | 'id': '100605-013-A', |
9c54ae33 | 191 | 'ext': 'mp4', |
8bdd16b4 | 192 | 'title': 'United we Stream November Lockdown Edition #13', |
193 | 'description': 'md5:be40b667f45189632b78c1425c7c2ce1', | |
194 | 'upload_date': '20201116', | |
69a0c470 | 195 | }, |
8bdd16b4 | 196 | }, { |
197 | 'url': 'https://www.arte.tv/player/v3/index.php?json_url=https://api.arte.tv/api/player/v2/config/de/100605-013-A', | |
198 | 'only_matching': True, | |
9c54ae33 | 199 | }] |
56a8ab7d | 200 | |
8bdd16b4 | 201 | @staticmethod |
202 | def _extract_urls(webpage): | |
203 | return [url for _, url in re.findall( | |
204 | r'<(?:iframe|script)[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?arte\.tv/player/v\d+/index\.php\?.*?\bjson_url=.+?)\1', | |
205 | webpage)] | |
6e6b9f60 | 206 | |
893f8832 | 207 | def _real_extract(self, url): |
8bdd16b4 | 208 | qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) |
209 | json_url = qs['json_url'][0] | |
210 | video_id = ArteTVIE._match_id(json_url) | |
211 | return self.url_result( | |
212 | json_url, ie=ArteTVIE.ie_key(), video_id=video_id) | |
4b492e35 S |
213 | |
214 | ||
6e6b9f60 | 215 | class ArteTVPlaylistIE(ArteTVBaseIE): |
8bdd16b4 | 216 | _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>%s)/videos/(?P<id>RC-\d{6})' % ArteTVBaseIE._ARTE_LANGUAGES |
6e6b9f60 | 217 | _TESTS = [{ |
ff0f4cfe | 218 | 'url': 'https://www.arte.tv/en/videos/RC-016954/earn-a-living/', |
6e6b9f60 | 219 | 'info_dict': { |
ff0f4cfe RA |
220 | 'id': 'RC-016954', |
221 | 'title': 'Earn a Living', | |
222 | 'description': 'md5:d322c55011514b3a7241f7fb80d494c2', | |
6e6b9f60 S |
223 | }, |
224 | 'playlist_mincount': 6, | |
8bdd16b4 | 225 | }, { |
226 | 'url': 'https://www.arte.tv/pl/videos/RC-014123/arte-reportage/', | |
227 | 'only_matching': True, | |
6e6b9f60 S |
228 | }] |
229 | ||
230 | def _real_extract(self, url): | |
ff0f4cfe | 231 | lang, playlist_id = re.match(self._VALID_URL, url).groups() |
6e6b9f60 | 232 | collection = self._download_json( |
8bdd16b4 | 233 | '%s/collectionData/%s/%s?source=videos' |
234 | % (self._API_BASE, lang, playlist_id), playlist_id) | |
235 | entries = [] | |
236 | for video in collection['videos']: | |
237 | if not isinstance(video, dict): | |
238 | continue | |
239 | video_url = url_or_none(video.get('url')) or url_or_none(video.get('jsonUrl')) | |
240 | if not video_url: | |
241 | continue | |
242 | video_id = video.get('programId') | |
243 | entries.append({ | |
244 | '_type': 'url_transparent', | |
245 | 'url': video_url, | |
246 | 'id': video_id, | |
247 | 'title': video.get('title'), | |
248 | 'alt_title': video.get('subtitle'), | |
249 | 'thumbnail': url_or_none(try_get(video, lambda x: x['mainImage']['url'], compat_str)), | |
250 | 'duration': int_or_none(video.get('durationSeconds')), | |
251 | 'view_count': int_or_none(video.get('views')), | |
252 | 'ie_key': ArteTVIE.ie_key(), | |
253 | }) | |
6e6b9f60 S |
254 | title = collection.get('title') |
255 | description = collection.get('shortDescription') or collection.get('teaserText') | |
6e6b9f60 | 256 | return self.playlist_result(entries, playlist_id, title, description) |