]>
Commit | Line | Data |
---|---|---|
1 | import base64 | |
2 | import binascii | |
3 | import json | |
4 | import os | |
5 | import random | |
6 | import time | |
7 | ||
8 | from .common import InfoExtractor | |
9 | from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7 | |
10 | from ..compat import compat_b64decode | |
11 | from ..networking.exceptions import HTTPError | |
12 | from ..utils import ( | |
13 | ExtractorError, | |
14 | ass_subtitles_timecode, | |
15 | bytes_to_intlist, | |
16 | bytes_to_long, | |
17 | float_or_none, | |
18 | int_or_none, | |
19 | intlist_to_bytes, | |
20 | long_to_bytes, | |
21 | parse_iso8601, | |
22 | pkcs1pad, | |
23 | str_or_none, | |
24 | strip_or_none, | |
25 | try_get, | |
26 | unified_strdate, | |
27 | urlencode_postdata, | |
28 | ) | |
29 | from ..utils.traversal import traverse_obj | |
30 | ||
31 | ||
32 | class ADNBaseIE(InfoExtractor): | |
33 | IE_DESC = 'Animation Digital Network' | |
34 | _NETRC_MACHINE = 'animationdigitalnetwork' | |
35 | _BASE = 'animationdigitalnetwork.fr' | |
36 | _API_BASE_URL = f'https://gw.api.{_BASE}/' | |
37 | _PLAYER_BASE_URL = f'{_API_BASE_URL}player/' | |
38 | _HEADERS = {} | |
39 | _LOGIN_ERR_MESSAGE = 'Unable to log in' | |
40 | _RSA_KEY = (0x9B42B08905199A5CCE2026274399CA560ECB209EE9878A708B1C0812E1BB8CB5D1FB7441861147C1A1F2F3A0476DD63A9CAC20D3E983613346850AA6CB38F16DC7D720FD7D86FC6E5B3D5BBC72E14CD0BF9E869F2CEA2CCAD648F1DCE38F1FF916CEFB2D339B64AA0264372344BC775E265E8A852F88144AB0BD9AA06C1A4ABB, 65537) | |
41 | _POS_ALIGN_MAP = { | |
42 | 'start': 1, | |
43 | 'end': 3, | |
44 | } | |
45 | _LINE_ALIGN_MAP = { | |
46 | 'middle': 8, | |
47 | 'end': 4, | |
48 | } | |
49 | ||
50 | ||
51 | class ADNIE(ADNBaseIE): | |
52 | _VALID_URL = r'https?://(?:www\.)?(?:animation|anime)digitalnetwork\.(?P<lang>fr|de)/video/[^/?#]+/(?P<id>\d+)' | |
53 | _TESTS = [{ | |
54 | 'url': 'https://animationdigitalnetwork.fr/video/fruits-basket/9841-episode-1-a-ce-soir', | |
55 | 'md5': '1c9ef066ceb302c86f80c2b371615261', | |
56 | 'info_dict': { | |
57 | 'id': '9841', | |
58 | 'ext': 'mp4', | |
59 | 'title': 'Fruits Basket - Episode 1', | |
60 | 'description': 'md5:14be2f72c3c96809b0ca424b0097d336', | |
61 | 'series': 'Fruits Basket', | |
62 | 'duration': 1437, | |
63 | 'release_date': '20190405', | |
64 | 'comment_count': int, | |
65 | 'average_rating': float, | |
66 | 'season_number': 1, | |
67 | 'episode': 'À ce soir !', | |
68 | 'episode_number': 1, | |
69 | 'thumbnail': str, | |
70 | 'season': 'Season 1', | |
71 | }, | |
72 | 'skip': 'Only available in French and German speaking Europe', | |
73 | }, { | |
74 | 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites', | |
75 | 'only_matching': True, | |
76 | }, { | |
77 | 'url': 'https://animationdigitalnetwork.de/video/the-eminence-in-shadow/23550-folge-1', | |
78 | 'md5': '5c5651bf5791fa6fcd7906012b9d94e8', | |
79 | 'info_dict': { | |
80 | 'id': '23550', | |
81 | 'ext': 'mp4', | |
82 | 'episode_number': 1, | |
83 | 'duration': 1417, | |
84 | 'release_date': '20231004', | |
85 | 'series': 'The Eminence in Shadow', | |
86 | 'season_number': 2, | |
87 | 'episode': str, | |
88 | 'title': str, | |
89 | 'thumbnail': str, | |
90 | 'season': 'Season 2', | |
91 | 'comment_count': int, | |
92 | 'average_rating': float, | |
93 | 'description': str, | |
94 | }, | |
95 | # 'skip': 'Only available in French and German speaking Europe', | |
96 | }] | |
97 | ||
98 | def _get_subtitles(self, sub_url, video_id): | |
99 | if not sub_url: | |
100 | return None | |
101 | ||
102 | enc_subtitles = self._download_webpage( | |
103 | sub_url, video_id, 'Downloading subtitles location', fatal=False) or '{}' | |
104 | subtitle_location = (self._parse_json(enc_subtitles, video_id, fatal=False) or {}).get('location') | |
105 | if subtitle_location: | |
106 | enc_subtitles = self._download_webpage( | |
107 | subtitle_location, video_id, 'Downloading subtitles data', | |
108 | fatal=False, headers={'Origin': 'https://' + self._BASE}) | |
109 | if not enc_subtitles: | |
110 | return None | |
111 | ||
112 | # http://animationdigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js | |
113 | dec_subtitles = unpad_pkcs7(aes_cbc_decrypt_bytes( | |
114 | compat_b64decode(enc_subtitles[24:]), | |
115 | binascii.unhexlify(self._K + '7fac1178830cfe0c'), | |
116 | compat_b64decode(enc_subtitles[:24]))) | |
117 | subtitles_json = self._parse_json(dec_subtitles.decode(), None, fatal=False) | |
118 | if not subtitles_json: | |
119 | return None | |
120 | ||
121 | subtitles = {} | |
122 | for sub_lang, sub in subtitles_json.items(): | |
123 | ssa = '''[Script Info] | |
124 | ScriptType:V4.00 | |
125 | [V4 Styles] | |
126 | Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,TertiaryColour,BackColour,Bold,Italic,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,AlphaLevel,Encoding | |
127 | Style: Default,Arial,18,16777215,16777215,16777215,0,-1,0,1,1,0,2,20,20,20,0,0 | |
128 | [Events] | |
129 | Format: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text''' | |
130 | for current in sub: | |
131 | start, end, text, line_align, position_align = ( | |
132 | float_or_none(current.get('startTime')), | |
133 | float_or_none(current.get('endTime')), | |
134 | current.get('text'), current.get('lineAlign'), | |
135 | current.get('positionAlign')) | |
136 | if start is None or end is None or text is None: | |
137 | continue | |
138 | alignment = self._POS_ALIGN_MAP.get(position_align, 2) + self._LINE_ALIGN_MAP.get(line_align, 0) | |
139 | ssa += os.linesep + 'Dialogue: Marked=0,%s,%s,Default,,0,0,0,,%s%s' % ( | |
140 | ass_subtitles_timecode(start), | |
141 | ass_subtitles_timecode(end), | |
142 | '{\\a%d}' % alignment if alignment != 2 else '', | |
143 | text.replace('\n', '\\N').replace('<i>', '{\\i1}').replace('</i>', '{\\i0}')) | |
144 | ||
145 | if sub_lang == 'vostf': | |
146 | sub_lang = 'fr' | |
147 | elif sub_lang == 'vostde': | |
148 | sub_lang = 'de' | |
149 | subtitles.setdefault(sub_lang, []).extend([{ | |
150 | 'ext': 'json', | |
151 | 'data': json.dumps(sub), | |
152 | }, { | |
153 | 'ext': 'ssa', | |
154 | 'data': ssa, | |
155 | }]) | |
156 | return subtitles | |
157 | ||
158 | def _perform_login(self, username, password): | |
159 | try: | |
160 | access_token = (self._download_json( | |
161 | self._API_BASE_URL + 'authentication/login', None, | |
162 | 'Logging in', self._LOGIN_ERR_MESSAGE, fatal=False, | |
163 | data=urlencode_postdata({ | |
164 | 'password': password, | |
165 | 'rememberMe': False, | |
166 | 'source': 'Web', | |
167 | 'username': username, | |
168 | })) or {}).get('accessToken') | |
169 | if access_token: | |
170 | self._HEADERS = {'authorization': 'Bearer ' + access_token} | |
171 | except ExtractorError as e: | |
172 | message = None | |
173 | if isinstance(e.cause, HTTPError) and e.cause.status == 401: | |
174 | resp = self._parse_json( | |
175 | e.cause.response.read().decode(), None, fatal=False) or {} | |
176 | message = resp.get('message') or resp.get('code') | |
177 | self.report_warning(message or self._LOGIN_ERR_MESSAGE) | |
178 | ||
179 | def _real_extract(self, url): | |
180 | lang, video_id = self._match_valid_url(url).group('lang', 'id') | |
181 | video_base_url = self._PLAYER_BASE_URL + 'video/%s/' % video_id | |
182 | player = self._download_json( | |
183 | video_base_url + 'configuration', video_id, | |
184 | 'Downloading player config JSON metadata', | |
185 | headers=self._HEADERS)['player'] | |
186 | options = player['options'] | |
187 | ||
188 | user = options['user'] | |
189 | if not user.get('hasAccess'): | |
190 | start_date = traverse_obj(options, ('video', 'startDate', {str})) | |
191 | if (parse_iso8601(start_date) or 0) > time.time(): | |
192 | raise ExtractorError(f'This video is not available yet. Release date: {start_date}', expected=True) | |
193 | self.raise_login_required('This video requires a subscription', method='password') | |
194 | ||
195 | token = self._download_json( | |
196 | user.get('refreshTokenUrl') or (self._PLAYER_BASE_URL + 'refresh/token'), | |
197 | video_id, 'Downloading access token', headers={ | |
198 | 'X-Player-Refresh-Token': user['refreshToken'], | |
199 | }, data=b'')['token'] | |
200 | ||
201 | links_url = try_get(options, lambda x: x['video']['url']) or (video_base_url + 'link') | |
202 | self._K = ''.join(random.choices('0123456789abcdef', k=16)) | |
203 | message = bytes_to_intlist(json.dumps({ | |
204 | 'k': self._K, | |
205 | 't': token, | |
206 | })) | |
207 | ||
208 | # Sometimes authentication fails for no good reason, retry with | |
209 | # a different random padding | |
210 | links_data = None | |
211 | for _ in range(3): | |
212 | padded_message = intlist_to_bytes(pkcs1pad(message, 128)) | |
213 | n, e = self._RSA_KEY | |
214 | encrypted_message = long_to_bytes(pow(bytes_to_long(padded_message), e, n)) | |
215 | authorization = base64.b64encode(encrypted_message).decode() | |
216 | ||
217 | try: | |
218 | links_data = self._download_json( | |
219 | links_url, video_id, 'Downloading links JSON metadata', headers={ | |
220 | 'X-Player-Token': authorization, | |
221 | 'X-Target-Distribution': lang, | |
222 | **self._HEADERS | |
223 | }, query={ | |
224 | 'freeWithAds': 'true', | |
225 | 'adaptive': 'false', | |
226 | 'withMetadata': 'true', | |
227 | 'source': 'Web' | |
228 | }) | |
229 | break | |
230 | except ExtractorError as e: | |
231 | if not isinstance(e.cause, HTTPError): | |
232 | raise e | |
233 | ||
234 | if e.cause.status == 401: | |
235 | # This usually goes away with a different random pkcs1pad, so retry | |
236 | continue | |
237 | ||
238 | error = self._parse_json(e.cause.response.read(), video_id) | |
239 | message = error.get('message') | |
240 | if e.cause.code == 403 and error.get('code') == 'player-bad-geolocation-country': | |
241 | self.raise_geo_restricted(msg=message) | |
242 | raise ExtractorError(message) | |
243 | else: | |
244 | raise ExtractorError('Giving up retrying') | |
245 | ||
246 | links = links_data.get('links') or {} | |
247 | metas = links_data.get('metadata') or {} | |
248 | sub_url = (links.get('subtitles') or {}).get('all') | |
249 | video_info = links_data.get('video') or {} | |
250 | title = metas['title'] | |
251 | ||
252 | formats = [] | |
253 | for format_id, qualities in (links.get('streaming') or {}).items(): | |
254 | if not isinstance(qualities, dict): | |
255 | continue | |
256 | for quality, load_balancer_url in qualities.items(): | |
257 | load_balancer_data = self._download_json( | |
258 | load_balancer_url, video_id, | |
259 | 'Downloading %s %s JSON metadata' % (format_id, quality), | |
260 | fatal=False) or {} | |
261 | m3u8_url = load_balancer_data.get('location') | |
262 | if not m3u8_url: | |
263 | continue | |
264 | m3u8_formats = self._extract_m3u8_formats( | |
265 | m3u8_url, video_id, 'mp4', 'm3u8_native', | |
266 | m3u8_id=format_id, fatal=False) | |
267 | if format_id == 'vf': | |
268 | for f in m3u8_formats: | |
269 | f['language'] = 'fr' | |
270 | elif format_id == 'vde': | |
271 | for f in m3u8_formats: | |
272 | f['language'] = 'de' | |
273 | formats.extend(m3u8_formats) | |
274 | ||
275 | if not formats: | |
276 | self.raise_login_required('This video requires a subscription', method='password') | |
277 | ||
278 | video = (self._download_json( | |
279 | self._API_BASE_URL + 'video/%s' % video_id, video_id, | |
280 | 'Downloading additional video metadata', fatal=False) or {}).get('video') or {} | |
281 | show = video.get('show') or {} | |
282 | ||
283 | return { | |
284 | 'id': video_id, | |
285 | 'title': title, | |
286 | 'description': strip_or_none(metas.get('summary') or video.get('summary')), | |
287 | 'thumbnail': video_info.get('image') or player.get('image'), | |
288 | 'formats': formats, | |
289 | 'subtitles': self.extract_subtitles(sub_url, video_id), | |
290 | 'episode': metas.get('subtitle') or video.get('name'), | |
291 | 'episode_number': int_or_none(video.get('shortNumber')), | |
292 | 'series': show.get('title'), | |
293 | 'season_number': int_or_none(video.get('season')), | |
294 | 'duration': int_or_none(video_info.get('duration') or video.get('duration')), | |
295 | 'release_date': unified_strdate(video.get('releaseDate')), | |
296 | 'average_rating': float_or_none(video.get('rating') or metas.get('rating')), | |
297 | 'comment_count': int_or_none(video.get('commentsCount')), | |
298 | } | |
299 | ||
300 | ||
301 | class ADNSeasonIE(ADNBaseIE): | |
302 | _VALID_URL = r'https?://(?:www\.)?(?:animation|anime)digitalnetwork\.(?P<lang>fr|de)/video/(?P<id>[^/?#]+)/?(?:$|[#?])' | |
303 | _TESTS = [{ | |
304 | 'url': 'https://animationdigitalnetwork.fr/video/tokyo-mew-mew-new', | |
305 | 'playlist_count': 12, | |
306 | 'info_dict': { | |
307 | 'id': '911', | |
308 | 'title': 'Tokyo Mew Mew New', | |
309 | }, | |
310 | # 'skip': 'Only available in French end German speaking Europe', | |
311 | }] | |
312 | ||
313 | def _real_extract(self, url): | |
314 | lang, video_show_slug = self._match_valid_url(url).group('lang', 'id') | |
315 | show = self._download_json( | |
316 | f'{self._API_BASE_URL}show/{video_show_slug}/', video_show_slug, | |
317 | 'Downloading show JSON metadata', headers=self._HEADERS)['show'] | |
318 | show_id = str(show['id']) | |
319 | episodes = self._download_json( | |
320 | f'{self._API_BASE_URL}video/show/{show_id}', video_show_slug, | |
321 | 'Downloading episode list', headers={ | |
322 | 'X-Target-Distribution': lang, | |
323 | **self._HEADERS | |
324 | }, query={ | |
325 | 'order': 'asc', | |
326 | 'limit': '-1', | |
327 | }) | |
328 | ||
329 | def entries(): | |
330 | for episode_id in traverse_obj(episodes, ('videos', ..., 'id', {str_or_none})): | |
331 | yield self.url_result( | |
332 | f'https://animationdigitalnetwork.{lang}/video/{video_show_slug}/{episode_id}', | |
333 | ADNIE, episode_id) | |
334 | ||
335 | return self.playlist_result(entries(), show_id, show.get('title')) |