2016-02-26 07:32:31 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/deluan/gosonic/repositories"
|
|
|
|
"github.com/deluan/gosonic/models"
|
2016-02-27 09:35:01 +01:00
|
|
|
"strings"
|
2016-02-29 19:56:09 +01:00
|
|
|
"github.com/deluan/gosonic/utils"
|
2016-02-26 07:32:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Scanner interface {
|
|
|
|
LoadFolder(path string) []Track
|
|
|
|
}
|
|
|
|
|
2016-02-28 21:46:53 +01:00
|
|
|
type tempIndex map[string]*models.ArtistInfo
|
|
|
|
|
2016-02-26 07:32:31 +01:00
|
|
|
func StartImport() {
|
|
|
|
go doImport(beego.AppConfig.String("musicFolder"), &ItunesScanner{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func doImport(mediaFolder string, scanner Scanner) {
|
|
|
|
beego.Info("Starting iTunes import from:", mediaFolder)
|
|
|
|
files := scanner.LoadFolder(mediaFolder)
|
2016-02-28 19:50:05 +01:00
|
|
|
importLibrary(files)
|
2016-02-26 07:32:31 +01:00
|
|
|
beego.Info("Finished importing", len(files), "files")
|
|
|
|
}
|
|
|
|
|
2016-02-28 19:50:05 +01:00
|
|
|
func importLibrary(files []Track) {
|
2016-02-26 07:32:31 +01:00
|
|
|
mfRepo := repositories.NewMediaFileRepository()
|
2016-02-28 19:50:05 +01:00
|
|
|
albumRepo := repositories.NewAlbumRepository()
|
|
|
|
artistRepo := repositories.NewArtistRepository()
|
2016-02-28 21:46:53 +01:00
|
|
|
var artistIndex = make(map[string]tempIndex)
|
2016-02-28 19:50:05 +01:00
|
|
|
|
2016-02-26 07:32:31 +01:00
|
|
|
for _, t := range files {
|
2016-02-29 04:56:24 +01:00
|
|
|
mf, album, artist := parseTrack(&t)
|
|
|
|
persist(mfRepo, mf, albumRepo, album, artistRepo, artist)
|
2016-02-28 21:46:53 +01:00
|
|
|
collectIndex(artist, artistIndex)
|
2016-02-26 07:32:31 +01:00
|
|
|
}
|
2016-02-28 19:50:05 +01:00
|
|
|
|
2016-02-28 21:46:53 +01:00
|
|
|
if err := saveIndex(artistIndex); err != nil {
|
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-02-29 04:56:24 +01:00
|
|
|
c, _ := artistRepo.CountAll()
|
|
|
|
beego.Info("Total Artists in database:", c)
|
|
|
|
c, _ = albumRepo.CountAll()
|
|
|
|
beego.Info("Total Albums in database:", c)
|
|
|
|
c, _ = mfRepo.CountAll()
|
|
|
|
beego.Info("Total MediaFiles in database:", c)
|
2016-02-27 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 04:56:24 +01:00
|
|
|
func parseTrack(t *Track) (*models.MediaFile, *models.Album, *models.Artist) {
|
2016-02-28 19:50:05 +01:00
|
|
|
mf := &models.MediaFile{
|
|
|
|
Id: t.Id,
|
|
|
|
Album: t.Album,
|
|
|
|
Artist: t.Artist,
|
|
|
|
AlbumArtist: t.AlbumArtist,
|
|
|
|
Title: t.Title,
|
|
|
|
Compilation: t.Compilation,
|
|
|
|
Path: t.Path,
|
|
|
|
CreatedAt: t.CreatedAt,
|
|
|
|
UpdatedAt: t.UpdatedAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
album := &models.Album{
|
|
|
|
Name: t.Album,
|
|
|
|
Year: t.Year,
|
|
|
|
Compilation: t.Compilation,
|
|
|
|
}
|
|
|
|
|
|
|
|
artist := &models.Artist{
|
2016-02-28 21:46:53 +01:00
|
|
|
Name: t.RealArtist(),
|
2016-02-28 19:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return mf, album, artist
|
|
|
|
}
|
|
|
|
|
2016-02-29 04:56:24 +01:00
|
|
|
func persist(mfRepo *repositories.MediaFile, mf *models.MediaFile, albumRepo *repositories.Album, album *models.Album, artistRepo *repositories.Artist, artist *models.Artist) {
|
|
|
|
if err := artistRepo.Put(artist); err != nil {
|
2016-02-28 19:50:05 +01:00
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-02-29 04:56:24 +01:00
|
|
|
album.ArtistId = artist.Id
|
|
|
|
if err := albumRepo.Put(album); err != nil {
|
2016-02-28 19:50:05 +01:00
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-02-29 04:56:24 +01:00
|
|
|
mf.AlbumId = album.Id
|
2016-02-28 19:50:05 +01:00
|
|
|
if err := mfRepo.Put(mf); err != nil {
|
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 21:46:53 +01:00
|
|
|
func collectIndex(a *models.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
|
|
|
|
}
|
|
|
|
initial := strings.ToUpper(indexName[0:1])
|
|
|
|
artists := artistIndex[initial]
|
|
|
|
if artists == nil {
|
2016-02-28 21:46:53 +01:00
|
|
|
artists = make(tempIndex)
|
2016-02-27 09:35:01 +01:00
|
|
|
artistIndex[initial] = artists
|
|
|
|
}
|
2016-02-28 21:46:53 +01:00
|
|
|
artists[indexName] = &models.ArtistInfo{ArtistId: a.Id, Artist: a.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveIndex(artistIndex map[string]tempIndex) error {
|
|
|
|
idxRepo := repositories.NewArtistIndexRepository()
|
|
|
|
|
|
|
|
for k, temp := range artistIndex {
|
|
|
|
idx := &models.ArtistIndex{Id: k}
|
|
|
|
for _, v := range temp {
|
|
|
|
idx.Artists = append(idx.Artists, *v)
|
|
|
|
}
|
|
|
|
err := idxRepo.Put(idx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-02-26 07:32:31 +01:00
|
|
|
}
|