Scanning artists and albums too
This commit is contained in:
+68
-19
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/deluan/gosonic/repositories"
|
||||
"github.com/deluan/gosonic/models"
|
||||
"strings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Scanner interface {
|
||||
@@ -18,38 +19,86 @@ func StartImport() {
|
||||
func doImport(mediaFolder string, scanner Scanner) {
|
||||
beego.Info("Starting iTunes import from:", mediaFolder)
|
||||
files := scanner.LoadFolder(mediaFolder)
|
||||
updateDatastore(files)
|
||||
importLibrary(files)
|
||||
beego.Info("Finished importing", len(files), "files")
|
||||
}
|
||||
|
||||
func updateDatastore(files []Track) {
|
||||
func importLibrary(files []Track) {
|
||||
mfRepo := repositories.NewMediaFileRepository()
|
||||
albumRepo := repositories.NewAlbumRepository()
|
||||
artistRepo := repositories.NewArtistRepository()
|
||||
var artistIndex = make(map[string]map[string]string)
|
||||
|
||||
for _, t := range files {
|
||||
m := &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,
|
||||
}
|
||||
err := mfRepo.Add(m)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
collectIndex(m, artistIndex)
|
||||
mf, album, artist := processTrack(&t)
|
||||
mergeInfo(mfRepo, mf, albumRepo, album, artistRepo, artist)
|
||||
fmt.Printf("%#v\n", album)
|
||||
fmt.Printf("%#v\n\n", artist)
|
||||
collectIndex(mf, artistIndex)
|
||||
}
|
||||
//mfRepo.Dump()
|
||||
|
||||
//j,_ := json.MarshalIndent(artistIndex, "", " ")
|
||||
//fmt.Println(string(j))
|
||||
c, _ := mfRepo.CountAll()
|
||||
|
||||
beego.Info("Total mediafiles in database:", c)
|
||||
}
|
||||
|
||||
func processTrack(t *Track) (*models.MediaFile, *models.Album, *models.Artist) {
|
||||
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{
|
||||
Name: t.Artist,
|
||||
}
|
||||
|
||||
return mf, album, artist
|
||||
}
|
||||
|
||||
func mergeInfo(mfRepo *repositories.MediaFile, mf *models.MediaFile, albumRepo *repositories.Album, album *models.Album, artistRepo *repositories.Artist, artist *models.Artist) {
|
||||
artist.Id = artistRepo.NewId(artist.Name)
|
||||
|
||||
sAlbum, err := albumRepo.GetByName(album.Name)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
album.ArtistId = artist.Id
|
||||
album.AddMediaFiles(mf, sAlbum.MediaFiles)
|
||||
sAlbum, err = albumRepo.Put(album)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
|
||||
sArtist, err := artistRepo.GetByName(artist.Name)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
artist.AddAlbums(sAlbum, sArtist.Albums)
|
||||
sArtist, err = artistRepo.Put(artist)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
|
||||
if err := mfRepo.Put(mf); err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func collectIndex(m *models.MediaFile, artistIndex map[string]map[string]string) {
|
||||
name := m.RealArtist()
|
||||
indexName := strings.ToLower(models.NoArticle(name))
|
||||
|
||||
Reference in New Issue
Block a user