From 978b7a319ffde44651a11d307f3014598dbdb701 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Thu, 7 May 2026 15:20:52 +0200 Subject: [PATCH] removed comment fix & moved comment fix to sort --- cmd/ripsort.go | 7 --- internal/fixComment.go | 103 ---------------------------------- internal/metadata/metadata.go | 21 +++++++ internal/metadata/vobis.go | 2 + 4 files changed, 23 insertions(+), 110 deletions(-) delete mode 100644 internal/fixComment.go diff --git a/cmd/ripsort.go b/cmd/ripsort.go index cc2b87a..903a765 100644 --- a/cmd/ripsort.go +++ b/cmd/ripsort.go @@ -32,14 +32,9 @@ type NavidromeArgs struct { NavidromePass string `arg:"--navidrome-pass,env:ND_PASS,required"` } -type FixCommentCmd struct { - Path string `arg:"positional,required"` -} - type args struct { Info *InfoCmd `arg:"subcommand:info"` Sort *SortCmd `arg:"subcommand:sort"` - FixCommentTag *FixCommentCmd `arg:"subcommand:fix-comment"` SyncPlaylists *SyncPlaylists `arg:"subcommand:sync-playlist"` Verbose bool `arg:"-v" default:"false"` DryRun bool `arg:"--dry-run" default:"false"` @@ -58,8 +53,6 @@ func Run() { ripsort.Scan(args.Info.File) case args.Sort != nil: ripsort.Sort(args.Sort.Dst, args.Sort.Path) - case args.FixCommentTag != nil: - ripsort.FixComment(args.FixCommentTag.Path, args.DryRun) case args.SyncPlaylists != nil: ripsort.SyncPlaylists( args.SyncPlaylists.SpotifyClientID, diff --git a/internal/fixComment.go b/internal/fixComment.go deleted file mode 100644 index 840f959..0000000 --- a/internal/fixComment.go +++ /dev/null @@ -1,103 +0,0 @@ -package ripsort - -import ( - "io/fs" - "log/slog" - "os" - "path/filepath" - "regexp" - - "git.kapelle.org/niklas/ripsort/internal/metadata" -) - -func FixComment(path string, dry bool) { - - info, err := os.Stat(path) - if err != nil { - slog.Error("Failed to stat path", "file", path, "err", err) - os.Exit(1) - } - - // Handle single file - if !info.IsDir() { - if !fileSupported(path) { - slog.Error("Unsupported file format", "file", path) - os.Exit(1) - } - - err = fixCommentForFile(path, dry) - if err != nil { - slog.Error("Failed to fix comment on file", "file", path, "err", err) - os.Exit(1) - } - - return - } - - // Handle directory - err = filepath.WalkDir(path, func(p string, d fs.DirEntry, err error) error { - if err != nil { - slog.Error("Failed to walk path", "path", path, "file", p, "err", err) - return err - } - - if !d.IsDir() { - if !fileSupported(p) { - return nil - } - err = fixCommentForFile(p, dry) - if err != nil { - slog.Error("Failed to fix comment on file", "file", path, "err", err) - os.Exit(1) - } - - } - - return nil - }) - - if err != nil { - os.Exit(1) - } -} - -func fixCommentForFile(file string, dry bool) error { - - info, err := metadata.ReadAudioTags(file) - if err != nil { - return err - } - - var re = regexp.MustCompile(`(?m)(https:\/\/open\.spotify\.com\/track\/)(.*)`) - - if info.Comment == nil { - slog.Debug("Song does not contain comment", "file", file) - return nil - } - - matches := re.FindAllStringSubmatch(*info.Comment, -1) - - if len(matches) != 1 { - slog.Debug("Song does not match regex", "file", file, "comment", info.Comment) - return nil - } - - id := matches[0][2] - - slog.Debug("Spotify tag found", "file", file, "id", id) - - if dry { - slog.Info("Dryrun: Don't wite id", "file", file, "id", id) - return nil - } - - info.SpotifyID = &id - info.Comment = nil - - err = metadata.UpdateMetadata(file, file, *info) - if err != nil { - return err - } - - return nil -} diff --git a/internal/metadata/metadata.go b/internal/metadata/metadata.go index d861965..2025755 100644 --- a/internal/metadata/metadata.go +++ b/internal/metadata/metadata.go @@ -5,6 +5,7 @@ import ( "log/slog" "os" "path/filepath" + "regexp" "strings" "github.com/dhowden/tag" @@ -62,6 +63,8 @@ func readGenericAudioTags(filePath string) (*Metadata, error) { Comment: &comment, } + commentToSpotifyid(info) + return info, nil } @@ -89,3 +92,21 @@ func UpdateMetadata(src, dst string, m Metadata) error { return nil } + +func commentToSpotifyid(m *Metadata) { + var re = regexp.MustCompile(`(?m)(https:\/\/open\.spotify\.com\/track\/)(.*)`) + + if m.Comment == nil { + return + } + + matches := re.FindAllStringSubmatch(*m.Comment, -1) + + if len(matches) != 1 { + return + } + + id := matches[0][2] + + m.SpotifyID = &id +} diff --git a/internal/metadata/vobis.go b/internal/metadata/vobis.go index a6fc95b..9cd7984 100644 --- a/internal/metadata/vobis.go +++ b/internal/metadata/vobis.go @@ -105,6 +105,8 @@ func readVorbisMetadata(file string) (*Metadata, error) { ISRC: isrc, } + commentToSpotifyid(metadata) + return metadata, nil }