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