mirror of
https://github.com/glomatico/votify.git
synced 2026-10-07 08:22:07 +02:00
general refactoring
This commit is contained in:
+12
-10
@@ -14,7 +14,7 @@ from .constants import EXCLUDED_CONFIG_FILE_PARAMS, X_NOT_FOUND_STRING
|
||||
from .downloader import Downloader
|
||||
from .downloader_episode import DownloaderEpisode
|
||||
from .downloader_song import DownloaderSong
|
||||
from .enums import DownloadMode, Quality
|
||||
from .enums import DownloadMode, AudioQuality
|
||||
from .spotify_api import SpotifyApi
|
||||
|
||||
logger = logging.getLogger("votify")
|
||||
@@ -118,11 +118,11 @@ def load_config_file(
|
||||
)
|
||||
# Downloader specific options
|
||||
@click.option(
|
||||
"--quality",
|
||||
"-q",
|
||||
type=Quality,
|
||||
default=downloader_sig.parameters["quality"].default,
|
||||
help="Audio quality.",
|
||||
"--audio-quality",
|
||||
"-a",
|
||||
type=AudioQuality,
|
||||
default=downloader_sig.parameters["audio_quality"].default,
|
||||
help="Audio quality for songs and podcasts.",
|
||||
)
|
||||
@click.option(
|
||||
"--output-path",
|
||||
@@ -206,7 +206,6 @@ def load_config_file(
|
||||
)
|
||||
@click.option(
|
||||
"--save-cover",
|
||||
"-s",
|
||||
is_flag=True,
|
||||
help="Save cover as a separate file.",
|
||||
)
|
||||
@@ -261,7 +260,7 @@ def main(
|
||||
log_level: str,
|
||||
print_exceptions: bool,
|
||||
cookies_path: Path,
|
||||
quality: Quality,
|
||||
audio_quality: AudioQuality,
|
||||
output_path: Path,
|
||||
temp_path: Path,
|
||||
download_mode: DownloadMode,
|
||||
@@ -293,7 +292,7 @@ def main(
|
||||
spotify_api = SpotifyApi(cookies_path)
|
||||
downloader = Downloader(
|
||||
spotify_api,
|
||||
quality,
|
||||
audio_quality,
|
||||
output_path,
|
||||
temp_path,
|
||||
download_mode,
|
||||
@@ -331,7 +330,10 @@ def main(
|
||||
spotify_api.config_info["isPremium"] = (
|
||||
True if force_premium else spotify_api.config_info["isPremium"]
|
||||
)
|
||||
if not spotify_api.config_info["isPremium"] and quality == Quality.HIGH:
|
||||
if (
|
||||
not spotify_api.config_info["isPremium"]
|
||||
and audio_quality == AudioQuality.VORBIS_HIGH
|
||||
):
|
||||
logger.critical("Cannot download at chosen quality with a free account")
|
||||
return
|
||||
error_count = 0
|
||||
|
||||
+6
-4
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .enums import Quality
|
||||
from .enums import AudioQuality
|
||||
|
||||
EXCLUDED_CONFIG_FILE_PARAMS = (
|
||||
"urls",
|
||||
@@ -33,9 +33,11 @@ VORBIS_TAGS_MAPPING = {
|
||||
}
|
||||
|
||||
QUALITY_X_FORMAT_ID_MAPPING = {
|
||||
Quality.HIGH: "OGG_VORBIS_320",
|
||||
Quality.MEDIUM: "OGG_VORBIS_160",
|
||||
Quality.LOW: "OGG_VORBIS_96",
|
||||
AudioQuality.VORBIS_HIGH: "OGG_VORBIS_320",
|
||||
AudioQuality.VORBIS_MEDIUM: "OGG_VORBIS_160",
|
||||
AudioQuality.VORBIS_LOW: "OGG_VORBIS_96",
|
||||
}
|
||||
|
||||
X_NOT_FOUND_STRING = "{} not found at {}"
|
||||
|
||||
PREMIUM_SONG_QUALITIES = (AudioQuality.VORBIS_HIGH,)
|
||||
|
||||
+10
-10
@@ -19,8 +19,8 @@ from PIL import Image
|
||||
from yt_dlp import YoutubeDL
|
||||
|
||||
from .constants import QUALITY_X_FORMAT_ID_MAPPING, VORBIS_TAGS_MAPPING
|
||||
from .enums import DownloadMode, Quality
|
||||
from .models import DownloadQueueItem, StreamInfo, UrlInfo
|
||||
from .enums import AudioQuality, DownloadMode
|
||||
from .models import DownloadQueueItem, StreamInfoAudio, UrlInfo
|
||||
from .playplay_pb2 import (
|
||||
AUDIO_TRACK,
|
||||
Interactivity,
|
||||
@@ -44,7 +44,7 @@ class Downloader:
|
||||
def __init__(
|
||||
self,
|
||||
spotify_api: SpotifyApi,
|
||||
quality: Quality = Quality.MEDIUM,
|
||||
audio_quality: AudioQuality = AudioQuality.VORBIS_MEDIUM,
|
||||
output_path: Path = Path("./Spotify"),
|
||||
temp_path: Path = Path("./temp"),
|
||||
download_mode: DownloadMode = DownloadMode.YTDLP,
|
||||
@@ -66,7 +66,7 @@ class Downloader:
|
||||
silence: bool = False,
|
||||
):
|
||||
self.spotify_api = spotify_api
|
||||
self.quality = quality
|
||||
self.audio_quality = audio_quality
|
||||
self.output_path = output_path
|
||||
self.temp_path = temp_path
|
||||
self.download_mode = download_mode
|
||||
@@ -333,9 +333,9 @@ class Downloader:
|
||||
def get_audio_file(
|
||||
self,
|
||||
audio_files: list[dict],
|
||||
) -> tuple[Quality, dict] | tuple[None, None]:
|
||||
qualities = list(Quality)
|
||||
start_index = qualities.index(self.quality)
|
||||
) -> tuple[AudioQuality, dict] | tuple[None, None]:
|
||||
qualities = list(AudioQuality)
|
||||
start_index = qualities.index(self.audio_quality)
|
||||
for quality in qualities[start_index:]:
|
||||
for audio_file in audio_files:
|
||||
if audio_file["format"] == QUALITY_X_FORMAT_ID_MAPPING[quality]:
|
||||
@@ -350,12 +350,12 @@ class Downloader:
|
||||
gid = self.spotify_api.media_id_to_gid(media_id)
|
||||
return self.spotify_api.get_gid_metadata(gid, media_type)
|
||||
|
||||
def get_stream_info(
|
||||
def get_stream_info_audio(
|
||||
self,
|
||||
gid_metadata: dict,
|
||||
media_type: str,
|
||||
) -> StreamInfo:
|
||||
stream_info = StreamInfo()
|
||||
) -> StreamInfoAudio:
|
||||
stream_info = StreamInfoAudio()
|
||||
if media_type == "track":
|
||||
audio_files = gid_metadata.get("file")
|
||||
elif media_type == "episode":
|
||||
|
||||
@@ -4,7 +4,7 @@ import logging
|
||||
from pathlib import Path
|
||||
|
||||
from .downloader import Downloader
|
||||
from .models import StreamInfo
|
||||
from .models import StreamInfoAudio
|
||||
|
||||
logger = logging.getLogger("votify")
|
||||
|
||||
@@ -68,7 +68,7 @@ class DownloaderEpisode:
|
||||
episode_metadata: dict = None,
|
||||
show_metadata: dict = None,
|
||||
gid_metadata: dict = None,
|
||||
stream_info: StreamInfo = None,
|
||||
stream_info: StreamInfoAudio = None,
|
||||
playlist_metadata: dict = None,
|
||||
playlist_track: int = None,
|
||||
decryption_key: bytes = None,
|
||||
@@ -86,14 +86,14 @@ class DownloaderEpisode:
|
||||
gid_metadata = self.downloader.get_gid_metadata(episode_id, "episode")
|
||||
if not stream_info:
|
||||
logger.debug("Getting stream info")
|
||||
stream_info = self.downloader.get_stream_info(gid_metadata, "episode")
|
||||
stream_info = self.downloader.get_stream_info_audio(gid_metadata, "episode")
|
||||
if not stream_info.file_id:
|
||||
logger.warning(
|
||||
"Episode is not available on Spotify's "
|
||||
"servers and no alternative found, skipping"
|
||||
)
|
||||
return
|
||||
if stream_info.quality != self.downloader.quality:
|
||||
if stream_info.quality != self.downloader.audio_quality:
|
||||
logger.warning(f"Quality has been changed to {stream_info.quality.value}")
|
||||
tags = self.get_tags(
|
||||
episode_metadata,
|
||||
|
||||
@@ -5,7 +5,7 @@ import logging
|
||||
from pathlib import Path
|
||||
|
||||
from .downloader import Downloader
|
||||
from .models import Lyrics, StreamInfo
|
||||
from .models import Lyrics, StreamInfoAudio
|
||||
|
||||
logger = logging.getLogger("votify")
|
||||
|
||||
@@ -145,7 +145,7 @@ class DownloaderSong:
|
||||
track_metadata: dict = None,
|
||||
album_metadata: dict = None,
|
||||
gid_metadata: dict = None,
|
||||
stream_info: StreamInfo = None,
|
||||
stream_info: StreamInfoAudio = None,
|
||||
playlist_metadata: dict = None,
|
||||
playlist_track: int = None,
|
||||
decryption_key: bytes = None,
|
||||
@@ -163,14 +163,14 @@ class DownloaderSong:
|
||||
gid_metadata = self.downloader.get_gid_metadata(track_id, "track")
|
||||
if not stream_info:
|
||||
logger.debug("Getting stream info")
|
||||
stream_info = self.downloader.get_stream_info(gid_metadata, "track")
|
||||
stream_info = self.downloader.get_stream_info_audio(gid_metadata, "track")
|
||||
if not stream_info.file_id:
|
||||
logger.warning(
|
||||
"Track is not available on Spotify's "
|
||||
"servers and no alternative found, skipping"
|
||||
)
|
||||
return
|
||||
if stream_info.quality != self.downloader.quality:
|
||||
if stream_info.quality != self.downloader.audio_quality:
|
||||
logger.warning(f"Quality has been changed to {stream_info.quality.value}")
|
||||
if gid_metadata.get("has_lyrics"):
|
||||
logger.debug("Getting lyrics")
|
||||
|
||||
+4
-4
@@ -1,10 +1,10 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Quality(Enum):
|
||||
HIGH = "high"
|
||||
MEDIUM = "medium"
|
||||
LOW = "low"
|
||||
class AudioQuality(Enum):
|
||||
VORBIS_HIGH = "high"
|
||||
VORBIS_MEDIUM = "medium"
|
||||
VORBIS_LOW = "low"
|
||||
|
||||
|
||||
class DownloadMode(Enum):
|
||||
|
||||
+3
-3
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .enums import Quality
|
||||
from .enums import AudioQuality
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -26,7 +26,7 @@ class DownloadQueueItem:
|
||||
|
||||
|
||||
@dataclass
|
||||
class StreamInfo:
|
||||
class StreamInfoAudio:
|
||||
stream_url: str = None
|
||||
file_id: str = None
|
||||
quality: Quality = None
|
||||
quality: AudioQuality = None
|
||||
|
||||
Reference in New Issue
Block a user