From 2f8b78eaec669d9c13596d59de19eefbd8ac2c68 Mon Sep 17 00:00:00 2001 From: sentriz Date: Wed, 12 Jun 2019 18:40:55 +0100 Subject: [PATCH] move tags to their own package --- scanner/scanner.go | 23 ++++++++-------- scanner/tags.go | 56 --------------------------------------- scanner/tags/tags.go | 41 ++++++++++++++++++++++++++++ scanner/tags/utilities.go | 18 +++++++++++++ 4 files changed, 71 insertions(+), 67 deletions(-) delete mode 100644 scanner/tags.go create mode 100644 scanner/tags/tags.go create mode 100644 scanner/tags/utilities.go diff --git a/scanner/scanner.go b/scanner/scanner.go index 62b828a..a57ac0a 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -15,6 +15,7 @@ import ( "github.com/sentriz/gonic/mime" "github.com/sentriz/gonic/model" + "github.com/sentriz/gonic/scanner/tags" ) var ( @@ -249,7 +250,7 @@ func (s *Scanner) handleTrack(it *item) error { track.Filename = it.filename track.Size = int(it.stat.Size()) track.AlbumID = s.curFolderID() - tags, err := readTags(it.fullPath) + trTags, err := tags.New(it.fullPath) if err != nil { // not returning the error here because we don't // want the entire walk to stop if we can't read @@ -258,21 +259,21 @@ func (s *Scanner) handleTrack(it *item) error { s.seenTracksErr++ return nil } - track.TagTitle = tags.Title() - track.TagTrackArtist = tags.Artist() - track.TagTrackNumber = tags.TrackNumber() - track.TagDiscNumber = tags.DiscNumber() - track.Length = tags.Length() // these two should be calculated - track.Bitrate = tags.Bitrate() // from the file instead of tags + track.TagTitle = trTags.Title() + track.TagTrackArtist = trTags.Artist() + track.TagTrackNumber = trTags.TrackNumber() + track.TagDiscNumber = trTags.DiscNumber() + track.Length = trTags.Length() // these two should be calculated + track.Bitrate = trTags.Bitrate() // from the file instead of tags // // set album artist basics artist := &model.Artist{} err = s.tx. - Where("name = ?", tags.AlbumArtist()). + Where("name = ?", trTags.AlbumArtist()). First(artist). Error if gorm.IsRecordNotFoundError(err) { - artist.Name = tags.AlbumArtist() + artist.Name = trTags.AlbumArtist() s.tx.Save(artist) } track.ArtistID = artist.ID @@ -286,8 +287,8 @@ func (s *Scanner) handleTrack(it *item) error { // the folder hasn't been modified or already has it's tags return nil } - folder.TagTitle = tags.Album() - folder.TagYear = tags.Year() + folder.TagTitle = trTags.Album() + folder.TagYear = trTags.Year() folder.TagArtistID = artist.ID folder.ReceivedTags = true return nil diff --git a/scanner/tags.go b/scanner/tags.go deleted file mode 100644 index e75d417..0000000 --- a/scanner/tags.go +++ /dev/null @@ -1,56 +0,0 @@ -package scanner - -import ( - "strconv" - "strings" - - "github.com/nicksellen/audiotags" - "github.com/pkg/errors" -) - -type tags struct { - map_ map[string]string - props *audiotags.AudioProperties -} - -func readTags(path string) (*tags, error) { - map_, props, err := audiotags.Read(path) - if err != nil { - return nil, errors.Wrap(err, "audiotags module") - } - return &tags{ - map_: map_, - props: props, - }, nil -} - -func (t *tags) firstTag(keys ...string) string { - for _, key := range keys { - if val, ok := t.map_[key]; ok { - return val - } - } - return "" -} - -func intSep(in, sep string) int { - if in == "" { - return 0 - } - start := strings.SplitN(in, sep, 2)[0] - out, err := strconv.Atoi(start) - if err != nil { - return 0 - } - return out -} - -func (t *tags) Title() string { return t.firstTag("title") } -func (t *tags) Artist() string { return t.firstTag("artist") } -func (t *tags) Album() string { return t.firstTag("album") } -func (t *tags) AlbumArtist() string { return t.firstTag("albumartist", "album artist") } -func (t *tags) Year() int { return intSep(t.firstTag("date", "year"), "-") } // eg. 2019-6-11 -func (t *tags) TrackNumber() int { return intSep(t.firstTag("tracknumber"), "/") } // eg. 5/12 -func (t *tags) DiscNumber() int { return intSep(t.firstTag("discnumber"), "/") } // eg. 1/2 -func (t *tags) Length() int { return t.props.Length } -func (t *tags) Bitrate() int { return t.props.Bitrate } diff --git a/scanner/tags/tags.go b/scanner/tags/tags.go new file mode 100644 index 0000000..7ab0643 --- /dev/null +++ b/scanner/tags/tags.go @@ -0,0 +1,41 @@ +package tags + +import ( + "github.com/nicksellen/audiotags" + "github.com/pkg/errors" +) + +type Tags struct { + raw map[string]string + props *audiotags.AudioProperties +} + +func New(path string) (*Tags, error) { + raw, props, err := audiotags.Read(path) + if err != nil { + return nil, errors.Wrap(err, "audiotags module") + } + return &Tags{ + raw: raw, + props: props, + }, nil +} + +func (t *Tags) firstTag(keys ...string) string { + for _, key := range keys { + if val, ok := t.raw[key]; ok { + return val + } + } + return "" +} + +func (t *Tags) Title() string { return t.firstTag("title") } +func (t *Tags) Artist() string { return t.firstTag("artist") } +func (t *Tags) Album() string { return t.firstTag("album") } +func (t *Tags) AlbumArtist() string { return t.firstTag("albumartist", "album artist") } +func (t *Tags) Year() int { return intSep(t.firstTag("date", "year"), "-") } // eg. 2019-6-11 +func (t *Tags) TrackNumber() int { return intSep(t.firstTag("tracknumber"), "/") } // eg. 5/12 +func (t *Tags) DiscNumber() int { return intSep(t.firstTag("discnumber"), "/") } // eg. 1/2 +func (t *Tags) Length() int { return t.props.Length } +func (t *Tags) Bitrate() int { return t.props.Bitrate } diff --git a/scanner/tags/utilities.go b/scanner/tags/utilities.go new file mode 100644 index 0000000..70090fe --- /dev/null +++ b/scanner/tags/utilities.go @@ -0,0 +1,18 @@ +package tags + +import ( + "strconv" + "strings" +) + +func intSep(in, sep string) int { + if in == "" { + return 0 + } + start := strings.SplitN(in, sep, 2)[0] + out, err := strconv.Atoi(start) + if err != nil { + return 0 + } + return out +}