Implemented proper index grouping

This commit is contained in:
Deluan 2016-03-01 17:50:05 -05:00
parent 9d6eb40f6f
commit c64a0f8242
4 changed files with 81 additions and 12 deletions

View File

@ -8,7 +8,7 @@ apiVersion = 1.0.0
ignoredArticles="The El La Los Las Le Les Os As O A" 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) 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 user=deluan
password=wordpass password=wordpass
dbPath = ./devDb dbPath = ./devDb

View File

@ -3,5 +3,4 @@ package models
type Artist struct { type Artist struct {
Id string Id string
Name string Name string
Albums map[string]bool
} }

View File

@ -15,7 +15,7 @@ type Scanner interface {
LoadFolder(path string) []Track LoadFolder(path string) []Track
} }
type tempIndex map[string]*models.ArtistInfo type tempIndex map[string]models.ArtistInfo
// TODO Implement a flag 'isScanning'. // TODO Implement a flag 'isScanning'.
func StartImport() { func StartImport() {
@ -30,6 +30,7 @@ func doImport(mediaFolder string, scanner Scanner) {
} }
func importLibrary(files []Track) (err error){ func importLibrary(files []Track) (err error){
indexGroups := utils.ParseIndexGroups(beego.AppConfig.String("indexGroups"))
mfRepo := repositories.NewMediaFileRepository() mfRepo := repositories.NewMediaFileRepository()
albumRepo := repositories.NewAlbumRepository() albumRepo := repositories.NewAlbumRepository()
artistRepo := repositories.NewArtistRepository() artistRepo := repositories.NewArtistRepository()
@ -38,7 +39,7 @@ func importLibrary(files []Track) (err error){
for _, t := range files { for _, t := range files {
mf, album, artist := parseTrack(&t) mf, album, artist := parseTrack(&t)
persist(mfRepo, mf, albumRepo, album, artistRepo, artist) persist(mfRepo, mf, albumRepo, album, artistRepo, artist)
collectIndex(artist, artistIndex) collectIndex(indexGroups, artist, artistIndex)
} }
if err = saveIndex(artistIndex); err != nil { 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 name := a.Name
indexName := strings.ToLower(utils.NoArticle(name)) indexName := strings.ToLower(utils.NoArticle(name))
if indexName == "" { if indexName == "" {
return return
} }
initial := strings.ToUpper(indexName[0:1]) group := findGroup(ig, indexName)
artists := artistIndex[initial] artists := artistIndex[group]
if artists == nil { if artists == nil {
artists = make(tempIndex) 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 { func saveIndex(artistIndex map[string]tempIndex) error {
@ -125,7 +136,7 @@ func saveIndex(artistIndex map[string]tempIndex) error {
for k, temp := range artistIndex { for k, temp := range artistIndex {
idx := &models.ArtistIndex{Id: k} idx := &models.ArtistIndex{Id: k}
for _, v := range temp { for _, v := range temp {
idx.Artists = append(idx.Artists, *v) idx.Artists = append(idx.Artists, v)
} }
err := idxRepo.Put(idx) err := idxRepo.Put(idx)
if err != nil { if err != nil {

View File

@ -3,10 +3,69 @@ package scanner
import ( import (
"testing" "testing"
. "github.com/smartystreets/goconvey/convey" . "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)
}
})
} }