Converted scanner to a proper Import "class", to facilitate testing

This commit is contained in:
Deluan
2016-03-03 20:01:27 -05:00
parent 3b60b62aa1
commit 4b70daf143
2 changed files with 58 additions and 40 deletions
+52 -36
View File
@@ -3,14 +3,14 @@ package scanner
import ( import (
"fmt" "fmt"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/deluan/gosonic/consts"
"github.com/deluan/gosonic/domain" "github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/persistence" "github.com/deluan/gosonic/persistence"
"github.com/deluan/gosonic/utils"
"strings"
"time" "time"
"github.com/dhowden/tag" "github.com/dhowden/tag"
"github.com/deluan/gosonic/utils"
"github.com/deluan/gosonic/consts"
"os" "os"
"strings"
) )
type Scanner interface { type Scanner interface {
@@ -19,53 +19,71 @@ type Scanner interface {
type tempIndex map[string]domain.ArtistInfo type tempIndex map[string]domain.ArtistInfo
// TODO Implement a flag 'isScanning'.
func StartImport() { func StartImport() {
go doImport(beego.AppConfig.String("musicFolder"), &ItunesScanner{}) go func() {
i := &Importer{
scanner: &ItunesScanner{},
mediaFolder: beego.AppConfig.String("musicFolder"),
mfRepo: persistence.NewMediaFileRepository(),
albumRepo:persistence.NewAlbumRepository(),
artistRepo: persistence.NewArtistRepository(),
idxRepo: persistence.NewArtistIndexRepository(),
propertyRepo: persistence.NewPropertyRepository(),
}
i.Run()
}()
} }
func doImport(mediaFolder string, scanner Scanner) { // TODO Implement a flag 'inProgress'.
beego.Info("Starting iTunes import from:", mediaFolder) type Importer struct {
files := scanner.LoadFolder(mediaFolder) scanner Scanner
importLibrary(files) mediaFolder string
mfRepo domain.MediaFileRepository
albumRepo domain.AlbumRepository
artistRepo domain.ArtistRepository
idxRepo domain.ArtistIndexRepository
propertyRepo domain.PropertyRepository
}
func (i *Importer) Run() {
beego.Info("Starting iTunes import from:", i.mediaFolder)
files := i.scanner.LoadFolder(i.mediaFolder)
i.importLibrary(files)
beego.Info("Finished importing", len(files), "files") beego.Info("Finished importing", len(files), "files")
} }
func importLibrary(files []Track) (err error) { func (i *Importer) importLibrary(files []Track) (err error) {
indexGroups := utils.ParseIndexGroups(beego.AppConfig.String("indexGroups")) indexGroups := utils.ParseIndexGroups(beego.AppConfig.String("indexGroups"))
mfRepo := persistence.NewMediaFileRepository()
albumRepo := persistence.NewAlbumRepository()
artistRepo := persistence.NewArtistRepository()
var artistIndex = make(map[string]tempIndex) var artistIndex = make(map[string]tempIndex)
for _, t := range files { for _, t := range files {
mf, album, artist := parseTrack(&t) mf, album, artist := i.parseTrack(&t)
persist(mfRepo, mf, albumRepo, album, artistRepo, artist) i.persist(mf, album, artist)
collectIndex(indexGroups, artist, artistIndex) i.collectIndex(indexGroups, artist, artistIndex)
} }
if err = saveIndex(artistIndex); err != nil { if err = i.saveIndex(artistIndex); err != nil {
beego.Error(err) beego.Error(err)
} }
c, _ := artistRepo.CountAll() c, _ := i.artistRepo.CountAll()
beego.Info("Total Artists in database:", c) beego.Info("Total Artists in database:", c)
c, _ = albumRepo.CountAll() c, _ = i.albumRepo.CountAll()
beego.Info("Total Albums in database:", c) beego.Info("Total Albums in database:", c)
c, _ = mfRepo.CountAll() c, _ = i.mfRepo.CountAll()
beego.Info("Total MediaFiles in database:", c) beego.Info("Total MediaFiles in database:", c)
if err == nil { if err == nil {
propertyRepo := persistence.NewPropertyRepository()
millis := time.Now().UnixNano() / 1000000 millis := time.Now().UnixNano() / 1000000
propertyRepo.Put(consts.LastScan, fmt.Sprint(millis)) i.propertyRepo.Put(consts.LastScan, fmt.Sprint(millis))
beego.Info("LastScan timestamp:", millis) beego.Info("LastScan timestamp:", millis)
} }
return err return err
} }
func hasCoverArt(path string) bool { func (i *Importer) hasCoverArt(path string) bool {
if _, err := os.Stat(path); err == nil { if _, err := os.Stat(path); err == nil {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@@ -84,8 +102,8 @@ func hasCoverArt(path string) bool {
return false return false
} }
func parseTrack(t *Track) (*domain.MediaFile, *domain.Album, *domain.Artist) { func (i *Importer) parseTrack(t *Track) (*domain.MediaFile, *domain.Album, *domain.Artist) {
hasCover := hasCoverArt(t.Path) hasCover := i.hasCoverArt(t.Path)
mf := &domain.MediaFile{ mf := &domain.MediaFile{
Id: t.Id, Id: t.Id,
Album: t.Album, Album: t.Album,
@@ -129,29 +147,29 @@ func parseTrack(t *Track) (*domain.MediaFile, *domain.Album, *domain.Artist) {
return mf, album, artist return mf, album, artist
} }
func persist(mfRepo domain.MediaFileRepository, mf *domain.MediaFile, albumRepo domain.AlbumRepository, album *domain.Album, artistRepo domain.ArtistRepository, artist *domain.Artist) { func (i *Importer) persist(mf *domain.MediaFile, album *domain.Album, artist *domain.Artist) {
if err := artistRepo.Put(artist); err != nil { if err := i.artistRepo.Put(artist); err != nil {
beego.Error(err) beego.Error(err)
} }
album.ArtistId = artist.Id album.ArtistId = artist.Id
if err := albumRepo.Put(album); err != nil { if err := i.albumRepo.Put(album); err != nil {
beego.Error(err) beego.Error(err)
} }
mf.AlbumId = album.Id mf.AlbumId = album.Id
if err := mfRepo.Put(mf); err != nil { if err := i.mfRepo.Put(mf); err != nil {
beego.Error(err) beego.Error(err)
} }
} }
func collectIndex(ig utils.IndexGroups, a *domain.Artist, artistIndex map[string]tempIndex) { func (i *Importer) collectIndex(ig utils.IndexGroups, a *domain.Artist, artistIndex map[string]tempIndex) {
name := a.Name name := a.Name
indexName := strings.ToLower(utils.NoArticle(name)) indexName := strings.ToLower(utils.NoArticle(name))
if indexName == "" { if indexName == "" {
return return
} }
group := findGroup(ig, indexName) group := i.findGroup(ig, indexName)
artists := artistIndex[group] artists := artistIndex[group]
if artists == nil { if artists == nil {
artists = make(tempIndex) artists = make(tempIndex)
@@ -160,7 +178,7 @@ func collectIndex(ig utils.IndexGroups, a *domain.Artist, artistIndex map[string
artists[indexName] = domain.ArtistInfo{ArtistId: a.Id, Artist: a.Name} artists[indexName] = domain.ArtistInfo{ArtistId: a.Id, Artist: a.Name}
} }
func findGroup(ig utils.IndexGroups, name string) string { func (i *Importer) findGroup(ig utils.IndexGroups, name string) string {
for k, v := range ig { for k, v := range ig {
key := strings.ToLower(k) key := strings.ToLower(k)
if strings.HasPrefix(name, key) { if strings.HasPrefix(name, key) {
@@ -170,15 +188,13 @@ func findGroup(ig utils.IndexGroups, name string) string {
return "#" return "#"
} }
func saveIndex(artistIndex map[string]tempIndex) error { func (i *Importer) saveIndex(artistIndex map[string]tempIndex) error {
idxRepo := persistence.NewArtistIndexRepository()
for k, temp := range artistIndex { for k, temp := range artistIndex {
idx := &domain.ArtistIndex{Id: k} idx := &domain.ArtistIndex{Id: k}
for _, v := range temp { for _, v := range temp {
idx.Artists = append(idx.Artists, v) idx.Artists = append(idx.Artists, v)
} }
err := idxRepo.Put(idx) err := i.idxRepo.Put(idx)
if err != nil { if err != nil {
return err return err
} }
@@ -13,11 +13,13 @@ func TestCollectIndex(t *testing.T) {
ig := utils.IndexGroups{"A": "A", "B": "B", "Tom": "Tom", "X": "X-Z"} ig := utils.IndexGroups{"A": "A", "B": "B", "Tom": "Tom", "X": "X-Z"}
importer := &Importer{}
Convey("Simple Name", t, func() { Convey("Simple Name", t, func() {
a := &domain.Artist{Name: "Björk"} a := &domain.Artist{Name: "Björk"}
artistIndex := make(map[string]tempIndex) artistIndex := make(map[string]tempIndex)
collectIndex(ig, a, artistIndex) importer.collectIndex(ig, a, artistIndex)
So(artistIndex, ShouldContainKey, "B") So(artistIndex, ShouldContainKey, "B")
So(artistIndex["B"], ShouldContainKey, "björk") So(artistIndex["B"], ShouldContainKey, "björk")
@@ -31,7 +33,7 @@ func TestCollectIndex(t *testing.T) {
a := &domain.Artist{Name: "Kraftwerk"} a := &domain.Artist{Name: "Kraftwerk"}
artistIndex := make(map[string]tempIndex) artistIndex := make(map[string]tempIndex)
collectIndex(ig, a, artistIndex) importer.collectIndex(ig, a, artistIndex)
So(artistIndex, ShouldContainKey, "#") So(artistIndex, ShouldContainKey, "#")
So(artistIndex["#"], ShouldContainKey, "kraftwerk") So(artistIndex["#"], ShouldContainKey, "kraftwerk")
@@ -45,7 +47,7 @@ func TestCollectIndex(t *testing.T) {
a := &domain.Artist{Name: "The The"} a := &domain.Artist{Name: "The The"}
artistIndex := make(map[string]tempIndex) artistIndex := make(map[string]tempIndex)
collectIndex(ig, a, artistIndex) importer.collectIndex(ig, a, artistIndex)
So(artistIndex, ShouldContainKey, "#") So(artistIndex, ShouldContainKey, "#")
So(artistIndex["#"], ShouldContainKey, "the") So(artistIndex["#"], ShouldContainKey, "the")
@@ -59,7 +61,7 @@ func TestCollectIndex(t *testing.T) {
a := &domain.Artist{Name: "Tom Waits"} a := &domain.Artist{Name: "Tom Waits"}
artistIndex := make(map[string]tempIndex) artistIndex := make(map[string]tempIndex)
collectIndex(ig, a, artistIndex) importer.collectIndex(ig, a, artistIndex)
So(artistIndex, ShouldContainKey, "Tom") So(artistIndex, ShouldContainKey, "Tom")
So(artistIndex["Tom"], ShouldContainKey, "tom waits") So(artistIndex["Tom"], ShouldContainKey, "tom waits")