]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/funimation.py
[extractor] Add `_perform_login` function (#2943)
[yt-dlp.git] / yt_dlp / extractor / funimation.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import random
5 import re
6 import string
7
8 from .common import InfoExtractor
9 from ..compat import compat_HTTPError
10 from ..utils import (
11 determine_ext,
12 int_or_none,
13 join_nonempty,
14 js_to_json,
15 orderedSet,
16 qualities,
17 str_or_none,
18 traverse_obj,
19 try_get,
20 urlencode_postdata,
21 ExtractorError,
22 )
23
24
25 class FunimationBaseIE(InfoExtractor):
26 _NETRC_MACHINE = 'funimation'
27 _REGION = None
28 _TOKEN = None
29
30 def _get_region(self):
31 region_cookie = self._get_cookies('https://www.funimation.com').get('region')
32 region = region_cookie.value if region_cookie else self.get_param('geo_bypass_country')
33 return region or traverse_obj(
34 self._download_json(
35 'https://geo-service.prd.funimationsvc.com/geo/v1/region/check', None, fatal=False,
36 note='Checking geo-location', errnote='Unable to fetch geo-location information'),
37 'region') or 'US'
38
39 def _perform_login(self, username, password):
40 if self._TOKEN:
41 return
42 try:
43 data = self._download_json(
44 'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/',
45 None, 'Logging in', data=urlencode_postdata({
46 'username': username,
47 'password': password,
48 }))
49 FunimationBaseIE._TOKEN = data['token']
50 except ExtractorError as e:
51 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
52 error = self._parse_json(e.cause.read().decode(), None)['error']
53 raise ExtractorError(error, expected=True)
54 raise
55
56
57 class FunimationPageIE(FunimationBaseIE):
58 IE_NAME = 'funimation:page'
59 _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?:(?P<lang>[^/]+)/)?(?:shows|v)/(?P<show>[^/]+)/(?P<episode>[^/?#&]+)'
60
61 _TESTS = [{
62 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
63 'info_dict': {
64 'id': '210050',
65 'ext': 'mp4',
66 'title': 'Broadcast Dub Preview',
67 # Other metadata is tested in FunimationIE
68 },
69 'params': {
70 'skip_download': 'm3u8',
71 },
72 'add_ie': ['Funimation'],
73 }, {
74 # Not available in US
75 'url': 'https://www.funimation.com/shows/hacksign/role-play/',
76 'only_matching': True,
77 }, {
78 # with lang code
79 'url': 'https://www.funimation.com/en/shows/hacksign/role-play/',
80 'only_matching': True,
81 }, {
82 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
83 'only_matching': True,
84 }, {
85 'url': 'https://www.funimation.com/v/a-certain-scientific-railgun/super-powered-level-5',
86 'only_matching': True,
87 }]
88
89 def _real_initialize(self):
90 if not self._REGION:
91 FunimationBaseIE._REGION = self._get_region()
92
93 def _real_extract(self, url):
94 locale, show, episode = self._match_valid_url(url).group('lang', 'show', 'episode')
95
96 video_id = traverse_obj(self._download_json(
97 f'https://title-api.prd.funimationsvc.com/v1/shows/{show}/episodes/{episode}',
98 f'{show}_{episode}', query={
99 'deviceType': 'web',
100 'region': self._REGION,
101 'locale': locale or 'en'
102 }), ('videoList', ..., 'id'), get_all=False)
103
104 return self.url_result(f'https://www.funimation.com/player/{video_id}', FunimationIE.ie_key(), video_id)
105
106
107 class FunimationIE(FunimationBaseIE):
108 _VALID_URL = r'https?://(?:www\.)?funimation\.com/player/(?P<id>\d+)'
109
110 _TESTS = [{
111 'url': 'https://www.funimation.com/player/210051',
112 'info_dict': {
113 'id': '210050',
114 'display_id': 'broadcast-dub-preview',
115 'ext': 'mp4',
116 'title': 'Broadcast Dub Preview',
117 'thumbnail': r're:https?://.*\.(?:jpg|png)',
118 'episode': 'Broadcast Dub Preview',
119 'episode_id': '210050',
120 'season': 'Extras',
121 'season_id': '166038',
122 'season_number': 99,
123 'series': 'Attack on Titan: Junior High',
124 'description': '',
125 'duration': 155,
126 },
127 'params': {
128 'skip_download': 'm3u8',
129 },
130 }, {
131 'note': 'player_id should be extracted with the relevent compat-opt',
132 'url': 'https://www.funimation.com/player/210051',
133 'info_dict': {
134 'id': '210051',
135 'display_id': 'broadcast-dub-preview',
136 'ext': 'mp4',
137 'title': 'Broadcast Dub Preview',
138 'thumbnail': r're:https?://.*\.(?:jpg|png)',
139 'episode': 'Broadcast Dub Preview',
140 'episode_id': '210050',
141 'season': 'Extras',
142 'season_id': '166038',
143 'season_number': 99,
144 'series': 'Attack on Titan: Junior High',
145 'description': '',
146 'duration': 155,
147 },
148 'params': {
149 'skip_download': 'm3u8',
150 'compat_opts': ['seperate-video-versions'],
151 },
152 }]
153
154 @staticmethod
155 def _get_experiences(episode):
156 for lang, lang_data in episode.get('languages', {}).items():
157 for video_data in lang_data.values():
158 for version, f in video_data.items():
159 yield lang, version.title(), f
160
161 def _get_episode(self, webpage, experience_id=None, episode_id=None, fatal=True):
162 ''' Extract the episode, season and show objects given either episode/experience id '''
163 show = self._parse_json(
164 self._search_regex(
165 r'show\s*=\s*({.+?})\s*;', webpage, 'show data', fatal=fatal),
166 experience_id, transform_source=js_to_json, fatal=fatal) or []
167 for season in show.get('seasons', []):
168 for episode in season.get('episodes', []):
169 if episode_id is not None:
170 if str(episode.get('episodePk')) == episode_id:
171 return episode, season, show
172 continue
173 for _, _, f in self._get_experiences(episode):
174 if f.get('experienceId') == experience_id:
175 return episode, season, show
176 if fatal:
177 raise ExtractorError('Unable to find episode information')
178 else:
179 self.report_warning('Unable to find episode information')
180 return {}, {}, {}
181
182 def _real_extract(self, url):
183 initial_experience_id = self._match_id(url)
184 webpage = self._download_webpage(
185 url, initial_experience_id, note=f'Downloading player webpage for {initial_experience_id}')
186 episode, season, show = self._get_episode(webpage, experience_id=int(initial_experience_id))
187 episode_id = str(episode['episodePk'])
188 display_id = episode.get('slug') or episode_id
189
190 formats, subtitles, thumbnails, duration = [], {}, [], 0
191 requested_languages, requested_versions = self._configuration_arg('language'), self._configuration_arg('version')
192 language_preference = qualities((requested_languages or [''])[::-1])
193 source_preference = qualities((requested_versions or ['uncut', 'simulcast'])[::-1])
194 only_initial_experience = 'seperate-video-versions' in self.get_param('compat_opts', [])
195
196 for lang, version, fmt in self._get_experiences(episode):
197 experience_id = str(fmt['experienceId'])
198 if (only_initial_experience and experience_id != initial_experience_id
199 or requested_languages and lang.lower() not in requested_languages
200 or requested_versions and version.lower() not in requested_versions):
201 continue
202 thumbnails.append({'url': fmt.get('poster')})
203 duration = max(duration, fmt.get('duration', 0))
204 format_name = '%s %s (%s)' % (version, lang, experience_id)
205 self.extract_subtitles(
206 subtitles, experience_id, display_id=display_id, format_name=format_name,
207 episode=episode if experience_id == initial_experience_id else episode_id)
208
209 headers = {}
210 if self._TOKEN:
211 headers['Authorization'] = 'Token %s' % self._TOKEN
212 page = self._download_json(
213 'https://www.funimation.com/api/showexperience/%s/' % experience_id,
214 display_id, headers=headers, expected_status=403, query={
215 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
216 }, note=f'Downloading {format_name} JSON')
217 sources = page.get('items') or []
218 if not sources:
219 error = try_get(page, lambda x: x['errors'][0], dict)
220 if error:
221 self.report_warning('%s said: Error %s - %s' % (
222 self.IE_NAME, error.get('code'), error.get('detail') or error.get('title')))
223 else:
224 self.report_warning('No sources found for format')
225
226 current_formats = []
227 for source in sources:
228 source_url = source.get('src')
229 source_type = source.get('videoType') or determine_ext(source_url)
230 if source_type == 'm3u8':
231 current_formats.extend(self._extract_m3u8_formats(
232 source_url, display_id, 'mp4', m3u8_id='%s-%s' % (experience_id, 'hls'), fatal=False,
233 note=f'Downloading {format_name} m3u8 information'))
234 else:
235 current_formats.append({
236 'format_id': '%s-%s' % (experience_id, source_type),
237 'url': source_url,
238 })
239 for f in current_formats:
240 # TODO: Convert language to code
241 f.update({
242 'language': lang,
243 'format_note': version,
244 'source_preference': source_preference(version.lower()),
245 'language_preference': language_preference(lang.lower()),
246 })
247 formats.extend(current_formats)
248 self._remove_duplicate_formats(formats)
249 self._sort_formats(formats, ('lang', 'source'))
250
251 return {
252 'id': initial_experience_id if only_initial_experience else episode_id,
253 'display_id': display_id,
254 'duration': duration,
255 'title': episode['episodeTitle'],
256 'description': episode.get('episodeSummary'),
257 'episode': episode.get('episodeTitle'),
258 'episode_number': int_or_none(episode.get('episodeId')),
259 'episode_id': episode_id,
260 'season': season.get('seasonTitle'),
261 'season_number': int_or_none(season.get('seasonId')),
262 'season_id': str_or_none(season.get('seasonPk')),
263 'series': show.get('showTitle'),
264 'formats': formats,
265 'thumbnails': thumbnails,
266 'subtitles': subtitles,
267 }
268
269 def _get_subtitles(self, subtitles, experience_id, episode, display_id, format_name):
270 if isinstance(episode, str):
271 webpage = self._download_webpage(
272 f'https://www.funimation.com/player/{experience_id}/', display_id,
273 fatal=False, note=f'Downloading player webpage for {format_name}')
274 episode, _, _ = self._get_episode(webpage, episode_id=episode, fatal=False)
275
276 for _, version, f in self._get_experiences(episode):
277 for source in f.get('sources'):
278 for text_track in source.get('textTracks'):
279 if not text_track.get('src'):
280 continue
281 sub_type = text_track.get('type').upper()
282 sub_type = sub_type if sub_type != 'FULL' else None
283 current_sub = {
284 'url': text_track['src'],
285 'name': join_nonempty(version, text_track.get('label'), sub_type, delim=' ')
286 }
287 lang = join_nonempty(text_track.get('language', 'und'),
288 version if version != 'Simulcast' else None,
289 sub_type, delim='_')
290 if current_sub not in subtitles.get(lang, []):
291 subtitles.setdefault(lang, []).append(current_sub)
292 return subtitles
293
294
295 class FunimationShowIE(FunimationBaseIE):
296 IE_NAME = 'funimation:show'
297 _VALID_URL = r'(?P<url>https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?P<locale>[^/]+)?/?shows/(?P<id>[^/?#&]+))/?(?:[?#]|$)'
298
299 _TESTS = [{
300 'url': 'https://www.funimation.com/en/shows/sk8-the-infinity',
301 'info_dict': {
302 'id': 1315000,
303 'title': 'SK8 the Infinity'
304 },
305 'playlist_count': 13,
306 'params': {
307 'skip_download': True,
308 },
309 }, {
310 # without lang code
311 'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/',
312 'info_dict': {
313 'id': 39643,
314 'title': 'Ouran High School Host Club'
315 },
316 'playlist_count': 26,
317 'params': {
318 'skip_download': True,
319 },
320 }]
321
322 def _real_initialize(self):
323 if not self._REGION:
324 FunimationBaseIE._REGION = self._get_region()
325
326 def _real_extract(self, url):
327 base_url, locale, display_id = self._match_valid_url(url).groups()
328
329 show_info = self._download_json(
330 'https://title-api.prd.funimationsvc.com/v2/shows/%s?region=%s&deviceType=web&locale=%s'
331 % (display_id, self._REGION, locale or 'en'), display_id)
332 items_info = self._download_json(
333 'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s'
334 % show_info.get('id'), display_id)
335
336 vod_items = traverse_obj(items_info, ('items', ..., re.compile('(?i)mostRecent[AS]vod').match, 'item'))
337
338 return {
339 '_type': 'playlist',
340 'id': show_info['id'],
341 'title': show_info['name'],
342 'entries': orderedSet(
343 self.url_result(
344 '%s/%s' % (base_url, vod_item.get('episodeSlug')), FunimationPageIE.ie_key(),
345 vod_item.get('episodeId'), vod_item.get('episodeName'))
346 for vod_item in sorted(vod_items, key=lambda x: x.get('episodeOrder', -1))),
347 }