From f87558f7a55d15678db34a2a9955e37161ee1a50 Mon Sep 17 00:00:00 2001 From: grqx_wsl <173253225+grqx@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:40:48 +1300 Subject: [PATCH] [ie/anigamer] add extractor --- yt_dlp/extractor/_extractors.py | 1 + yt_dlp/extractor/anigamer.py | 82 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 yt_dlp/extractor/anigamer.py diff --git a/yt_dlp/extractor/_extractors.py b/yt_dlp/extractor/_extractors.py index 8d5936094..5382440ab 100644 --- a/yt_dlp/extractor/_extractors.py +++ b/yt_dlp/extractor/_extractors.py @@ -127,6 +127,7 @@ from .americastestkitchen import ( ) from .anchorfm import AnchorFMEpisodeIE from .angel import AngelIE +from .anigamer import AniGamerIE from .antenna import ( Ant1NewsGrArticleIE, Ant1NewsGrEmbedIE, diff --git a/yt_dlp/extractor/anigamer.py b/yt_dlp/extractor/anigamer.py new file mode 100644 index 000000000..de2cea6e3 --- /dev/null +++ b/yt_dlp/extractor/anigamer.py @@ -0,0 +1,82 @@ +from .common import InfoExtractor +from ..utils import float_or_none, smuggle_url, unified_timestamp, unsmuggle_url +from ..utils.traversal import traverse_obj + + +class AniGamerIE(InfoExtractor): + _VALID_URL = r'https?://ani\.gamer\.com\.tw/animeVideo\.php\?sn=(?P\d+)' + + RATING_TO_AGE_LIMIT = { + 1: 0, + 2: 6, + 3: 12, + 4: 15, + # Seems like there's no age limit for '5' + 6: 18, + } + + def _real_extract(self, url): + url, unsmuggled_data = unsmuggle_url(url, {}) + video_id = self._match_id(url) + device_id = self._download_json( + 'https://ani.gamer.com.tw/ajax/getdeviceid.php', video_id, + 'Downloading device ID', 'Failed to download device ID', + headers=self.geo_verification_headers())['deviceid'] + metadata = {} + format_id = '0' + 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', + query={'videoSn': video_id}).get('data'): + + 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, + }), + AniGamerIE, + video_id=ep['videoSn'], thumbnail=ep['cover']) for ep in traverse_obj( + 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}), + 'duration': ('duration', {float_or_none}, {lambda x: x * 60}), + 'age_limit': ('rating', {lambda x: self.RATING_TO_AGE_LIMIT.get(x)}), + }))) + format_id = traverse_obj(api_result, ('video', 'quality'), default=format_id) + + m3u8_info = self._download_json('https://ani.gamer.com.tw/ajax/m3u8.php', video_id, query={ + 'sn': video_id, + 'device': device_id, + }, headers=self.geo_verification_headers(), expected_status=400) + + error_code = traverse_obj(m3u8_info, ('error', 'code')) + if error_code == 1011: + self.raise_geo_restricted() + # TODO: handle more error codes + src = m3u8_info['src'] + return { + **metadata, + 'id': video_id, + 'formats': [{ + 'format_id': format_id, + 'url': src, + 'ext': 'mp4', + }], + }