2016-02-26 07:32:31 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
2016-03-02 19:18:39 +01:00
|
|
|
"fmt"
|
2016-03-08 20:18:17 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-03-12 00:37:37 +01:00
|
|
|
"os"
|
|
|
|
|
2016-02-26 07:32:31 +01:00
|
|
|
"github.com/astaxie/beego"
|
2016-03-04 15:09:16 +01:00
|
|
|
"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"
|
2016-03-04 02:01:27 +01:00
|
|
|
"github.com/deluan/gosonic/utils"
|
2016-02-26 07:32:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Scanner interface {
|
2016-03-07 03:32:31 +01:00
|
|
|
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-02-26 07:32:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 15:07:24 +01:00
|
|
|
type tempIndex map[string]domain.ArtistInfo
|
2016-02-28 21:46:53 +01:00
|
|
|
|
2016-03-12 00:37:37 +01:00
|
|
|
var (
|
2016-03-12 00:39:06 +01:00
|
|
|
inProgress chan int
|
|
|
|
lastCheck time.Time
|
2016-03-12 00:37:37 +01:00
|
|
|
itunesLibrary string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-03-12 00:39:06 +01:00
|
|
|
inProgress = make(chan int)
|
2016-03-15 22:37:17 +01:00
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
startImport()
|
|
|
|
}()
|
2016-03-12 00:37:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckForUpdates(force bool) {
|
|
|
|
<-inProgress
|
|
|
|
|
|
|
|
if force {
|
2016-03-12 00:39:06 +01:00
|
|
|
lastCheck = time.Time{}
|
2016-03-12 00:37:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
startImport()
|
|
|
|
}
|
|
|
|
|
|
|
|
func startImport() {
|
2016-03-04 02:01:27 +01:00
|
|
|
go func() {
|
2016-03-12 00:37:37 +01:00
|
|
|
itunesLibrary = beego.AppConfig.String("musicFolder")
|
|
|
|
|
|
|
|
info, err := os.Stat(itunesLibrary)
|
|
|
|
if err != nil {
|
|
|
|
inProgress <- 1
|
|
|
|
beego.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-12 00:39:06 +01:00
|
|
|
if lastCheck.After(info.ModTime()) {
|
2016-03-12 00:37:37 +01:00
|
|
|
inProgress <- 1
|
|
|
|
return
|
|
|
|
}
|
2016-03-12 00:39:06 +01:00
|
|
|
lastCheck = time.Now()
|
2016-03-12 00:37:37 +01:00
|
|
|
|
2016-03-11 05:19:13 +01:00
|
|
|
// 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)
|
2016-03-04 02:01:27 +01:00
|
|
|
i.Run()
|
2016-03-12 00:37:37 +01:00
|
|
|
inProgress <- 1
|
2016-03-04 02:01:27 +01:00
|
|
|
}()
|
2016-02-26 07:32:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 02:01:27 +01:00
|
|
|
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
|
2016-03-11 05:19:13 +01:00
|
|
|
search engine.Search
|
2016-03-08 20:18:17 +01:00
|
|
|
lastScan time.Time
|
2016-03-04 02:01:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Importer) Run() {
|
2016-03-08 20:18:17 +01:00
|
|
|
i.lastScan = i.lastModifiedSince()
|
2016-03-10 15:37:35 +01:00
|
|
|
|
2016-03-15 22:37:17 +01:00
|
|
|
if i.lastScan.IsZero() {
|
|
|
|
beego.Info("Starting first iTunes Library scan. This can take a while...")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2016-03-15 22:37:17 +01:00
|
|
|
if i.lastScan.IsZero() {
|
|
|
|
beego.Info("Finished first iTunes Library import")
|
|
|
|
} else {
|
|
|
|
beego.Debug("Finished updating tracks from iTunes Library")
|
|
|
|
}
|
2016-02-26 07:32:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-07 03:32:31 +01:00
|
|
|
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{}
|
|
|
|
}
|
2016-03-07 03:32:31 +01:00
|
|
|
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)
|
2016-03-08 20:18:17 +01:00
|
|
|
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-03-15 22:06:23 +01:00
|
|
|
arc, _ := i.artistRepo.CountAll()
|
|
|
|
alc, _ := i.albumRepo.CountAll()
|
|
|
|
mfc, _ := i.mfRepo.CountAll()
|
|
|
|
plc, _ := i.plsRepo.CountAll()
|
|
|
|
alu := 0
|
|
|
|
mfu := 0
|
2016-02-28 19:50:05 +01:00
|
|
|
|
2016-03-11 05:24:30 +01:00
|
|
|
i.search.ClearAll()
|
|
|
|
|
2016-03-08 20:18:17 +01:00
|
|
|
beego.Debug("Saving updated data")
|
|
|
|
j := 0
|
2016-03-04 22:42:09 +01:00
|
|
|
for _, mf := range i.scanner.MediaFiles() {
|
2016-03-08 20:18:17 +01:00
|
|
|
mfs[j] = *mf
|
|
|
|
j++
|
2016-03-16 20:35:21 +01:00
|
|
|
if err := i.search.IndexMediaFile(mf); err != nil {
|
|
|
|
beego.Error("Error indexing artist:", err)
|
|
|
|
}
|
2016-03-08 20:18:17 +01:00
|
|
|
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-15 22:06:23 +01:00
|
|
|
mfu++
|
2016-03-11 18:10:37 +01:00
|
|
|
if !i.lastScan.IsZero() {
|
2016-03-11 23:09:47 +01:00
|
|
|
beego.Debug("-- Updated Track:", mf.Title)
|
2016-03-11 18:10:37 +01:00
|
|
|
}
|
2016-03-04 22:42:09 +01:00
|
|
|
}
|
|
|
|
|
2016-03-08 20:18:17 +01:00
|
|
|
j = 0
|
2016-03-04 22:42:09 +01:00
|
|
|
for _, al := range i.scanner.Albums() {
|
2016-03-08 20:18:17 +01:00
|
|
|
als[j] = *al
|
|
|
|
j++
|
2016-03-16 20:35:21 +01:00
|
|
|
if err := i.search.IndexAlbum(al); err != nil {
|
|
|
|
beego.Error("Error indexing artist:", err)
|
|
|
|
}
|
2016-03-08 20:18:17 +01:00
|
|
|
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-15 22:06:23 +01:00
|
|
|
alu++
|
2016-03-11 18:10:37 +01:00
|
|
|
if !i.lastScan.IsZero() {
|
2016-03-11 23:09:47 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-08 20:18:17 +01:00
|
|
|
j = 0
|
2016-03-04 22:42:09 +01:00
|
|
|
for _, ar := range i.scanner.Artists() {
|
2016-03-08 20:18:17 +01:00
|
|
|
ars[j] = *ar
|
|
|
|
j++
|
2016-03-04 22:42:09 +01:00
|
|
|
if err := i.artistRepo.Put(ar); err != nil {
|
|
|
|
beego.Error(err)
|
|
|
|
}
|
2016-03-11 05:19:13 +01:00
|
|
|
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-26 07:32:31 +01:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 02:01:27 +01:00
|
|
|
if err = i.saveIndex(artistIndex); err != nil {
|
2016-02-28 21:46:53 +01:00
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-03-08 20:18:17 +01:00
|
|
|
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)
|
|
|
|
}
|
2016-03-08 20:18:17 +01:00
|
|
|
|
2016-03-15 22:06:23 +01:00
|
|
|
arc2, _ := i.artistRepo.CountAll()
|
|
|
|
alc2, _ := i.albumRepo.CountAll()
|
|
|
|
mfc2, _ := i.mfRepo.CountAll()
|
|
|
|
plc2, _ := i.plsRepo.CountAll()
|
|
|
|
|
|
|
|
if arc != arc2 || alc != alc2 || mfc != mfc2 || plc != plc2 {
|
|
|
|
beego.Info(fmt.Sprintf("Updated library totals: %d(%+d) artists, %d(%+d) albums, %d(%+d) songs, %d(%+d) playlists", arc2, arc2-arc, alc2, alc2-alc, mfc2, mfc2-mfc, plc2, plc2-plc))
|
|
|
|
}
|
|
|
|
if alu > 0 || mfu > 0 {
|
|
|
|
beego.Info(fmt.Sprintf("Updated items: %d album(s), %d song(s)", alu, mfu))
|
|
|
|
}
|
2016-03-01 20:40:26 +01:00
|
|
|
|
|
|
|
if err == nil {
|
2016-03-07 03:32:31 +01:00
|
|
|
millis := time.Now().UnixNano() / int64(time.Millisecond)
|
2016-03-04 02:01:27 +01:00
|
|
|
i.propertyRepo.Put(consts.LastScan, fmt.Sprint(millis))
|
2016-03-15 22:06:23 +01:00
|
|
|
beego.Debug("LastScan timestamp:", millis)
|
2016-03-01 20:40:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2016-02-27 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 02:01:27 +01:00
|
|
|
func (i *Importer) collectIndex(ig utils.IndexGroups, a *domain.Artist, artistIndex map[string]tempIndex) {
|
2016-02-28 21:46:53 +01:00
|
|
|
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
|
|
|
|
}
|
2016-03-04 02:01:27 +01:00
|
|
|
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 {
|
2016-02-28 21:46:53 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-04 02:01:27 +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 "#"
|
2016-02-28 21:46:53 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 02:01:27 +01:00
|
|
|
func (i *Importer) saveIndex(artistIndex map[string]tempIndex) error {
|
2016-02-28 21:46:53 +01:00
|
|
|
for k, temp := range artistIndex {
|
2016-03-02 15:07:24 +01:00
|
|
|
idx := &domain.ArtistIndex{Id: k}
|
2016-02-28 21:46:53 +01:00
|
|
|
for _, v := range temp {
|
2016-03-01 23:50:05 +01:00
|
|
|
idx.Artists = append(idx.Artists, v)
|
2016-02-28 21:46:53 +01:00
|
|
|
}
|
2016-03-04 02:01:27 +01:00
|
|
|
err := i.idxRepo.Put(idx)
|
2016-02-28 21:46:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-03-02 19:18:39 +01:00
|
|
|
}
|