From e42ae3405f8149d4ba9d6b51a5e940a78135a9eb Mon Sep 17 00:00:00 2001 From: oskvr37 Date: Sat, 3 Aug 2024 20:08:24 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20metadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 ++- tiddl/__init__.py | 17 ++++++++++++++--- tiddl/download.py | 21 +++++++++++++++++---- tiddl/utils.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1480256..32b03c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests>=2.20.0 \ No newline at end of file +requests>=2.20.0 +mutagen>=1.47.0 \ No newline at end of file diff --git a/tiddl/__init__.py b/tiddl/__init__.py index 1b4f516..a8026d6 100644 --- a/tiddl/__init__.py +++ b/tiddl/__init__.py @@ -1,7 +1,6 @@ import os import time import logging - from random import randint from .api import TidalApi @@ -10,7 +9,14 @@ from .download import downloadTrackStream from .parser import QUALITY_ARGS, parser from .types import TRACK_QUALITY, TrackQuality, Track -from .utils import RESOURCE, parseURL, formatFilename, sanitizeDirName, loadingSymbol +from .utils import ( + RESOURCE, + parseURL, + formatFilename, + sanitizeDirName, + loadingSymbol, + setMetadata, +) def main(): @@ -139,7 +145,7 @@ def main(): days, hours = time_to_expire // (24 * 3600), time_to_expire % (24 * 3600) // 3600 days_text = f" {days} {'day' if days == 1 else 'days'}" if days else "" hours_text = f" {hours} {'hour' if hours == 1 else 'hours'}" if hours else "" - logger.info(f"token expires in{days_text}{hours_text}") + logger.debug(f"token expires in{days_text}{hours_text}") user_input: str = args.input @@ -199,6 +205,11 @@ def downloadTrack(track: Track, skip_existing=True, sleep=False): logger.info(f"track saved in {track_path}") + try: + setMetadata(track_path, track) + except Exception as e: + logger.error(e) + def downloadAlbum(album_id: str | int, skip_existing: bool): # i dont know if limit 100 is suspicious # but i will leave it here diff --git a/tiddl/download.py b/tiddl/download.py index aad548d..0c6425a 100644 --- a/tiddl/download.py +++ b/tiddl/download.py @@ -133,8 +133,6 @@ def downloadTrackStream( else: track_data = threadDownload(track_urls) - logger.debug(f"codecs: {codecs}") - """ known codecs flac (master) @@ -144,9 +142,24 @@ def downloadTrackStream( # TODO: use proper file extension ✨ + # quick fix for file extension + + if codecs is None: + raise Exception("Missing codecs") + + if codecs == "flac": + extension = "flac" + elif codecs.startswith("mp4a"): + extension = "m4a" + else: + extension = "flac" + logger.warning( + f'unknown file codecs: "{codecs}", please submit this as issue on GitHub' + ) + file_path = os.path.dirname(full_path) - file_name = f"{full_path}.flac" - logger.debug(f"file_path:{file_path}, file_name: {file_name}") + file_name = f"{full_path}.{extension}" + logger.debug(f"file_path: {file_path}, file_name: {file_name}") os.makedirs(file_path, exist_ok=True) diff --git a/tiddl/utils.py b/tiddl/utils.py index 8838603..c1eb723 100644 --- a/tiddl/utils.py +++ b/tiddl/utils.py @@ -1,6 +1,9 @@ import re +import os from typing import TypedDict, Literal, List, get_args +from mutagen.flac import FLAC as MutagenFLAC +from mutagen.easymp4 import EasyMP4 as MutagenMP4 from .types.track import Track @@ -67,3 +70,29 @@ def loadingSymbol(i: int, text: str): symbols = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] symbol = symbols[i % len(symbols)] print(f"\r{text} {symbol}", end="\r") + + +def setMetadata(file_path: str, track: Track): + _, extension = os.path.splitext(file_path) + + if extension == ".flac": + metadata = MutagenFLAC(file_path) + elif extension == ".m4a": + metadata = MutagenMP4(file_path) + else: + raise ValueError(f"Unknown file extension: {extension}") + + # TODO: add `audioQuality` and other special tags ✨ + + new_metadata = { + # "id": str(track["id"]), + "title": track["title"], + "trackNumber": str(track["trackNumber"]), + "copyright": track["copyright"], + # "audioQuality": track["audioQuality"], + "artist": track["artist"]["name"], + "album": track["album"]["title"], + } + + metadata.update(new_metadata) + metadata.save()