Compare commits

...

2 Commits

Author SHA1 Message Date
bashonly
a4cc752440
Update nhk.py 2023-10-06 22:23:45 +00:00
bashonly
4b546e9650
Apply suggestions from code review 2023-10-06 22:19:36 +00:00

View File

@ -28,30 +28,43 @@ class NhkBaseIE(InfoExtractor):
m_id, lang, '/all' if is_video else ''), m_id, lang, '/all' if is_video else ''),
m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'] or [] m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'] or []
def _extract_vod_api(self): def _get_api_info(self, refresh=True):
if not refresh:
return self.cache.load('nhk', 'api_info')
self.cache.store('nhk', 'api_info', {})
movie_player_js = self._download_webpage( movie_player_js = self._download_webpage(
'https://movie-a.nhk.or.jp/world/player/js/movie-player.js', None, 'https://movie-a.nhk.or.jp/world/player/js/movie-player.js', None,
note='Downloading stream API information') note='Downloading stream API information')
api_url = self._search_regex( api_info = {
r'prod:[^;]+apiUrl:\s*[\'"]([^\'"]+)[\'"]', movie_player_js, None, 'stream API url') 'url': self._search_regex(
api_token = self._search_regex( r'prod:[^;]+\bapiUrl:\s*[\'"]([^\'"]+)[\'"]', movie_player_js, None, 'stream API url'),
r'prod:[^;]+token:\s*[\'"]([^\'"]+)[\'"]', movie_player_js, None, 'stream API token') 'token': self._search_regex(
self.cache.store('nhkworld', 'vod_api', {'url': api_url, 'token': api_token}) r'prod:[^;]+\btoken:\s*[\'"]([^\'"]+)[\'"]', movie_player_js, None, 'stream API token'),
return api_url, api_token }
self.cache.store('nhk', 'api_info', api_info)
return api_info
def _load_vod_api(self): def _extract_formats_and_subtitles(self, vod_id):
cachedata = self.cache.load('nhkworld', 'vod_api') for refresh in (False, True):
if cachedata is not None and url_or_none(cachedata.get('url')) is not None: api_info = self._get_api_info(refresh)
return cachedata['url'], cachedata.get('token') if not api_info:
return self._extract_vod_api() continue
def _extract_stream_info(self, api_url, api_token, vod_id): api_url = api_info.pop('url')
return self._download_json(api_url, vod_id, query={ stream_url = traverse_obj(
'token': api_token, self._download_json(
api_url, vod_id, 'Downloading stream url info', fatal=False, query={
**api_info,
'type': 'json', 'type': 'json',
'optional_id': vod_id, 'optional_id': vod_id,
'active_flg': 1, 'active_flg': 1,
}, note='Downloading stream information') }),
('meta', 0, 'movie_url', ('mb_auto', 'auto_sp', 'auto_pc'), {url_or_none}), get_all=False)
if stream_url:
return self._extract_m3u8_formats_and_subtitles(stream_url, vod_id)
raise ExtractorError('Unable to extract stream url')
def _extract_episode_info(self, url, episode=None): def _extract_episode_info(self, url, episode=None):
fetch_episode = episode is None fetch_episode = episode is None
@ -92,16 +105,7 @@ class NhkBaseIE(InfoExtractor):
} }
if is_video: if is_video:
vod_id = episode['vod_id'] vod_id = episode['vod_id']
api_url, api_token = self._load_vod_api() formats, subs = self._extract_formats_and_subtitles(vod_id)
streams_json = self._extract_stream_info(api_url, api_token, vod_id)
if streams_json.get('response_status') != '2000':
self.to_screen('Getting stream information failed, trying again with fresh API details')
api_url, api_token = self._extract_vod_api()
streams_json = self._extract_stream_info(api_url, api_token, vod_id)
stream_url = traverse_obj(streams_json, (
'meta', 0, 'movie_url', ('mb_auto', 'auto_sp', 'auto_pc'), {url_or_none}), get_all=False)
formats, subs = self._extract_m3u8_formats_and_subtitles(stream_url, vod_id)
info.update({ info.update({
'id': vod_id, 'id': vod_id,