2024-10-22 16:40:48 -05:00
|
|
|
from .common import InfoExtractor
|
2024-10-23 00:32:11 -05:00
|
|
|
from ..utils import (
|
|
|
|
ExtractorError,
|
|
|
|
float_or_none,
|
|
|
|
smuggle_url,
|
|
|
|
unified_timestamp,
|
|
|
|
unsmuggle_url,
|
|
|
|
)
|
2024-10-22 16:40:48 -05:00
|
|
|
from ..utils.traversal import traverse_obj
|
|
|
|
|
|
|
|
|
2024-10-30 00:11:38 -05:00
|
|
|
class BahamutIE(InfoExtractor):
|
2024-10-22 16:40:48 -05:00
|
|
|
_VALID_URL = r'https?://ani\.gamer\.com\.tw/animeVideo\.php\?sn=(?P<id>\d+)'
|
2024-12-23 18:34:48 -06:00
|
|
|
_DEVICE_ID = None
|
2024-10-22 16:40:48 -05:00
|
|
|
|
2024-12-23 20:42:53 -06:00
|
|
|
_TESTS = [{
|
|
|
|
'url': 'https://ani.gamer.com.tw/animeVideo.php?sn=40137',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '40137',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': '膽大黨 [1]',
|
|
|
|
'upload_date': '20241004',
|
|
|
|
'duration': 0.38333333333333336,
|
|
|
|
'age_limit': 12,
|
|
|
|
'tags': ['動作', '冒險', '奇幻', '超能力', '科幻', '喜劇', '戀愛', '青春', '血腥暴力', '靈異神怪'],
|
|
|
|
'thumbnail': 'https://p2.bahamut.com.tw/B/2KU/19/7d54e1421935f94781555420131rolv5.JPG',
|
|
|
|
'creators': ['山代風我'],
|
|
|
|
'timestamp': 1728000000,
|
|
|
|
'description': 'md5:c16931fb4d24d91b858715a2560362b5',
|
|
|
|
},
|
|
|
|
'params': {'noplaylist': True},
|
|
|
|
'skip': 'geo-restricted',
|
|
|
|
}]
|
|
|
|
|
2024-10-30 00:11:38 -05:00
|
|
|
# see anime_player.js
|
2024-10-22 16:40:48 -05:00
|
|
|
RATING_TO_AGE_LIMIT = {
|
|
|
|
1: 0,
|
|
|
|
2: 6,
|
|
|
|
3: 12,
|
|
|
|
4: 15,
|
2024-10-30 00:11:38 -05:00
|
|
|
5: 18,
|
|
|
|
6: 18, # age-gated, needs login
|
2024-10-22 16:40:48 -05:00
|
|
|
}
|
|
|
|
|
2024-12-23 18:34:48 -06:00
|
|
|
def _download_device_id(self, video_id):
|
|
|
|
return self._download_json(
|
|
|
|
'https://ani.gamer.com.tw/ajax/getdeviceid.php', video_id,
|
|
|
|
'Downloading device ID', 'Failed to download device ID',
|
|
|
|
impersonate=True, headers=self.geo_verification_headers())['deviceid']
|
|
|
|
|
2024-10-22 16:40:48 -05:00
|
|
|
def _real_extract(self, url):
|
|
|
|
url, unsmuggled_data = unsmuggle_url(url, {})
|
|
|
|
video_id = self._match_id(url)
|
2024-12-23 18:34:48 -06:00
|
|
|
if not self._DEVICE_ID:
|
|
|
|
self._DEVICE_ID = (
|
|
|
|
self._configuration_arg('device_id', [None], casesense=True)[0]
|
|
|
|
or self._download_device_id(video_id))
|
2024-10-30 00:11:38 -05:00
|
|
|
|
|
|
|
# TODO: extract metadata from webpage
|
2024-10-22 16:40:48 -05:00
|
|
|
metadata = {}
|
|
|
|
if api_result := self._download_json(
|
|
|
|
'https://api.gamer.com.tw/anime/v1/video.php', video_id,
|
|
|
|
'Downloading video info', 'Failed to download video info',
|
2024-10-31 17:15:54 -05:00
|
|
|
impersonate=True, query={'videoSn': video_id}).get('data'):
|
2024-10-22 16:40:48 -05:00
|
|
|
|
|
|
|
metadata.update(traverse_obj(api_result, ('anime', {
|
|
|
|
'description': 'content',
|
|
|
|
'thumbnail': 'cover',
|
|
|
|
'tags': 'tags',
|
|
|
|
'creators': ('director', {lambda x: [x]}),
|
|
|
|
'title': 'title',
|
|
|
|
})))
|
|
|
|
playlist_id = traverse_obj(api_result, ('video', 'animeSn')) or ''
|
|
|
|
if self._yes_playlist(playlist_id, video_id) and unsmuggled_data.get('extract_playlist') is not False:
|
|
|
|
return self.playlist_result(
|
|
|
|
(self.url_result(
|
|
|
|
smuggle_url(f'https://ani.gamer.com.tw/animeVideo.php?sn={ep["videoSn"]}', {
|
|
|
|
'extract_playlist': False,
|
2024-10-30 00:11:38 -05:00
|
|
|
}), ie=BahamutIE,
|
2024-10-23 00:32:11 -05:00
|
|
|
video_id=ep['videoSn'], thumbnail=ep.get('cover')) for ep in traverse_obj(
|
2024-10-22 16:40:48 -05:00
|
|
|
api_result,
|
|
|
|
# This (the first ellipsis) extracts episodes of all languages,
|
|
|
|
# maybe just extract episodes of the current language?
|
|
|
|
('anime', 'episodes', ..., ...))),
|
|
|
|
playlist_id=playlist_id, **metadata)
|
|
|
|
|
|
|
|
# video-specific metadata, extract after returning the playlist result
|
|
|
|
metadata.update(traverse_obj(api_result, ('video', {
|
|
|
|
'thumbnail': 'cover',
|
|
|
|
'title': 'title',
|
|
|
|
'timestamp': ('upTime', {unified_timestamp}),
|
2024-10-31 05:37:35 -05:00
|
|
|
'duration': ('duration', {float_or_none(scale=60)}),
|
2024-10-22 16:40:48 -05:00
|
|
|
'age_limit': ('rating', {lambda x: self.RATING_TO_AGE_LIMIT.get(x)}),
|
|
|
|
})))
|
|
|
|
|
2024-10-30 00:11:38 -05:00
|
|
|
m3u8_info, urlh = self._download_json_handle(
|
|
|
|
'https://ani.gamer.com.tw/ajax/m3u8.php', video_id,
|
|
|
|
note='Downloading m3u8 URL', errnote='Failed to download m3u8 URL', query={
|
|
|
|
'sn': video_id,
|
2024-12-23 18:34:48 -06:00
|
|
|
'device': self._DEVICE_ID,
|
2024-10-31 17:15:54 -05:00
|
|
|
}, impersonate=True, headers=self.geo_verification_headers(), expected_status=400)
|
2024-10-30 00:11:38 -05:00
|
|
|
|
2024-10-31 06:00:59 -05:00
|
|
|
formats_fatal = True
|
2024-10-30 00:11:38 -05:00
|
|
|
if urlh.status == 400:
|
|
|
|
# TODO: handle more error codes, search for /case \d+{4}:/g in anime_player.js
|
|
|
|
error_code = traverse_obj(m3u8_info, ('error', 'code'))
|
|
|
|
if error_code == 1011:
|
2024-10-31 06:00:59 -05:00
|
|
|
formats_fatal = False
|
2024-12-23 20:42:53 -06:00
|
|
|
self.raise_geo_restricted(metadata_available=not formats_fatal)
|
2024-10-30 00:11:38 -05:00
|
|
|
elif error_code == 1007:
|
2024-12-23 18:34:48 -06:00
|
|
|
if self._configuration_arg('device_id', casesense=True):
|
|
|
|
# the passed device id may be wrong or expired
|
|
|
|
self._DEVICE_ID = self._download_device_id(video_id)
|
|
|
|
return self.url_result(url, ie=BahamutIE, video_id=video_id)
|
2024-10-30 00:11:38 -05:00
|
|
|
raise ExtractorError('Invalid device id!')
|
|
|
|
elif error_code == 1017:
|
2024-10-31 06:00:59 -05:00
|
|
|
formats_fatal = False
|
2024-12-23 20:42:53 -06:00
|
|
|
self.raise_login_required(metadata_available=not formats_fatal)
|
2024-10-31 06:00:59 -05:00
|
|
|
else:
|
|
|
|
raise ExtractorError(
|
|
|
|
traverse_obj(m3u8_info, ('error', 'message')) or 'Failed to download m3u8 URL')
|
2024-10-31 05:37:35 -05:00
|
|
|
|
2024-10-22 16:40:48 -05:00
|
|
|
return {
|
|
|
|
**metadata,
|
|
|
|
'id': video_id,
|
2024-10-31 06:00:59 -05:00
|
|
|
'formats': self._extract_m3u8_formats(
|
|
|
|
m3u8_info.get('src'), video_id, ext='mp4', fatal=formats_fatal, headers={
|
|
|
|
'Origin': 'https://ani.gamer.com.tw',
|
|
|
|
**self.geo_verification_headers(),
|
|
|
|
}),
|
2024-10-31 05:37:35 -05:00
|
|
|
'http_headers': {'Origin': 'https://ani.gamer.com.tw'},
|
2024-10-22 16:40:48 -05:00
|
|
|
}
|