From 65b72da247b41be0eef697874134b33050a258b2 Mon Sep 17 00:00:00 2001 From: Joel Goguen Date: Sun, 21 Jul 2024 17:14:41 -0400 Subject: [PATCH] Do not overwrite existing lyrics Instead of overwriting the lyrics file, which may have been locally modified after download, skip processing if the lyrics file is already present. ``` Skipping 06 Vengeance Venom, lyrics file exists: /opt/docker-data/jellyfin/media/music/Leaves' Eyes/King of Kings (Deluxe Version)/06 Vengeance Venom.lrc [exact_search] requesting: http://lrclib.net/api/get?track_name=09+Haraldskv%C3%A6di&artist_name=Leaves%27+Eyes&album_name=King+of+Kings+%28Deluxe+Version%29&duration=204 ``` --- README.md | 5 ++--- src/main.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f398b03..10d009f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ### Lyrics getter -Huge thanks to https://github.com/tranxuanthang/lrclib ofc. +Huge thanks to ofc. Uses lrclib.net to get lyrics for my Jellyfin library. Does /get, if unavailable tried to do /search @@ -8,7 +8,6 @@ Is very much dependant on having the Jellyfin suggested music library structure. To run go `lyricsrs ` or clone the repo and `cargo run `. -Will overwrite any .lrc files you already have with the existing name. +Will not overwrite any .lrc files you already have with the existing name. Only does synced lyrics because they are cool. - diff --git a/src/main.rs b/src/main.rs index 55e9948..bc75310 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,14 +147,12 @@ fn get_audio_duration(file_path: &PathBuf) -> Duration { tagged_file.properties().duration() } -async fn save_synced_lyrics( +fn lyrics_file_name( music_dir: &Path, artist_dir: &Path, album_dir: &Path, song_name: &String, - synced_lyrics: String, - successful_count: Arc, -) { +) -> String { let mut parent_dir = PathBuf::new(); parent_dir.push(music_dir); parent_dir.push(artist_dir); @@ -162,6 +160,19 @@ async fn save_synced_lyrics( let file_path = format!("{}/{}.lrc", parent_dir.to_string_lossy(), song_name); + return file_path; +} + +async fn save_synced_lyrics( + music_dir: &Path, + artist_dir: &Path, + album_dir: &Path, + song_name: &String, + synced_lyrics: String, + successful_count: Arc, +) { + let file_path = lyrics_file_name(music_dir, artist_dir, album_dir, song_name); + // Create a new file or overwrite existing one let mut file = File::create(&file_path) .await @@ -204,6 +215,14 @@ async fn exact_search( .expect("invalid file_path") .to_string_lossy() .into_owned(); + + // Skip if the lyrics file already exists + let lyrics_path = lyrics_file_name(music_dir, artist_dir, album_dir, &song_name); + if Path::new(&lyrics_path).exists() { + println!("Skipping {}, lyrics file exists: {}", song_name, lyrics_path); + return; + } + let clean_song = remove_numbered_prefix(&song_name); let mut full_path = PathBuf::from(""); @@ -311,6 +330,14 @@ async fn fuzzy_search( .expect("invalid file_path") .to_string_lossy() .into_owned(); + + // Skip if the lyrics file already exists + let lyrics_path = lyrics_file_name(music_dir, artist_dir, album_dir, &song_name); + if Path::new(&lyrics_path).exists() { + println!("Skipping {}, lyrics file exists: {}", song_name, lyrics_path); + return; + } + let clean_song = remove_numbered_prefix(&song_name); let mut url = "http://lrclib.net/api/search?q=".to_string();