2019-01-15 09:23:59 +00:00
|
|
|
import random
|
2021-10-09 15:21:41 +00:00
|
|
|
import re
|
2019-01-15 09:23:59 +00:00
|
|
|
import string
|
|
|
|
|
2015-12-06 22:15:19 +00:00
|
|
|
from .common import InfoExtractor
|
2017-05-08 17:50:25 +00:00
|
|
|
from ..compat import compat_HTTPError
|
2015-12-06 22:15:19 +00:00
|
|
|
from ..utils import (
|
2022-08-01 22:10:47 +00:00
|
|
|
ExtractorError,
|
2015-12-11 17:00:22 +00:00
|
|
|
determine_ext,
|
2015-12-11 19:02:54 +00:00
|
|
|
int_or_none,
|
2021-11-06 01:05:24 +00:00
|
|
|
join_nonempty,
|
2017-04-01 12:32:38 +00:00
|
|
|
js_to_json,
|
2022-08-01 22:10:47 +00:00
|
|
|
make_archive_id,
|
2021-10-09 15:21:41 +00:00
|
|
|
orderedSet,
|
|
|
|
qualities,
|
2021-07-06 21:21:29 +00:00
|
|
|
str_or_none,
|
2021-10-09 15:21:41 +00:00
|
|
|
traverse_obj,
|
2021-07-06 21:21:29 +00:00
|
|
|
try_get,
|
2021-06-23 01:57:53 +00:00
|
|
|
urlencode_postdata,
|
2015-12-06 22:15:19 +00:00
|
|
|
)
|
|
|
|
|
2015-12-11 15:11:45 +00:00
|
|
|
|
2021-10-09 15:21:41 +00:00
|
|
|
class FunimationBaseIE(InfoExtractor):
|
|
|
|
_NETRC_MACHINE = 'funimation'
|
|
|
|
_REGION = None
|
|
|
|
_TOKEN = None
|
|
|
|
|
|
|
|
def _get_region(self):
|
|
|
|
region_cookie = self._get_cookies('https://www.funimation.com').get('region')
|
|
|
|
region = region_cookie.value if region_cookie else self.get_param('geo_bypass_country')
|
|
|
|
return region or traverse_obj(
|
|
|
|
self._download_json(
|
|
|
|
'https://geo-service.prd.funimationsvc.com/geo/v1/region/check', None, fatal=False,
|
|
|
|
note='Checking geo-location', errnote='Unable to fetch geo-location information'),
|
|
|
|
'region') or 'US'
|
|
|
|
|
2022-03-18 20:53:33 +00:00
|
|
|
def _perform_login(self, username, password):
|
|
|
|
if self._TOKEN:
|
2021-10-09 15:21:41 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
data = self._download_json(
|
|
|
|
'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/',
|
|
|
|
None, 'Logging in', data=urlencode_postdata({
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
}))
|
2022-03-18 20:53:33 +00:00
|
|
|
FunimationBaseIE._TOKEN = data['token']
|
2021-10-09 15:21:41 +00:00
|
|
|
except ExtractorError as e:
|
|
|
|
if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
|
|
|
|
error = self._parse_json(e.cause.read().decode(), None)['error']
|
|
|
|
raise ExtractorError(error, expected=True)
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
class FunimationPageIE(FunimationBaseIE):
|
2021-07-06 21:21:29 +00:00
|
|
|
IE_NAME = 'funimation:page'
|
2021-10-09 15:21:41 +00:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?:(?P<lang>[^/]+)/)?(?:shows|v)/(?P<show>[^/]+)/(?P<episode>[^/?#&]+)'
|
2021-07-06 21:21:29 +00:00
|
|
|
|
|
|
|
_TESTS = [{
|
|
|
|
'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '210050',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': 'Broadcast Dub Preview',
|
|
|
|
# Other metadata is tested in FunimationIE
|
|
|
|
},
|
|
|
|
'params': {
|
|
|
|
'skip_download': 'm3u8',
|
|
|
|
},
|
|
|
|
'add_ie': ['Funimation'],
|
|
|
|
}, {
|
|
|
|
# Not available in US
|
|
|
|
'url': 'https://www.funimation.com/shows/hacksign/role-play/',
|
|
|
|
'only_matching': True,
|
|
|
|
}, {
|
|
|
|
# with lang code
|
|
|
|
'url': 'https://www.funimation.com/en/shows/hacksign/role-play/',
|
|
|
|
'only_matching': True,
|
|
|
|
}, {
|
|
|
|
'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
|
|
|
|
'only_matching': True,
|
2021-10-09 15:21:41 +00:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.funimation.com/v/a-certain-scientific-railgun/super-powered-level-5',
|
|
|
|
'only_matching': True,
|
2021-07-06 21:21:29 +00:00
|
|
|
}]
|
|
|
|
|
2021-10-09 15:21:41 +00:00
|
|
|
def _real_initialize(self):
|
|
|
|
if not self._REGION:
|
|
|
|
FunimationBaseIE._REGION = self._get_region()
|
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
def _real_extract(self, url):
|
2021-10-09 15:21:41 +00:00
|
|
|
locale, show, episode = self._match_valid_url(url).group('lang', 'show', 'episode')
|
|
|
|
|
|
|
|
video_id = traverse_obj(self._download_json(
|
|
|
|
f'https://title-api.prd.funimationsvc.com/v1/shows/{show}/episodes/{episode}',
|
|
|
|
f'{show}_{episode}', query={
|
|
|
|
'deviceType': 'web',
|
|
|
|
'region': self._REGION,
|
|
|
|
'locale': locale or 'en'
|
|
|
|
}), ('videoList', ..., 'id'), get_all=False)
|
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
return self.url_result(f'https://www.funimation.com/player/{video_id}', FunimationIE.ie_key(), video_id)
|
|
|
|
|
|
|
|
|
2021-10-09 15:21:41 +00:00
|
|
|
class FunimationIE(FunimationBaseIE):
|
2021-07-06 21:21:29 +00:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?funimation\.com/player/(?P<id>\d+)'
|
2015-12-06 22:15:19 +00:00
|
|
|
|
2015-12-11 18:38:58 +00:00
|
|
|
_TESTS = [{
|
2021-07-06 21:21:29 +00:00
|
|
|
'url': 'https://www.funimation.com/player/210051',
|
2015-12-06 22:15:19 +00:00
|
|
|
'info_dict': {
|
2021-07-06 21:21:29 +00:00
|
|
|
'id': '210050',
|
|
|
|
'display_id': 'broadcast-dub-preview',
|
2015-12-11 18:38:58 +00:00
|
|
|
'ext': 'mp4',
|
2021-07-06 21:21:29 +00:00
|
|
|
'title': 'Broadcast Dub Preview',
|
|
|
|
'thumbnail': r're:https?://.*\.(?:jpg|png)',
|
|
|
|
'episode': 'Broadcast Dub Preview',
|
|
|
|
'episode_id': '210050',
|
|
|
|
'season': 'Extras',
|
|
|
|
'season_id': '166038',
|
|
|
|
'season_number': 99,
|
|
|
|
'series': 'Attack on Titan: Junior High',
|
|
|
|
'description': '',
|
2021-10-09 15:21:41 +00:00
|
|
|
'duration': 155,
|
2015-12-11 18:38:58 +00:00
|
|
|
},
|
2017-04-01 12:32:38 +00:00
|
|
|
'params': {
|
2021-07-06 21:21:29 +00:00
|
|
|
'skip_download': 'm3u8',
|
2017-04-01 12:32:38 +00:00
|
|
|
},
|
2015-12-11 18:43:03 +00:00
|
|
|
}, {
|
2021-07-06 21:21:29 +00:00
|
|
|
'note': 'player_id should be extracted with the relevent compat-opt',
|
|
|
|
'url': 'https://www.funimation.com/player/210051',
|
2015-12-11 18:52:00 +00:00
|
|
|
'info_dict': {
|
2017-05-08 17:50:25 +00:00
|
|
|
'id': '210051',
|
2015-12-11 18:52:00 +00:00
|
|
|
'display_id': 'broadcast-dub-preview',
|
|
|
|
'ext': 'mp4',
|
2021-07-06 21:21:29 +00:00
|
|
|
'title': 'Broadcast Dub Preview',
|
2017-01-02 12:08:07 +00:00
|
|
|
'thumbnail': r're:https?://.*\.(?:jpg|png)',
|
2021-07-06 21:21:29 +00:00
|
|
|
'episode': 'Broadcast Dub Preview',
|
|
|
|
'episode_id': '210050',
|
|
|
|
'season': 'Extras',
|
|
|
|
'season_id': '166038',
|
|
|
|
'season_number': 99,
|
|
|
|
'series': 'Attack on Titan: Junior High',
|
|
|
|
'description': '',
|
2021-10-09 15:21:41 +00:00
|
|
|
'duration': 155,
|
2015-12-11 18:52:00 +00:00
|
|
|
},
|
2017-05-08 17:50:25 +00:00
|
|
|
'params': {
|
2021-07-06 21:21:29 +00:00
|
|
|
'skip_download': 'm3u8',
|
|
|
|
'compat_opts': ['seperate-video-versions'],
|
2017-05-08 17:50:25 +00:00
|
|
|
},
|
2015-12-11 18:38:58 +00:00
|
|
|
}]
|
2015-12-11 17:00:22 +00:00
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_experiences(episode):
|
|
|
|
for lang, lang_data in episode.get('languages', {}).items():
|
|
|
|
for video_data in lang_data.values():
|
|
|
|
for version, f in video_data.items():
|
|
|
|
yield lang, version.title(), f
|
2015-12-11 17:00:22 +00:00
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
def _get_episode(self, webpage, experience_id=None, episode_id=None, fatal=True):
|
|
|
|
''' Extract the episode, season and show objects given either episode/experience id '''
|
|
|
|
show = self._parse_json(
|
|
|
|
self._search_regex(
|
|
|
|
r'show\s*=\s*({.+?})\s*;', webpage, 'show data', fatal=fatal),
|
|
|
|
experience_id, transform_source=js_to_json, fatal=fatal) or []
|
|
|
|
for season in show.get('seasons', []):
|
|
|
|
for episode in season.get('episodes', []):
|
|
|
|
if episode_id is not None:
|
|
|
|
if str(episode.get('episodePk')) == episode_id:
|
|
|
|
return episode, season, show
|
|
|
|
continue
|
|
|
|
for _, _, f in self._get_experiences(episode):
|
|
|
|
if f.get('experienceId') == experience_id:
|
|
|
|
return episode, season, show
|
|
|
|
if fatal:
|
|
|
|
raise ExtractorError('Unable to find episode information')
|
|
|
|
else:
|
|
|
|
self.report_warning('Unable to find episode information')
|
|
|
|
return {}, {}, {}
|
2017-04-01 12:32:38 +00:00
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
def _real_extract(self, url):
|
|
|
|
initial_experience_id = self._match_id(url)
|
|
|
|
webpage = self._download_webpage(
|
|
|
|
url, initial_experience_id, note=f'Downloading player webpage for {initial_experience_id}')
|
|
|
|
episode, season, show = self._get_episode(webpage, experience_id=int(initial_experience_id))
|
|
|
|
episode_id = str(episode['episodePk'])
|
|
|
|
display_id = episode.get('slug') or episode_id
|
2017-04-01 12:32:38 +00:00
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
formats, subtitles, thumbnails, duration = [], {}, [], 0
|
|
|
|
requested_languages, requested_versions = self._configuration_arg('language'), self._configuration_arg('version')
|
2021-10-02 17:05:24 +00:00
|
|
|
language_preference = qualities((requested_languages or [''])[::-1])
|
|
|
|
source_preference = qualities((requested_versions or ['uncut', 'simulcast'])[::-1])
|
2021-07-06 21:21:29 +00:00
|
|
|
only_initial_experience = 'seperate-video-versions' in self.get_param('compat_opts', [])
|
|
|
|
|
|
|
|
for lang, version, fmt in self._get_experiences(episode):
|
|
|
|
experience_id = str(fmt['experienceId'])
|
|
|
|
if (only_initial_experience and experience_id != initial_experience_id
|
2021-07-08 15:33:13 +00:00
|
|
|
or requested_languages and lang.lower() not in requested_languages
|
|
|
|
or requested_versions and version.lower() not in requested_versions):
|
2021-07-06 21:21:29 +00:00
|
|
|
continue
|
|
|
|
thumbnails.append({'url': fmt.get('poster')})
|
|
|
|
duration = max(duration, fmt.get('duration', 0))
|
|
|
|
format_name = '%s %s (%s)' % (version, lang, experience_id)
|
|
|
|
self.extract_subtitles(
|
|
|
|
subtitles, experience_id, display_id=display_id, format_name=format_name,
|
|
|
|
episode=episode if experience_id == initial_experience_id else episode_id)
|
2015-12-11 17:00:22 +00:00
|
|
|
|
2017-05-08 17:13:58 +00:00
|
|
|
headers = {}
|
|
|
|
if self._TOKEN:
|
|
|
|
headers['Authorization'] = 'Token %s' % self._TOKEN
|
2021-07-06 21:21:29 +00:00
|
|
|
page = self._download_json(
|
|
|
|
'https://www.funimation.com/api/showexperience/%s/' % experience_id,
|
|
|
|
display_id, headers=headers, expected_status=403, query={
|
2019-01-15 09:23:59 +00:00
|
|
|
'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
|
2021-07-06 21:21:29 +00:00
|
|
|
}, note=f'Downloading {format_name} JSON')
|
|
|
|
sources = page.get('items') or []
|
|
|
|
if not sources:
|
|
|
|
error = try_get(page, lambda x: x['errors'][0], dict)
|
|
|
|
if error:
|
|
|
|
self.report_warning('%s said: Error %s - %s' % (
|
|
|
|
self.IE_NAME, error.get('code'), error.get('detail') or error.get('title')))
|
|
|
|
else:
|
|
|
|
self.report_warning('No sources found for format')
|
2015-12-11 17:00:22 +00:00
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
current_formats = []
|
|
|
|
for source in sources:
|
|
|
|
source_url = source.get('src')
|
|
|
|
source_type = source.get('videoType') or determine_ext(source_url)
|
|
|
|
if source_type == 'm3u8':
|
|
|
|
current_formats.extend(self._extract_m3u8_formats(
|
|
|
|
source_url, display_id, 'mp4', m3u8_id='%s-%s' % (experience_id, 'hls'), fatal=False,
|
|
|
|
note=f'Downloading {format_name} m3u8 information'))
|
|
|
|
else:
|
|
|
|
current_formats.append({
|
|
|
|
'format_id': '%s-%s' % (experience_id, source_type),
|
|
|
|
'url': source_url,
|
|
|
|
})
|
|
|
|
for f in current_formats:
|
|
|
|
# TODO: Convert language to code
|
2021-10-02 17:05:24 +00:00
|
|
|
f.update({
|
|
|
|
'language': lang,
|
|
|
|
'format_note': version,
|
|
|
|
'source_preference': source_preference(version.lower()),
|
|
|
|
'language_preference': language_preference(lang.lower()),
|
|
|
|
})
|
2021-07-06 21:21:29 +00:00
|
|
|
formats.extend(current_formats)
|
2022-04-29 01:48:36 +00:00
|
|
|
if not formats and (requested_languages or requested_versions):
|
|
|
|
self.raise_no_formats(
|
|
|
|
'There are no video formats matching the requested languages/versions', expected=True, video_id=display_id)
|
2021-07-06 21:21:29 +00:00
|
|
|
self._remove_duplicate_formats(formats)
|
2015-12-11 18:38:58 +00:00
|
|
|
|
2015-12-06 22:15:19 +00:00
|
|
|
return {
|
2022-07-13 09:33:05 +00:00
|
|
|
'id': episode_id,
|
2022-08-01 22:10:47 +00:00
|
|
|
'_old_archive_ids': [make_archive_id(self, initial_experience_id)],
|
2015-12-11 17:00:22 +00:00
|
|
|
'display_id': display_id,
|
2021-07-06 21:21:29 +00:00
|
|
|
'duration': duration,
|
|
|
|
'title': episode['episodeTitle'],
|
|
|
|
'description': episode.get('episodeSummary'),
|
|
|
|
'episode': episode.get('episodeTitle'),
|
|
|
|
'episode_number': int_or_none(episode.get('episodeId')),
|
|
|
|
'episode_id': episode_id,
|
|
|
|
'season': season.get('seasonTitle'),
|
|
|
|
'season_number': int_or_none(season.get('seasonId')),
|
|
|
|
'season_id': str_or_none(season.get('seasonPk')),
|
|
|
|
'series': show.get('showTitle'),
|
2015-12-06 22:15:19 +00:00
|
|
|
'formats': formats,
|
2021-07-06 21:21:29 +00:00
|
|
|
'thumbnails': thumbnails,
|
|
|
|
'subtitles': subtitles,
|
2022-11-17 05:10:03 +00:00
|
|
|
'_format_sort_fields': ('lang', 'source'),
|
2015-12-06 22:15:19 +00:00
|
|
|
}
|
2021-06-23 01:57:53 +00:00
|
|
|
|
2021-07-06 21:21:29 +00:00
|
|
|
def _get_subtitles(self, subtitles, experience_id, episode, display_id, format_name):
|
|
|
|
if isinstance(episode, str):
|
|
|
|
webpage = self._download_webpage(
|
2021-11-19 00:06:28 +00:00
|
|
|
f'https://www.funimation.com/player/{experience_id}/', display_id,
|
2021-07-06 21:21:29 +00:00
|
|
|
fatal=False, note=f'Downloading player webpage for {format_name}')
|
|
|
|
episode, _, _ = self._get_episode(webpage, episode_id=episode, fatal=False)
|
|
|
|
|
|
|
|
for _, version, f in self._get_experiences(episode):
|
|
|
|
for source in f.get('sources'):
|
|
|
|
for text_track in source.get('textTracks'):
|
|
|
|
if not text_track.get('src'):
|
|
|
|
continue
|
|
|
|
sub_type = text_track.get('type').upper()
|
|
|
|
sub_type = sub_type if sub_type != 'FULL' else None
|
|
|
|
current_sub = {
|
|
|
|
'url': text_track['src'],
|
2021-11-06 01:05:24 +00:00
|
|
|
'name': join_nonempty(version, text_track.get('label'), sub_type, delim=' ')
|
2021-07-06 21:21:29 +00:00
|
|
|
}
|
2021-11-06 01:05:24 +00:00
|
|
|
lang = join_nonempty(text_track.get('language', 'und'),
|
|
|
|
version if version != 'Simulcast' else None,
|
|
|
|
sub_type, delim='_')
|
2021-07-06 21:21:29 +00:00
|
|
|
if current_sub not in subtitles.get(lang, []):
|
|
|
|
subtitles.setdefault(lang, []).append(current_sub)
|
2021-06-23 01:57:53 +00:00
|
|
|
return subtitles
|
2021-06-25 00:15:23 +00:00
|
|
|
|
|
|
|
|
2021-10-09 15:21:41 +00:00
|
|
|
class FunimationShowIE(FunimationBaseIE):
|
2021-06-25 00:15:23 +00:00
|
|
|
IE_NAME = 'funimation:show'
|
|
|
|
_VALID_URL = r'(?P<url>https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?P<locale>[^/]+)?/?shows/(?P<id>[^/?#&]+))/?(?:[?#]|$)'
|
|
|
|
|
|
|
|
_TESTS = [{
|
|
|
|
'url': 'https://www.funimation.com/en/shows/sk8-the-infinity',
|
|
|
|
'info_dict': {
|
|
|
|
'id': 1315000,
|
|
|
|
'title': 'SK8 the Infinity'
|
|
|
|
},
|
|
|
|
'playlist_count': 13,
|
|
|
|
'params': {
|
|
|
|
'skip_download': True,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
# without lang code
|
|
|
|
'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/',
|
|
|
|
'info_dict': {
|
|
|
|
'id': 39643,
|
|
|
|
'title': 'Ouran High School Host Club'
|
|
|
|
},
|
|
|
|
'playlist_count': 26,
|
|
|
|
'params': {
|
|
|
|
'skip_download': True,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
|
2021-09-12 16:04:45 +00:00
|
|
|
def _real_initialize(self):
|
2021-10-09 15:21:41 +00:00
|
|
|
if not self._REGION:
|
|
|
|
FunimationBaseIE._REGION = self._get_region()
|
2021-09-12 16:04:45 +00:00
|
|
|
|
2021-06-25 00:15:23 +00:00
|
|
|
def _real_extract(self, url):
|
2021-08-19 01:41:24 +00:00
|
|
|
base_url, locale, display_id = self._match_valid_url(url).groups()
|
2021-06-25 00:15:23 +00:00
|
|
|
|
|
|
|
show_info = self._download_json(
|
2021-09-12 16:04:45 +00:00
|
|
|
'https://title-api.prd.funimationsvc.com/v2/shows/%s?region=%s&deviceType=web&locale=%s'
|
2021-10-09 15:21:41 +00:00
|
|
|
% (display_id, self._REGION, locale or 'en'), display_id)
|
|
|
|
items_info = self._download_json(
|
2021-06-25 00:15:23 +00:00
|
|
|
'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s'
|
2021-10-09 15:21:41 +00:00
|
|
|
% show_info.get('id'), display_id)
|
|
|
|
|
2022-03-31 07:55:50 +00:00
|
|
|
vod_items = traverse_obj(items_info, ('items', ..., lambda k, _: re.match(r'(?i)mostRecent[AS]vod', k), 'item'))
|
2021-06-25 00:15:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'_type': 'playlist',
|
|
|
|
'id': show_info['id'],
|
|
|
|
'title': show_info['name'],
|
2021-10-09 15:21:41 +00:00
|
|
|
'entries': orderedSet(
|
2021-06-25 00:15:23 +00:00
|
|
|
self.url_result(
|
2021-07-06 21:21:29 +00:00
|
|
|
'%s/%s' % (base_url, vod_item.get('episodeSlug')), FunimationPageIE.ie_key(),
|
2021-06-25 00:15:23 +00:00
|
|
|
vod_item.get('episodeId'), vod_item.get('episodeName'))
|
2021-10-09 15:21:41 +00:00
|
|
|
for vod_item in sorted(vod_items, key=lambda x: x.get('episodeOrder', -1))),
|
2021-06-25 00:15:23 +00:00
|
|
|
}
|