Various scripts that I maintain

lyricfetch: Implement database

Signed-off-by: Shiloh Fen <shiloh@shilohfen.com>

+55 -9
+55 -9
scripts/lyricfetch.nu
··· 5 def main [ 6 --library (-l): path = ("~/Music" | path expand) 7 --dumb-instance (-d): string = "https://dumb.hyperreal.coffee" 8 --providers (-p): list = [dumb] 9 - --update (-u) # Overwrite existing lyrics 10 --minimal_screen (-m) # Clear the screen before each track 11 - --clear # Clears all existing lyrics from your library 12 ] { 13 if $minimal_screen {print (ansi clear_entire_screen) ; print (ansi cursor_home)} 14 print "Indexing compatible files in library" 15 let files = fd -g '*.flac' $library | lines 16 print $"(ansi blue)($files | length)(ansi reset) compatible files found" ··· 23 return null 24 } 25 26 - $files | each {|file| 27 if $minimal_screen {print (ansi clear_entire_screen) ; print (ansi cursor_home)} 28 29 let meta = metaflac $file --show-all-tags 30 | parse "{key}={value}" 31 | reduce --fold {} {|it, acc| $acc | upsert ($it.key | str downcase) $it.value} 32 33 if ($meta.artist? == null) or ($meta.title? == null) { 34 print $"(ansi yellow)`($file)`(ansi red) doesn't contain artist and title tags, cannot search for lyrics(ansi reset)" 35 - return 36 } 37 38 - if ($meta.lyrics? | is-not-empty) and not $update { 39 - log debug $"($meta.artist) – ($meta.title) by already contains lyrics, skipping" 40 - return 41 } 42 43 if not $minimal_screen {print} ··· 53 print $"Updated lyrics for (ansi green)($meta.artist) – ($meta.title)(ansi reset)" 54 } 55 } 56 - 57 - return null 58 } 59 60 def "fetch-lyrics dumb" [
··· 5 def main [ 6 --library (-l): path = ("~/Music" | path expand) 7 --dumb-instance (-d): string = "https://dumb.hyperreal.coffee" 8 + --db-path: path = ("~/.local/share/lyricfetch.nuon" | path expand) 9 --providers (-p): list = [dumb] 10 --minimal_screen (-m) # Clear the screen before each track 11 + --clear # Clear all existing lyrics from your library 12 + --reset # Start with a fresh database 13 + --ignore-if-existing (-e) # Skip updating lyrics on tracks that already have lyrics 14 ] { 15 if $minimal_screen {print (ansi clear_entire_screen) ; print (ansi cursor_home)} 16 + 17 + print "Initializing Database" 18 + mut db = if ($db_path | path exists) or $reset { 19 + open $db_path 20 + } else { 21 + { 22 + _METADATA: { 23 + version: 1 24 + } 25 + } 26 + } 27 + 28 + if $db._METADATA.version >= 2 { 29 + error make { 30 + msg: "Incompatible database version" 31 + } 32 + } 33 + 34 print "Indexing compatible files in library" 35 let files = fd -g '*.flac' $library | lines 36 print $"(ansi blue)($files | length)(ansi reset) compatible files found" ··· 43 return null 44 } 45 46 + mut autosave = 0 47 + 48 + for file in $files { 49 + $autosave += 1 50 if $minimal_screen {print (ansi clear_entire_screen) ; print (ansi cursor_home)} 51 52 + log debug "Performing database operations" 53 + let this = if $file in $db { 54 + $db | get $file 55 + } else { 56 + last_update: (0 | into datetime) 57 + } 58 + 59 + $db = $db | merge { 60 + $file: { 61 + last_update: (date now) 62 + } 63 + } 64 + 65 + if $autosave == 5 { 66 + $db | save -f $db_path 67 + $autosave = 0 68 + } 69 + 70 + log debug "Getting file tags" 71 let meta = metaflac $file --show-all-tags 72 | parse "{key}={value}" 73 | reduce --fold {} {|it, acc| $acc | upsert ($it.key | str downcase) $it.value} 74 75 if ($meta.artist? == null) or ($meta.title? == null) { 76 print $"(ansi yellow)`($file)`(ansi red) doesn't contain artist and title tags, cannot search for lyrics(ansi reset)" 77 + continue 78 } 79 80 + log debug "Performing checks" 81 + if ((date now) - $this.last_update) < 60day { 82 + print $"($meta.artist) – ($meta.title) was updated in the last 60 days, skipping" 83 + continue 84 + } 85 + 86 + if ($meta.lyrics? | is-not-empty) and $ignore_if_existing { 87 + log debug $"($meta.artist) – ($meta.title) already contains lyrics, skipping" 88 + continue 89 } 90 91 if not $minimal_screen {print} ··· 101 print $"Updated lyrics for (ansi green)($meta.artist) – ($meta.title)(ansi reset)" 102 } 103 } 104 } 105 106 def "fetch-lyrics dumb" [