Various scripts that I maintain

lyricfetch: Implement database

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

Changed files
+55 -9
scripts
+55 -9
scripts/lyricfetch.nu
··· 5 5 def main [ 6 6 --library (-l): path = ("~/Music" | path expand) 7 7 --dumb-instance (-d): string = "https://dumb.hyperreal.coffee" 8 + --db-path: path = ("~/.local/share/lyricfetch.nuon" | path expand) 8 9 --providers (-p): list = [dumb] 9 - --update (-u) # Overwrite existing lyrics 10 10 --minimal_screen (-m) # Clear the screen before each track 11 - --clear # Clears all existing lyrics from your library 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 12 14 ] { 13 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 + 14 34 print "Indexing compatible files in library" 15 35 let files = fd -g '*.flac' $library | lines 16 36 print $"(ansi blue)($files | length)(ansi reset) compatible files found" ··· 23 43 return null 24 44 } 25 45 26 - $files | each {|file| 46 + mut autosave = 0 47 + 48 + for file in $files { 49 + $autosave += 1 27 50 if $minimal_screen {print (ansi clear_entire_screen) ; print (ansi cursor_home)} 28 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" 29 71 let meta = metaflac $file --show-all-tags 30 72 | parse "{key}={value}" 31 73 | reduce --fold {} {|it, acc| $acc | upsert ($it.key | str downcase) $it.value} 32 74 33 75 if ($meta.artist? == null) or ($meta.title? == null) { 34 76 print $"(ansi yellow)`($file)`(ansi red) doesn't contain artist and title tags, cannot search for lyrics(ansi reset)" 35 - return 77 + continue 36 78 } 37 79 38 - if ($meta.lyrics? | is-not-empty) and not $update { 39 - log debug $"($meta.artist) – ($meta.title) by already contains lyrics, skipping" 40 - return 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 41 89 } 42 90 43 91 if not $minimal_screen {print} ··· 53 101 print $"Updated lyrics for (ansi green)($meta.artist) – ($meta.title)(ansi reset)" 54 102 } 55 103 } 56 - 57 - return null 58 104 } 59 105 60 106 def "fetch-lyrics dumb" [