Integrate ArtistIndex logic into Artist
This commit is contained in:
@@ -4,13 +4,11 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
)
|
||||
|
||||
type Scanner interface {
|
||||
@@ -21,29 +19,25 @@ type Scanner interface {
|
||||
Playlists() map[string]*model.Playlist
|
||||
}
|
||||
|
||||
type tempIndex map[string]model.ArtistInfo
|
||||
|
||||
type Importer struct {
|
||||
scanner Scanner
|
||||
mediaFolder string
|
||||
mfRepo model.MediaFileRepository
|
||||
albumRepo model.AlbumRepository
|
||||
artistRepo model.ArtistRepository
|
||||
idxRepo model.ArtistIndexRepository
|
||||
plsRepo model.PlaylistRepository
|
||||
propertyRepo model.PropertyRepository
|
||||
lastScan time.Time
|
||||
lastCheck time.Time
|
||||
}
|
||||
|
||||
func NewImporter(mediaFolder string, scanner Scanner, mfRepo model.MediaFileRepository, albumRepo model.AlbumRepository, artistRepo model.ArtistRepository, idxRepo model.ArtistIndexRepository, plsRepo model.PlaylistRepository, propertyRepo model.PropertyRepository) *Importer {
|
||||
func NewImporter(mediaFolder string, scanner Scanner, mfRepo model.MediaFileRepository, albumRepo model.AlbumRepository, artistRepo model.ArtistRepository, plsRepo model.PlaylistRepository, propertyRepo model.PropertyRepository) *Importer {
|
||||
return &Importer{
|
||||
scanner: scanner,
|
||||
mediaFolder: mediaFolder,
|
||||
mfRepo: mfRepo,
|
||||
albumRepo: albumRepo,
|
||||
artistRepo: artistRepo,
|
||||
idxRepo: idxRepo,
|
||||
plsRepo: plsRepo,
|
||||
propertyRepo: propertyRepo,
|
||||
}
|
||||
@@ -131,7 +125,6 @@ func (i *Importer) importLibrary() (err error) {
|
||||
log.Debug("Imported artists", "total", len(ars))
|
||||
pls := i.importPlaylists()
|
||||
log.Debug("Imported playlists", "total", len(pls))
|
||||
i.importArtistIndex()
|
||||
|
||||
log.Debug("Purging old data")
|
||||
if err := i.mfRepo.PurgeInactive(mfs); err != nil {
|
||||
@@ -239,19 +232,6 @@ func (i *Importer) importArtists() model.Artists {
|
||||
return ars
|
||||
}
|
||||
|
||||
func (i *Importer) importArtistIndex() {
|
||||
indexGroups := utils.ParseIndexGroups(conf.Sonic.IndexGroups)
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
for _, ar := range i.scanner.Artists() {
|
||||
i.collectIndex(indexGroups, ar, artistIndex)
|
||||
}
|
||||
|
||||
if err := i.saveIndex(artistIndex); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Importer) importPlaylists() model.Playlists {
|
||||
pls := make(model.Playlists, len(i.scanner.Playlists()))
|
||||
j := 0
|
||||
@@ -267,44 +247,3 @@ func (i *Importer) importPlaylists() model.Playlists {
|
||||
}
|
||||
return pls
|
||||
}
|
||||
|
||||
func (i *Importer) collectIndex(ig utils.IndexGroups, a *model.Artist, artistIndex map[string]tempIndex) {
|
||||
name := a.Name
|
||||
indexName := strings.ToLower(utils.NoArticle(name))
|
||||
if indexName == "" {
|
||||
return
|
||||
}
|
||||
group := i.findGroup(ig, indexName)
|
||||
artists := artistIndex[group]
|
||||
if artists == nil {
|
||||
artists = make(tempIndex)
|
||||
artistIndex[group] = artists
|
||||
}
|
||||
artists[indexName] = model.ArtistInfo{ArtistID: a.ID, Artist: a.Name, AlbumCount: a.AlbumCount}
|
||||
}
|
||||
|
||||
func (i *Importer) 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 (i *Importer) saveIndex(artistIndex map[string]tempIndex) error {
|
||||
i.idxRepo.DeleteAll()
|
||||
for k, temp := range artistIndex {
|
||||
idx := &model.ArtistIndex{ID: k}
|
||||
for _, v := range temp {
|
||||
idx.Artists = append(idx.Artists, v)
|
||||
}
|
||||
err := i.idxRepo.Put(idx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package scanner_legacy
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/cloudsonic/sonic-server/tests"
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestCollectIndex(t *testing.T) {
|
||||
tests.Init(t, false)
|
||||
|
||||
ig := utils.IndexGroups{"A": "A", "B": "B", "Tom": "Tom", "X": "X-Z"}
|
||||
|
||||
importer := &Importer{}
|
||||
|
||||
Convey("Simple Name", t, func() {
|
||||
a := &model.Artist{Name: "Björk"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
importer.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 := &model.Artist{Name: "Kraftwerk"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
importer.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 := &model.Artist{Name: "The The"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
importer.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 := &model.Artist{Name: "Tom Waits"}
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
importer.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)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user