mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-18 21:07:44 +03:00
Implemented proper index grouping
This commit is contained in:
parent
9d6eb40f6f
commit
c64a0f8242
@ -8,7 +8,7 @@ apiVersion = 1.0.0
|
||||
ignoredArticles="The El La Los Las Le Les Os As O A"
|
||||
indexGroups=A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ)
|
||||
|
||||
musicFolder=./iTunes.xml
|
||||
musicFolder=./iTunesFull.xml
|
||||
user=deluan
|
||||
password=wordpass
|
||||
dbPath = ./devDb
|
||||
|
@ -3,5 +3,4 @@ package models
|
||||
type Artist struct {
|
||||
Id string
|
||||
Name string
|
||||
Albums map[string]bool
|
||||
}
|
@ -15,7 +15,7 @@ type Scanner interface {
|
||||
LoadFolder(path string) []Track
|
||||
}
|
||||
|
||||
type tempIndex map[string]*models.ArtistInfo
|
||||
type tempIndex map[string]models.ArtistInfo
|
||||
|
||||
// TODO Implement a flag 'isScanning'.
|
||||
func StartImport() {
|
||||
@ -30,6 +30,7 @@ func doImport(mediaFolder string, scanner Scanner) {
|
||||
}
|
||||
|
||||
func importLibrary(files []Track) (err error){
|
||||
indexGroups := utils.ParseIndexGroups(beego.AppConfig.String("indexGroups"))
|
||||
mfRepo := repositories.NewMediaFileRepository()
|
||||
albumRepo := repositories.NewAlbumRepository()
|
||||
artistRepo := repositories.NewArtistRepository()
|
||||
@ -38,7 +39,7 @@ func importLibrary(files []Track) (err error){
|
||||
for _, t := range files {
|
||||
mf, album, artist := parseTrack(&t)
|
||||
persist(mfRepo, mf, albumRepo, album, artistRepo, artist)
|
||||
collectIndex(artist, artistIndex)
|
||||
collectIndex(indexGroups, artist, artistIndex)
|
||||
}
|
||||
|
||||
if err = saveIndex(artistIndex); err != nil {
|
||||
@ -104,19 +105,29 @@ func persist(mfRepo *repositories.MediaFile, mf *models.MediaFile, albumRepo *re
|
||||
}
|
||||
}
|
||||
|
||||
func collectIndex(a *models.Artist, artistIndex map[string]tempIndex) {
|
||||
func collectIndex(ig utils.IndexGroups, a *models.Artist, artistIndex map[string]tempIndex) {
|
||||
name := a.Name
|
||||
indexName := strings.ToLower(utils.NoArticle(name))
|
||||
if indexName == "" {
|
||||
return
|
||||
}
|
||||
initial := strings.ToUpper(indexName[0:1])
|
||||
artists := artistIndex[initial]
|
||||
group := findGroup(ig, indexName)
|
||||
artists := artistIndex[group]
|
||||
if artists == nil {
|
||||
artists = make(tempIndex)
|
||||
artistIndex[initial] = artists
|
||||
artistIndex[group] = artists
|
||||
}
|
||||
artists[indexName] = &models.ArtistInfo{ArtistId: a.Id, Artist: a.Name}
|
||||
artists[indexName] = models.ArtistInfo{ArtistId: a.Id, Artist: a.Name}
|
||||
}
|
||||
|
||||
func findGroup(ig utils.IndexGroups, name string) string {
|
||||
for k, v := range ig {
|
||||
key := strings.ToLower(k)
|
||||
if strings.HasPrefix(name, key) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return "#"
|
||||
}
|
||||
|
||||
func saveIndex(artistIndex map[string]tempIndex) error {
|
||||
@ -125,7 +136,7 @@ func saveIndex(artistIndex map[string]tempIndex) error {
|
||||
for k, temp := range artistIndex {
|
||||
idx := &models.ArtistIndex{Id: k}
|
||||
for _, v := range temp {
|
||||
idx.Artists = append(idx.Artists, *v)
|
||||
idx.Artists = append(idx.Artists, v)
|
||||
}
|
||||
err := idxRepo.Put(idx)
|
||||
if err != nil {
|
||||
|
@ -3,10 +3,69 @@ package scanner
|
||||
import (
|
||||
"testing"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"github.com/deluan/gosonic/models"
|
||||
"github.com/deluan/gosonic/tests"
|
||||
)
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
func TestCollectIndex(t *testing.T) {
|
||||
tests.Init(t, false)
|
||||
|
||||
Convey("Missing tests", t, nil)
|
||||
ig := utils.IndexGroups{"A":"A", "B":"B", "Tom":"Tom", "X":"X-Z"}
|
||||
|
||||
Convey("Simple Name", t, func() {
|
||||
a := &models.Artist{Name: "Björk"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
collectIndex(ig, a, artistIndex)
|
||||
|
||||
So(artistIndex, ShouldContainKey, "B")
|
||||
So(artistIndex["B"], ShouldContainKey, "björk")
|
||||
|
||||
for _, k := range []string{"A", "Tom", "X-Z", "#"} {
|
||||
So(artistIndex, ShouldNotContainKey, k)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("Name not in the index", t, func() {
|
||||
a := &models.Artist{Name: "Kraftwerk"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
collectIndex(ig, a, artistIndex)
|
||||
|
||||
So(artistIndex, ShouldContainKey, "#")
|
||||
So(artistIndex["#"], ShouldContainKey, "kraftwerk")
|
||||
|
||||
for _, k := range []string{"A", "B", "Tom", "X-Z"} {
|
||||
So(artistIndex, ShouldNotContainKey, k)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("Name starts with an article", t, func() {
|
||||
a := &models.Artist{Name: "The The"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
collectIndex(ig, a, artistIndex)
|
||||
|
||||
So(artistIndex, ShouldContainKey, "#")
|
||||
So(artistIndex["#"], ShouldContainKey, "the")
|
||||
|
||||
for _, k := range []string{"A", "B", "Tom", "X-Z"} {
|
||||
So(artistIndex, ShouldNotContainKey, k)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("Name match a multichar entry", t, func() {
|
||||
a := &models.Artist{Name: "Tom Waits"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
collectIndex(ig, a, artistIndex)
|
||||
|
||||
So(artistIndex, ShouldContainKey, "Tom")
|
||||
So(artistIndex["Tom"], ShouldContainKey, "tom waits")
|
||||
|
||||
for _, k := range []string{"A", "B", "X-Z", "#"} {
|
||||
So(artistIndex, ShouldNotContainKey, k)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user