mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-06 03:05:35 +00:00
add methods to open lyric files
+ add api endpoints to check and get lyrics
This commit is contained in:
parent
5fb465c921
commit
2321288be0
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user