diff --git a/app/api/lyrics.py b/app/api/lyrics.py index 5066ff8..2d9099c 100644 --- a/app/api/lyrics.py +++ b/app/api/lyrics.py @@ -1,6 +1,6 @@ from flask import Blueprint, request -from app.lib.lyrics import get_lyrics +from app.lib.lyrics import get_lyrics, check_lyrics_file, get_lyrics_from_duplicates api = Blueprint("lyrics", __name__, url_prefix="") @@ -13,13 +13,33 @@ def send_lyrics(): data = request.get_json() filepath = data.get("filepath", None) + trackhash = data.get("trackhash", None) - if filepath is None: - return {"error": "No filepath provided"}, 400 + if filepath is None or trackhash is None: + return {"error": "No filepath or trackhash provided"}, 400 lyrics = get_lyrics(filepath) + if lyrics is None: + lyrics = get_lyrics_from_duplicates(trackhash, filepath) + if lyrics is None: return {"error": "No lyrics found"}, 204 return {"lyrics": lyrics}, 200 + + +@api.route("/lyrics/check", methods=["POST"]) +def check_lyrics(): + data = request.get_json() + + filepath = data.get("filepath", None) + trackhash = data.get("trackhash", None) + + if filepath is None or trackhash is None: + return {"error": "No filepath or trackhash provided"}, 400 + + exists, filepath = check_lyrics_file(filepath, trackhash) + + if exists: + return {"filepath": filepath}, 200 diff --git a/app/lib/lyrics.py b/app/lib/lyrics.py index 6f97c0a..acc14f5 100644 --- a/app/lib/lyrics.py +++ b/app/lib/lyrics.py @@ -1,4 +1,5 @@ from pathlib import Path +from app.store.tracks import TrackStore filepath = "/home/cwilvx/Music/Editor's Pick/Bad Day 😢/6 Dogs - Crying in the Rarri.m4a" @@ -12,7 +13,11 @@ def split_line(line: str): def convert_to_milliseconds(time: str): - minutes, seconds = time.split(":") + try: + minutes, seconds = time.split(":") + except ValueError: + return 0 + milliseconds = int(minutes) * 60 * 1000 + float(seconds) * 1000 return int(milliseconds) @@ -27,7 +32,7 @@ def get_lyrics_from_lrc(filepath: str): time, lyric = split_line(line) milliseconds = convert_to_milliseconds(time) - lyrics.append({milliseconds: lyric}) + lyrics.append({"time": milliseconds, "text": lyric}) return lyrics @@ -42,6 +47,18 @@ def get_lyrics_file_rel_to_track(filepath: str): return lyrics_path +def check_lyrics_file_rel_to_track(filepath: str): + """ + Checks if the lyrics file exists relative to the track file + """ + lyrics_path = Path(filepath).with_suffix(".lrc") + + if lyrics_path.exists(): + return True + else: + return False + + def get_lyrics(track_path: str): """ Gets the lyrics for a track @@ -54,4 +71,30 @@ def get_lyrics(track_path: str): return None -get_lyrics(filepath) +def get_lyrics_from_duplicates(trackhash: str, filepath: str): + """ + Finds the lyrics from other duplicate tracks + """ + + for track in TrackStore.tracks: + if track.trackhash == trackhash and track.filepath != filepath: + lyrics = get_lyrics(track.filepath) + + if lyrics: + return lyrics + + +def check_lyrics_file(filepath: str, trackhash: str): + lyrics_exists = check_lyrics_file_rel_to_track(filepath) + + if lyrics_exists: + return True, filepath + + for track in TrackStore.tracks: + if track.trackhash == trackhash and track.filepath != filepath: + lyrics_exists = check_lyrics_file_rel_to_track(track.filepath) + + if lyrics_exists: + return True, track.filepath + + return False, None