Skip to content

Commit

Permalink
✨ add metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
oskvr37 committed Aug 3, 2024
1 parent 25b035e commit e42ae34
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests>=2.20.0
requests>=2.20.0
mutagen>=1.47.0
17 changes: 14 additions & 3 deletions tiddl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import time
import logging

from random import randint

from .api import TidalApi
Expand All @@ -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():
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions tiddl/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ def downloadTrackStream(
else:
track_data = threadDownload(track_urls)

logger.debug(f"codecs: {codecs}")

"""
known codecs
flac (master)
Expand All @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions tiddl/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()

0 comments on commit e42ae34

Please sign in to comment.