Reorganize metadata extractors code
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package taglib
|
||||
|
||||
import (
|
||||
"github.com/navidrome/navidrome/log"
|
||||
)
|
||||
|
||||
type Parser struct{}
|
||||
|
||||
type parsedTags = map[string][]string
|
||||
|
||||
func (e *Parser) Parse(paths ...string) (map[string]parsedTags, error) {
|
||||
fileTags := map[string]parsedTags{}
|
||||
for _, path := range paths {
|
||||
tags, err := e.extractMetadata(path)
|
||||
if err == nil {
|
||||
fileTags[path] = tags
|
||||
}
|
||||
}
|
||||
return fileTags, nil
|
||||
}
|
||||
|
||||
func (e *Parser) extractMetadata(filePath string) (parsedTags, error) {
|
||||
tags, err := Read(filePath)
|
||||
if err != nil {
|
||||
log.Warn("Error reading metadata from file. Skipping", "filePath", filePath, err)
|
||||
}
|
||||
|
||||
alternativeTags := map[string][]string{
|
||||
"title": {"titlesort"},
|
||||
"album": {"albumsort"},
|
||||
"artist": {"artistsort"},
|
||||
"tracknumber": {"trck", "_track"},
|
||||
}
|
||||
|
||||
for tagName, alternatives := range alternativeTags {
|
||||
for _, altName := range alternatives {
|
||||
if altValue, ok := tags[altName]; ok {
|
||||
tags[tagName] = append(tags[tagName], altValue...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return tags, nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package taglib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/tests"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestTagLib(t *testing.T) {
|
||||
tests.Init(t, true)
|
||||
log.SetLevel(log.LevelCritical)
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "TagLib Suite")
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package taglib
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Parser", func() {
|
||||
var e *Parser
|
||||
BeforeEach(func() {
|
||||
e = &Parser{}
|
||||
})
|
||||
Context("Parse", func() {
|
||||
It("correctly parses metadata from all files in folder", func() {
|
||||
mds, err := e.Parse("tests/fixtures/test.mp3", "tests/fixtures/test.ogg")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(mds).To(HaveLen(2))
|
||||
|
||||
m := mds["tests/fixtures/test.mp3"]
|
||||
Expect(m).To(HaveKeyWithValue("title", []string{"Song", "Song"}))
|
||||
Expect(m).To(HaveKeyWithValue("album", []string{"Album", "Album"}))
|
||||
Expect(m).To(HaveKeyWithValue("artist", []string{"Artist", "Artist"}))
|
||||
Expect(m).To(HaveKeyWithValue("albumartist", []string{"Album Artist"}))
|
||||
Expect(m).To(HaveKeyWithValue("tcmp", []string{"1"})) // Compilation
|
||||
Expect(m).To(HaveKeyWithValue("genre", []string{"Rock"}))
|
||||
Expect(m).To(HaveKeyWithValue("date", []string{"2014", "2014"}))
|
||||
Expect(m).To(HaveKeyWithValue("tracknumber", []string{"2/10", "2/10", "2"}))
|
||||
Expect(m).To(HaveKeyWithValue("discnumber", []string{"1/2"}))
|
||||
Expect(m).To(HaveKeyWithValue("has_picture", []string{"true"}))
|
||||
Expect(m).To(HaveKeyWithValue("duration", []string{"1"}))
|
||||
Expect(m).To(HaveKeyWithValue("bitrate", []string{"192"}))
|
||||
Expect(m).To(HaveKeyWithValue("comment", []string{"Comment1\nComment2"}))
|
||||
Expect(m).To(HaveKeyWithValue("lyrics", []string{"Lyrics 1\rLyrics 2"}))
|
||||
Expect(m).To(HaveKeyWithValue("bpm", []string{"123"}))
|
||||
|
||||
m = mds["tests/fixtures/test.ogg"]
|
||||
Expect(err).To(BeNil())
|
||||
Expect(m).ToNot(HaveKey("title"))
|
||||
Expect(m).ToNot(HaveKey("has_picture"))
|
||||
Expect(m).To(HaveKeyWithValue("duration", []string{"1"}))
|
||||
Expect(m).To(HaveKeyWithValue("fbpm", []string{"141.7"}))
|
||||
|
||||
// TabLib 1.12 returns 18, previous versions return 39.
|
||||
// See https://github.com/taglib/taglib/commit/2f238921824741b2cfe6fbfbfc9701d9827ab06b
|
||||
Expect(m).To(HaveKey("bitrate"))
|
||||
Expect(m["bitrate"][0]).To(BeElementOf("18", "39"))
|
||||
})
|
||||
})
|
||||
})
|
||||
+5
-5
@@ -13,7 +13,7 @@
|
||||
#include <tpropertymap.h>
|
||||
#include <vorbisfile.h>
|
||||
|
||||
#include "taglib_parser.h"
|
||||
#include "taglib_wrapper.h"
|
||||
|
||||
char has_cover(const TagLib::FileRef f);
|
||||
|
||||
@@ -39,16 +39,16 @@ int taglib_read(const char *filename, unsigned long id) {
|
||||
TagLib::Tag *basic = f.file()->tag();
|
||||
if (!basic->isEmpty()) {
|
||||
if (!basic->title().isEmpty()) {
|
||||
tags.insert("_title", basic->title());
|
||||
tags.insert("title", basic->title());
|
||||
}
|
||||
if (!basic->artist().isEmpty()) {
|
||||
tags.insert("_artist", basic->artist());
|
||||
tags.insert("artist", basic->artist());
|
||||
}
|
||||
if (!basic->album().isEmpty()) {
|
||||
tags.insert("_album", basic->album());
|
||||
tags.insert("album", basic->album());
|
||||
}
|
||||
if (basic->year() > 0) {
|
||||
tags.insert("_year", TagLib::String::number(basic->year()));
|
||||
tags.insert("date", TagLib::String::number(basic->year()));
|
||||
}
|
||||
if (basic->track() > 0) {
|
||||
tags.insert("_track", TagLib::String::number(basic->track()));
|
||||
+1
-1
@@ -7,7 +7,7 @@ package taglib
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "taglib_parser.h"
|
||||
#include "taglib_wrapper.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
Reference in New Issue
Block a user