Skip to content

Commit

Permalink
Do not overwrite existing lyrics
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
jgoguen committed Jul 21, 2024
1 parent cae5b6b commit 65b72da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
### Lyrics getter

Huge thanks to https://github.com/tranxuanthang/lrclib ofc.
Huge thanks to <https://github.com/tranxuanthang/lrclib> ofc.

Uses lrclib.net to get lyrics for my Jellyfin library. Does /get, if unavailable tried to do /search

Is very much dependant on having the Jellyfin suggested music library structure. (Artist/Album/Song).

To run go `lyricsrs <music_directory>` or clone the repo and `cargo run <music_directory>`.

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.

35 changes: 31 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,32 @@ 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<AtomicUsize>,
) {
) -> String {
let mut parent_dir = PathBuf::new();
parent_dir.push(music_dir);
parent_dir.push(artist_dir);
parent_dir.push(album_dir);

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<AtomicUsize>,
) {
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
Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 65b72da

Please sign in to comment.