]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/arte.py
d/p/skip-style-checks-for-flake8: Add metadata to please lintian.
[youtubedl] / youtube_dl / extractor / arte.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 qualities,
12 try_get,
13 unified_strdate,
14 )
15
16 # There are different sources of video in arte.tv, the extraction process
17 # is different for each one. The videos usually expire in 7 days, so we can't
18 # add tests.
19
20
21 class ArteTVBaseIE(InfoExtractor):
22 def _extract_from_json_url(self, json_url, video_id, lang, title=None):
23 info = self._download_json(json_url, video_id)
24 player_info = info['videoJsonPlayer']
25
26 vsr = try_get(player_info, lambda x: x['VSR'], dict)
27 if not vsr:
28 error = None
29 if try_get(player_info, lambda x: x['custom_msg']['type']) == 'error':
30 error = try_get(
31 player_info, lambda x: x['custom_msg']['msg'], compat_str)
32 if not error:
33 error = 'Video %s is not available' % player_info.get('VID') or video_id
34 raise ExtractorError(error, expected=True)
35
36 upload_date_str = player_info.get('shootingDate')
37 if not upload_date_str:
38 upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
39
40 title = (player_info.get('VTI') or title or player_info['VID']).strip()
41 subtitle = player_info.get('VSU', '').strip()
42 if subtitle:
43 title += ' - %s' % subtitle
44
45 info_dict = {
46 'id': player_info['VID'],
47 'title': title,
48 'description': player_info.get('VDE'),
49 'upload_date': unified_strdate(upload_date_str),
50 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
51 }
52 qfunc = qualities(['MQ', 'HQ', 'EQ', 'SQ'])
53
54 LANGS = {
55 'fr': 'F',
56 'de': 'A',
57 'en': 'E[ANG]',
58 'es': 'E[ESP]',
59 'it': 'E[ITA]',
60 'pl': 'E[POL]',
61 }
62
63 langcode = LANGS.get(lang, lang)
64
65 formats = []
66 for format_id, format_dict in vsr.items():
67 f = dict(format_dict)
68 versionCode = f.get('versionCode')
69 l = re.escape(langcode)
70
71 # Language preference from most to least priority
72 # Reference: section 6.8 of
73 # https://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-07-1.pdf
74 PREFERENCES = (
75 # original version in requested language, without subtitles
76 r'VO{0}$'.format(l),
77 # original version in requested language, with partial subtitles in requested language
78 r'VO{0}-ST{0}$'.format(l),
79 # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
80 r'VO{0}-STM{0}$'.format(l),
81 # non-original (dubbed) version in requested language, without subtitles
82 r'V{0}$'.format(l),
83 # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language
84 r'V{0}-ST{0}$'.format(l),
85 # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
86 r'V{0}-STM{0}$'.format(l),
87 # original version in requested language, with partial subtitles in different language
88 r'VO{0}-ST(?!{0}).+?$'.format(l),
89 # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language
90 r'VO{0}-STM(?!{0}).+?$'.format(l),
91 # original version in different language, with partial subtitles in requested language
92 r'VO(?:(?!{0}).+?)?-ST{0}$'.format(l),
93 # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language
94 r'VO(?:(?!{0}).+?)?-STM{0}$'.format(l),
95 # original version in different language, without subtitles
96 r'VO(?:(?!{0}))?$'.format(l),
97 # original version in different language, with partial subtitles in different language
98 r'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l),
99 # original version in different language, with subtitles for the deaf and hard-of-hearing in different language
100 r'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l),
101 )
102
103 for pref, p in enumerate(PREFERENCES):
104 if re.match(p, versionCode):
105 lang_pref = len(PREFERENCES) - pref
106 break
107 else:
108 lang_pref = -1
109
110 format = {
111 'format_id': format_id,
112 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
113 'language_preference': lang_pref,
114 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
115 'width': int_or_none(f.get('width')),
116 'height': int_or_none(f.get('height')),
117 'tbr': int_or_none(f.get('bitrate')),
118 'quality': qfunc(f.get('quality')),
119 }
120
121 if f.get('mediaType') == 'rtmp':
122 format['url'] = f['streamer']
123 format['play_path'] = 'mp4:' + f['url']
124 format['ext'] = 'flv'
125 else:
126 format['url'] = f['url']
127
128 formats.append(format)
129
130 self._check_formats(formats, video_id)
131 self._sort_formats(formats)
132
133 info_dict['formats'] = formats
134 return info_dict
135
136
137 class ArteTVPlus7IE(ArteTVBaseIE):
138 IE_NAME = 'arte.tv:+7'
139 _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>fr|de|en|es|it|pl)/videos/(?P<id>\d{6}-\d{3}-[AF])'
140
141 _TESTS = [{
142 'url': 'https://www.arte.tv/en/videos/088501-000-A/mexico-stealing-petrol-to-survive/',
143 'info_dict': {
144 'id': '088501-000-A',
145 'ext': 'mp4',
146 'title': 'Mexico: Stealing Petrol to Survive',
147 'upload_date': '20190628',
148 },
149 }]
150
151 def _real_extract(self, url):
152 lang, video_id = re.match(self._VALID_URL, url).groups()
153 return self._extract_from_json_url(
154 'https://api.arte.tv/api/player/v1/config/%s/%s' % (lang, video_id),
155 video_id, lang)
156
157
158 class ArteTVEmbedIE(ArteTVPlus7IE):
159 IE_NAME = 'arte.tv:embed'
160 _VALID_URL = r'''(?x)
161 https://www\.arte\.tv
162 /player/v3/index\.php\?json_url=
163 (?P<json_url>
164 https?://api\.arte\.tv/api/player/v1/config/
165 (?P<lang>[^/]+)/(?P<id>\d{6}-\d{3}-[AF])
166 )
167 '''
168
169 _TESTS = []
170
171 def _real_extract(self, url):
172 json_url, lang, video_id = re.match(self._VALID_URL, url).groups()
173 return self._extract_from_json_url(json_url, video_id, lang)
174
175
176 class ArteTVPlaylistIE(ArteTVBaseIE):
177 IE_NAME = 'arte.tv:playlist'
178 _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>fr|de|en|es|it|pl)/videos/(?P<id>RC-\d{6})'
179
180 _TESTS = [{
181 'url': 'https://www.arte.tv/en/videos/RC-016954/earn-a-living/',
182 'info_dict': {
183 'id': 'RC-016954',
184 'title': 'Earn a Living',
185 'description': 'md5:d322c55011514b3a7241f7fb80d494c2',
186 },
187 'playlist_mincount': 6,
188 }]
189
190 def _real_extract(self, url):
191 lang, playlist_id = re.match(self._VALID_URL, url).groups()
192 collection = self._download_json(
193 'https://api.arte.tv/api/player/v1/collectionData/%s/%s?source=videos'
194 % (lang, playlist_id), playlist_id)
195 title = collection.get('title')
196 description = collection.get('shortDescription') or collection.get('teaserText')
197 entries = [
198 self._extract_from_json_url(
199 video['jsonUrl'], video.get('programId') or playlist_id, lang)
200 for video in collection['videos'] if video.get('jsonUrl')]
201 return self.playlist_result(entries, playlist_id, title, description)