From 3125d14d230d4445f0fb3f971c582299526db02d Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 25 Feb 2020 23:23:03 -0500 Subject: [PATCH] Move track information into struct This adds a NewTrack func that creates the struct, and we now use this in RenameTrack(). --- sanitize.go | 36 ++++++++++++++++++++++++------------ track.go | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 track.go diff --git a/sanitize.go b/sanitize.go index a7be5bb..8df5454 100644 --- a/sanitize.go +++ b/sanitize.go @@ -13,29 +13,41 @@ import ( var trackNameReg = regexp.MustCompile("^([0-9]{2}).+") -// RenameTrack takes a filename, opens it, reads the metadata, and returns both -// the old and new filename. -func RenameTrack(file string) string { +func NewTrack(file string) *Track { f, err := os.Open(file) if err != nil { fmt.Printf("error loading file: %v", err) - return "" + return nil } defer f.Close() - fName := f.Name() - fMatch := trackNameReg.FindStringSubmatch(fName) + m, err := tag.ReadFrom(f) + if err != nil { + return nil + } + + return &Track{ + Title: m.Title(), + Artist: m.Artist(), + Filename: f.Name(), + } +} + +// RenameTrack takes a filename, opens it, reads the metadata, and returns both +// the old and new filename. +func RenameTrack(file string) string { + t := NewTrack(file) + + // Extract playlist track number from filename + fMatch := trackNameReg.FindStringSubmatch(t.Filename) if len(fMatch) < 2 { log.Fatal("Unexpect filename format") } trackNum := fMatch[1] - ext := fName[strings.LastIndex(fName, "."):] - m, err := tag.ReadFrom(f) - if err != nil { - log.Fatal(err) - } - return fmt.Sprintf("%s-%s-%s%s", trackNum, Sanitize(m.Artist()), Sanitize(m.Title()), ext) + ext := t.Filename[strings.LastIndex(t.Filename, "."):] + + return fmt.Sprintf("%s-%s-%s%s", trackNum, Sanitize(t.Artist), Sanitize(t.Title), ext) } // Sanitize takes a string and removes problematic characters from it. diff --git a/track.go b/track.go new file mode 100644 index 0000000..771e743 --- /dev/null +++ b/track.go @@ -0,0 +1,14 @@ +package cdr + +type ( + Mixtape struct { + Tracks []Track + } + + Track struct { + Title string + Artist string + + Filename string + } +)