From e16a84dd2e693102ef817ebf41474b68d9dd32b9 Mon Sep 17 00:00:00 2001 From: Rafael Moraes <50295204+glomatico@users.noreply.github.com> Date: Sun, 15 Dec 2024 00:09:05 -0300 Subject: [PATCH] adjust `get_cover_url` --- votify/constants.py | 9 ++++++++- votify/downloader.py | 14 +++++++++++++- votify/downloader_audio.py | 15 +-------------- votify/downloader_episode.py | 7 +++++-- votify/downloader_episode_video.py | 6 +++++- votify/downloader_music_video.py | 6 +++++- votify/downloader_song.py | 6 +++++- votify/downloader_video.py | 13 ------------- 8 files changed, 42 insertions(+), 34 deletions(-) diff --git a/votify/constants.py b/votify/constants.py index 5fef4fe..43d1d9f 100644 --- a/votify/constants.py +++ b/votify/constants.py @@ -72,13 +72,20 @@ MEDIA_TYPE_MP4_MAPPING = { "Music video": 6, } -COVER_SIZE_X_KEY_MAPPING_AUDIO = { +COVER_SIZE_X_KEY_MAPPING_SONG = { CoverSize.SMALL: "ab67616d00004851", CoverSize.MEDIUM: "ab67616d00001e02", CoverSize.LARGE: "ab67616d0000b273", CoverSize.EXTRA_LARGE: "ab67616d000082c1", } +COVER_SIZE_X_KEY_MAPPING_EPISODE= { + CoverSize.SMALL: "ab6765630000f68d", + CoverSize.MEDIUM: "ab67656300005f1f", + CoverSize.LARGE: "ab6765630000ba8a", + CoverSize.EXTRA_LARGE: "ab6765630000ba8a", +} + COVER_SIZE_X_KEY_MAPPING_VIDEO = { CoverSize.SMALL: "ab6742d3000052b7", CoverSize.MEDIUM: "ab6742d3000052b7", diff --git a/votify/downloader.py b/votify/downloader.py index 2c7d550..8889b8d 100644 --- a/votify/downloader.py +++ b/votify/downloader.py @@ -20,7 +20,6 @@ from PIL import Image from pywidevine import PSSH, Cdm, Device from .constants import ( - COVER_SIZE_X_KEY_MAPPING_AUDIO, MEDIA_TYPE_MP4_MAPPING, MP4_TAGS_MAP, VORBIS_TAGS_MAPPING, @@ -259,6 +258,19 @@ class Downloader: ) return download_queue + def get_cover_url(self, metadata: dict, cover_size_mapping: dict) -> str | None: + if not metadata.get("images"): + return None + return self._get_cover_url(metadata["images"], cover_size_mapping) + + def _get_cover_url(self, images_dict: list[dict], cover_size_mapping: dict) -> str: + original_cover_url = images_dict[0]["url"] + original_cover_id = original_cover_url.split("/")[-1] + cover_key = cover_size_mapping[self.cover_size] + cover_id = cover_key + original_cover_id[len(cover_key) :] + cover_url = f"{original_cover_url.rpartition('/')[0]}/{cover_id}" + return cover_url + def get_media_id(self, media_metadata: dict) -> str: return (media_metadata.get("linked_from") or media_metadata)["id"] diff --git a/votify/downloader_audio.py b/votify/downloader_audio.py index 0c47e9f..65c346b 100644 --- a/votify/downloader_audio.py +++ b/votify/downloader_audio.py @@ -10,7 +10,7 @@ from yt_dlp.YoutubeDL import YoutubeDL from .constants import ( AAC_AUDIO_QUALITIES, AUDIO_QUALITY_X_FORMAT_ID_MAPPING, - COVER_SIZE_X_KEY_MAPPING_AUDIO, + COVER_SIZE_X_KEY_MAPPING_SONG, VORBIS_AUDIO_QUALITIES, ) from .downloader import Downloader @@ -35,19 +35,6 @@ class DownloaderAudio: self.download_mode = download_mode self.remux_mode = remux_mode - def get_cover_url(self, metadata: dict) -> str | None: - if not metadata.get("images"): - return None - return self._get_cover_url(metadata["images"]) - - def _get_cover_url(self, images_dict: list[dict]) -> str: - original_cover_url = images_dict[0]["url"] - original_cover_id = original_cover_url.split("/")[-1] - cover_key = COVER_SIZE_X_KEY_MAPPING_AUDIO[self.downloader.cover_size] - cover_id = cover_key + original_cover_id[len(cover_key) :] - cover_url = f"{original_cover_url.rpartition('/')[0]}/{cover_id}" - return cover_url - def get_file_extension( self, ) -> str: diff --git a/votify/downloader_episode.py b/votify/downloader_episode.py index bbde7c7..0e0e759 100644 --- a/votify/downloader_episode.py +++ b/votify/downloader_episode.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging from pathlib import Path -from .constants import AAC_AUDIO_QUALITIES +from .constants import AAC_AUDIO_QUALITIES, COVER_SIZE_X_KEY_MAPPING_EPISODE from .downloader_audio import DownloaderAudio from .models import StreamInfoAudio @@ -114,7 +114,10 @@ class DownloaderEpisode(DownloaderAudio): file_extension, ) cover_path = self.get_cover_path(final_path) - cover_url = self.get_cover_url(episode_metadata) + cover_url = self.downloader.get_cover_url( + episode_metadata, + COVER_SIZE_X_KEY_MAPPING_EPISODE, + ) decrypted_path = None remuxed_path = None if final_path.exists() and not self.downloader.overwrite: diff --git a/votify/downloader_episode_video.py b/votify/downloader_episode_video.py index 7d3f52b..cdff1fa 100644 --- a/votify/downloader_episode_video.py +++ b/votify/downloader_episode_video.py @@ -4,6 +4,7 @@ import logging from .downloader_episode import DownloaderEpisode from .downloader_video import DownloaderVideo +from .constants import COVER_SIZE_X_KEY_MAPPING_EPISODE logger = logging.getLogger("votify") @@ -80,7 +81,10 @@ class DownloaderEpisodeVideo(DownloaderVideo): file_extension, ) cover_path = self.get_cover_path(final_path) - cover_url = self.downloader_episode.get_cover_url(show_metadata) + cover_url = self.downloader.get_cover_url( + show_metadata, + COVER_SIZE_X_KEY_MAPPING_EPISODE, + ) remuxed_path = None if final_path.exists() and not self.downloader.overwrite: logger.warning(f'Episode already exists at "{final_path}", skipping') diff --git a/votify/downloader_music_video.py b/votify/downloader_music_video.py index 9ebb31f..ccae774 100644 --- a/votify/downloader_music_video.py +++ b/votify/downloader_music_video.py @@ -5,6 +5,7 @@ import logging from InquirerPy import inquirer from InquirerPy.base.control import Choice +from .constants import COVER_SIZE_X_KEY_MAPPING_VIDEO from .downloader_video import DownloaderVideo logger = logging.getLogger("votify") @@ -189,7 +190,10 @@ class DownloaderMusicVideo(DownloaderVideo): file_extension, ) cover_path = self.get_cover_path(final_path) - cover_url = self.get_cover_url(album_metadata) + cover_url = self.downloader.get_cover_url( + album_metadata, + COVER_SIZE_X_KEY_MAPPING_VIDEO, + ) remuxed_path = None if final_path.exists() and not self.downloader.overwrite: logger.warning(f'Music video already exists at "{final_path}", skipping') diff --git a/votify/downloader_song.py b/votify/downloader_song.py index cf1b37f..320f12b 100644 --- a/votify/downloader_song.py +++ b/votify/downloader_song.py @@ -4,6 +4,7 @@ import datetime import logging from pathlib import Path +from .constants import COVER_SIZE_X_KEY_MAPPING_SONG from .downloader_audio import DownloaderAudio from .models import Lyrics, StreamInfoAudio @@ -200,7 +201,10 @@ class DownloaderSong(DownloaderAudio): ) lrc_path = self.downloader.get_lrc_path(final_path) cover_path = self.get_cover_path(final_path) - cover_url = self.get_cover_url(album_metadata) + cover_url = self.downloader.get_cover_url( + album_metadata, + COVER_SIZE_X_KEY_MAPPING_SONG, + ) decrypted_path = None remuxed_path = None if self.lrc_only: diff --git a/votify/downloader_video.py b/votify/downloader_video.py index a0fa45e..65eadcb 100644 --- a/votify/downloader_video.py +++ b/votify/downloader_video.py @@ -26,19 +26,6 @@ class DownloaderVideo: self.remux_mode = remux_mode self._adjust_remux_mode() - def get_cover_url(self, metadata: dict) -> str | None: - if not metadata.get("images"): - return None - return self._get_cover_url(metadata["images"]) - - def _get_cover_url(self, images_dict: list[dict]) -> str: - original_cover_url = images_dict[0]["url"] - original_cover_id = original_cover_url.split("/")[-1] - cover_key = COVER_SIZE_X_KEY_MAPPING_VIDEO[self.downloader.cover_size] - cover_id = cover_key + original_cover_id[len(cover_key) :] - cover_url = f"{original_cover_url.rpartition('/')[0]}/{cover_id}" - return cover_url - def _adjust_remux_mode(self): if self.video_format == VideoFormat.WEBM: self.remux_mode = RemuxModeVideo.FFMPEG