Various scripts that I maintain
1#!/usr/bin/nu
2
3def main [
4 --noconfirm (-y)
5] {
6 const library = "~/Music" | path expand
7 print $"Library path set to (ansi blue)($library)(ansi reset)"
8
9 if (which metaflac | is-empty) {
10 error make {
11 msg: "Could not find metaflac."
12 help: "Install the FLAC utils."
13 }
14 }
15 if (which fd | is-empty) {
16 error make {
17 msg: "Could not find fd"
18 help: "Install fd-find."
19 }
20 }
21
22 print "Indexing Library..."
23 let tracks = fd -g '*.flac' $library | lines
24 print $"Found (ansi green)($tracks | length)(ansi reset) supported tracks in this library."
25
26 for track in $tracks {
27 let meta = metaflac $track --show-all-tags
28 | parse "{key}={value}"
29 | reduce --fold {} {|it, acc| $acc | upsert ($it.key | str downcase) $it.value}
30
31 if ($meta.artist? == null) or ($meta.title? == null) {
32 print $"(ansi red)($track)(ansi yellow) doesn't contain artist and title tags, cannot search for lyrics. Skipping.(ansi reset)"
33 continue
34 }
35
36 print $"\n<- (ansi blue)($meta.title)(ansi reset) by (ansi purple)($meta.artist)(ansi reset) ->"
37
38 if ($meta.lyrics? | is-not-empty) {
39 print $"(ansi green)Track already contains lyrics. Skipping.(ansi reset)"
40 continue
41 } else if ($meta.unsyncedlyrics? | is-not-empty) {
42 print -n $"Track already contains unsynced lyrics. Would you like to copy them to the lyrics tag? [Y/n]: "
43 match (input) {
44 "n" | "N" => (print "Not copying unsynced lyrics, continuing.")
45 _ => {
46 print "Copying unsynced lyrics to lyrics tag..."
47 metaflac $track --set-tag $"LYRICS=($meta.unsyncedlyrics)"
48 continue
49 }
50 }
51 }
52
53 print $"Fetching lyrics..."
54 let info = try {
55 http get $"https://lyrics.astrid.sh/api/search?q=($meta.artist) ($meta.title)"
56 } catch {
57 print $"(ansi yellow)Could not find lyrics, skipping.(ansi reset)"
58 continue
59 }
60
61 let lyrics = if ($info.lyrics | is-empty) {
62 print $"(ansi yellow)Could not automatically fetch lyrics.(ansi reset)"
63 print -n $"Please manually fetch the lyrics from (ansi default_dimmed)($info.url | ansi link)(ansi reset), then paste them here \(enter to skip\):"
64 let input = input
65 if ($input | is-empty) {
66 print $"(ansi yellow)Manually skipped.(ansi reset)"
67 continue
68 } else {
69 $input
70 }
71 } else {
72 print "Successfully fetched lyrics."
73 $info.lyrics
74 }
75
76 print $"(ansi green_dimmed)<========>(ansi reset)\n(ansi green)($lyrics)(ansi reset)\n(ansi green_dimmed)<========>(ansi reset)"
77 print -n $"\nAre the above lyrics correct? [Y/n]: "
78 match (input) {
79 "n" | "N" => {
80 print $"(ansi yellow)Manually skipped.(ansi reset)"
81 continue
82 }
83 _ => {
84 print "Writing lyrics to track metadata..."
85 metaflac $track --set-tag $"LYRICS=($lyrics)"
86 }
87 }
88 }
89}
90