navidrome/scanner/importer.go

229 lines
5.5 KiB
Go
Raw Normal View History

package scanner
import (
2016-03-02 19:18:39 +01:00
"fmt"
"strconv"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/consts"
2016-03-02 15:07:24 +01:00
"github.com/deluan/gosonic/domain"
2016-03-09 00:40:16 +01:00
"github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils"
)
type Scanner interface {
ScanLibrary(lastModifiedSince time.Time, path string) (int, error)
2016-03-04 22:42:09 +01:00
MediaFiles() map[string]*domain.MediaFile
Albums() map[string]*domain.Album
Artists() map[string]*domain.Artist
2016-03-09 19:51:17 +01:00
Playlists() map[string]*domain.Playlist
}
2016-03-02 15:07:24 +01:00
type tempIndex map[string]domain.ArtistInfo
func StartImport() {
go func() {
// TODO Move all to DI
i := &Importer{mediaFolder: beego.AppConfig.String("musicFolder")}
2016-03-11 21:16:17 +01:00
utils.ResolveDependencies(&i.mfRepo, &i.albumRepo, &i.artistRepo, &i.idxRepo, &i.plsRepo,
&i.propertyRepo, &i.search, &i.scanner)
i.Run()
}()
}
// TODO Implement a flag 'inProgress'.
type Importer struct {
scanner Scanner
mediaFolder string
mfRepo domain.MediaFileRepository
albumRepo domain.AlbumRepository
artistRepo domain.ArtistRepository
idxRepo domain.ArtistIndexRepository
2016-03-09 19:51:17 +01:00
plsRepo domain.PlaylistRepository
2016-03-09 00:40:16 +01:00
propertyRepo engine.PropertyRepository
search engine.Search
lastScan time.Time
}
func (i *Importer) Run() {
i.lastScan = i.lastModifiedSince()
2016-03-10 15:37:35 +01:00
total, err := i.scanner.ScanLibrary(i.lastScan, i.mediaFolder)
if err != nil {
2016-03-04 22:42:09 +01:00
beego.Error("Error importing iTunes Library:", err)
return
}
2016-03-10 15:37:35 +01:00
beego.Debug("Found", total, "tracks,",
len(i.scanner.MediaFiles()), "songs,",
len(i.scanner.Albums()), "albums,",
len(i.scanner.Artists()), "artists",
len(i.scanner.Playlists()), "playlists")
2016-03-04 22:42:09 +01:00
if err := i.importLibrary(); err != nil {
beego.Error("Error persisting data:", err)
}
beego.Info("Finished importing tracks from iTunes Library")
}
func (i *Importer) lastModifiedSince() time.Time {
ms, err := i.propertyRepo.Get(consts.LastScan)
if err != nil {
beego.Warn("Couldn't read LastScan:", err)
return time.Time{}
}
2016-03-11 18:10:37 +01:00
if ms == "" {
beego.Debug("First scan")
return time.Time{}
}
s, _ := strconv.ParseInt(ms, 10, 64)
return time.Unix(0, s*int64(time.Millisecond))
}
2016-03-04 22:42:09 +01:00
func (i *Importer) importLibrary() (err error) {
2016-03-01 23:50:05 +01:00
indexGroups := utils.ParseIndexGroups(beego.AppConfig.String("indexGroups"))
2016-03-04 22:42:09 +01:00
artistIndex := make(map[string]tempIndex)
mfs := make(domain.MediaFiles, len(i.scanner.MediaFiles()))
als := make(domain.Albums, len(i.scanner.Albums()))
ars := make(domain.Artists, len(i.scanner.Artists()))
2016-03-09 19:51:17 +01:00
pls := make(domain.Playlists, len(i.scanner.Playlists()))
2016-02-28 19:50:05 +01:00
2016-03-11 05:24:30 +01:00
i.search.ClearAll()
beego.Debug("Saving updated data")
j := 0
2016-03-04 22:42:09 +01:00
for _, mf := range i.scanner.MediaFiles() {
mfs[j] = *mf
j++
if mf.UpdatedAt.Before(i.lastScan) {
continue
}
2016-03-04 22:42:09 +01:00
if err := i.mfRepo.Put(mf); err != nil {
beego.Error(err)
}
2016-03-11 05:24:30 +01:00
if err := i.search.IndexMediaFile(mf); err != nil {
beego.Error("Error indexing artist:", err)
}
2016-03-11 18:10:37 +01:00
if !i.lastScan.IsZero() {
beego.Debug("-- Updated Track:", mf.Title)
2016-03-11 18:10:37 +01:00
}
2016-03-04 22:42:09 +01:00
}
j = 0
2016-03-04 22:42:09 +01:00
for _, al := range i.scanner.Albums() {
als[j] = *al
j++
if al.UpdatedAt.Before(i.lastScan) {
continue
}
2016-03-04 22:42:09 +01:00
if err := i.albumRepo.Put(al); err != nil {
beego.Error(err)
}
2016-03-11 05:24:30 +01:00
if err := i.search.IndexAlbum(al); err != nil {
beego.Error("Error indexing artist:", err)
}
2016-03-11 18:10:37 +01:00
if !i.lastScan.IsZero() {
beego.Debug(fmt.Sprintf(`-- Updated Album:"%s" from "%s"`, al.Name, al.Artist))
2016-03-11 18:10:37 +01:00
}
2016-03-04 22:42:09 +01:00
}
j = 0
2016-03-04 22:42:09 +01:00
for _, ar := range i.scanner.Artists() {
ars[j] = *ar
j++
2016-03-04 22:42:09 +01:00
if err := i.artistRepo.Put(ar); err != nil {
beego.Error(err)
}
if err := i.search.IndexArtist(ar); err != nil {
beego.Error("Error indexing artist:", err)
}
2016-03-04 22:42:09 +01:00
i.collectIndex(indexGroups, ar, artistIndex)
}
2016-02-28 19:50:05 +01:00
2016-03-09 19:51:17 +01:00
j = 0
for _, pl := range i.scanner.Playlists() {
pls[j] = *pl
j++
if err := i.plsRepo.Put(pl); err != nil {
beego.Error(err)
}
}
if err = i.saveIndex(artistIndex); err != nil {
beego.Error(err)
}
beego.Debug("Purging old data")
if err := i.mfRepo.PurgeInactive(&mfs); err != nil {
beego.Error(err)
}
if err := i.albumRepo.PurgeInactive(&als); err != nil {
beego.Error(err)
}
if err := i.artistRepo.PurgeInactive(&ars); err != nil {
beego.Error(err)
}
2016-03-09 19:51:17 +01:00
if err := i.plsRepo.PurgeInactive(&pls); err != nil {
beego.Error(err)
}
c, _ := i.artistRepo.CountAll()
2016-02-29 04:56:24 +01:00
beego.Info("Total Artists in database:", c)
c, _ = i.albumRepo.CountAll()
2016-02-29 04:56:24 +01:00
beego.Info("Total Albums in database:", c)
c, _ = i.mfRepo.CountAll()
2016-02-29 04:56:24 +01:00
beego.Info("Total MediaFiles in database:", c)
2016-03-09 19:51:17 +01:00
c, _ = i.plsRepo.CountAll()
beego.Info("Total Playlists in database:", c)
if err == nil {
millis := time.Now().UnixNano() / int64(time.Millisecond)
i.propertyRepo.Put(consts.LastScan, fmt.Sprint(millis))
beego.Info("LastScan timestamp:", millis)
}
return err
2016-02-27 09:35:01 +01:00
}
func (i *Importer) collectIndex(ig utils.IndexGroups, a *domain.Artist, artistIndex map[string]tempIndex) {
name := a.Name
2016-02-29 19:56:09 +01:00
indexName := strings.ToLower(utils.NoArticle(name))
2016-02-27 09:35:01 +01:00
if indexName == "" {
return
}
group := i.findGroup(ig, indexName)
2016-03-01 23:50:05 +01:00
artists := artistIndex[group]
2016-02-27 09:35:01 +01:00
if artists == nil {
artists = make(tempIndex)
2016-03-01 23:50:05 +01:00
artistIndex[group] = artists
2016-02-27 09:35:01 +01:00
}
2016-03-02 15:07:24 +01:00
artists[indexName] = domain.ArtistInfo{ArtistId: a.Id, Artist: a.Name}
2016-03-01 23:50:05 +01:00
}
func (i *Importer) findGroup(ig utils.IndexGroups, name string) string {
2016-03-01 23:50:05 +01:00
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 {
for k, temp := range artistIndex {
2016-03-02 15:07:24 +01:00
idx := &domain.ArtistIndex{Id: k}
for _, v := range temp {
2016-03-01 23:50:05 +01:00
idx.Artists = append(idx.Artists, v)
}
err := i.idxRepo.Put(idx)
if err != nil {
return err
}
}
return nil
2016-03-02 19:18:39 +01:00
}