diff --git a/models/index.go b/models/index.go new file mode 100644 index 000000000..89048f72e --- /dev/null +++ b/models/index.go @@ -0,0 +1,13 @@ +package models + +type ArtistInfo struct { + ArtistId string + Artist string +} + +type ArtistIndex struct { + Id string + Artists []ArtistInfo +} + + diff --git a/models/media_file.go b/models/media_file.go index 900e4920a..3751c34ab 100644 --- a/models/media_file.go +++ b/models/media_file.go @@ -14,14 +14,4 @@ type MediaFile struct { Compilation bool CreatedAt time.Time UpdatedAt time.Time -} - -func (m*MediaFile) RealArtist() string { - if (m.Compilation) { - return "Various Artists" - } - if (m.AlbumArtist != "") { - return m.AlbumArtist - } - return m.Artist } \ No newline at end of file diff --git a/repositories/artist_repository.go b/repositories/artist_repository.go index b67919c06..6772f3036 100644 --- a/repositories/artist_repository.go +++ b/repositories/artist_repository.go @@ -14,7 +14,7 @@ func NewArtistRepository() *Artist { return r } -func (r *Artist) Put(m *models.Artist) (*models.Artist, error) { +func (r *Artist) Put(m *models.Artist) (*models.Artist, error) { // TODO Return only error if m.Id == "" { m.Id = r.NewId(m.Name) } diff --git a/repositories/index_repository.go b/repositories/index_repository.go new file mode 100644 index 000000000..56fc005df --- /dev/null +++ b/repositories/index_repository.go @@ -0,0 +1,26 @@ +package repositories + +import ( + "github.com/deluan/gosonic/models" +"errors" +) + +type ArtistIndex struct { + BaseRepository +} + +func NewArtistIndexRepository() *ArtistIndex { + r := &ArtistIndex{} + r.key = "index" + return r +} + +func (r *ArtistIndex) Put(m *models.ArtistIndex) error { + if m.Id == "" { + return errors.New("Id is not set") + } + return r.saveOrUpdate(m.Id, m) +} + + + diff --git a/scanner/scanner.go b/scanner/scanner.go index 0cb5eff78..293e04bc5 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -6,12 +6,15 @@ import ( "github.com/deluan/gosonic/models" "strings" "fmt" + "encoding/json" ) type Scanner interface { LoadFolder(path string) []Track } +type tempIndex map[string]*models.ArtistInfo + func StartImport() { go doImport(beego.AppConfig.String("musicFolder"), &ItunesScanner{}) } @@ -27,20 +30,24 @@ func importLibrary(files []Track) { mfRepo := repositories.NewMediaFileRepository() albumRepo := repositories.NewAlbumRepository() artistRepo := repositories.NewArtistRepository() - var artistIndex = make(map[string]map[string]string) + var artistIndex = make(map[string]tempIndex) for _, t := range files { 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) + collectIndex(artist, artistIndex) } - //j,_ := json.MarshalIndent(artistIndex, "", " ") - //fmt.Println(string(j)) - c, _ := mfRepo.CountAll() + if err := saveIndex(artistIndex); err != nil { + beego.Error(err) + } + j,_ := json.MarshalIndent(artistIndex, "", " ") + fmt.Println(string(j)) + + c, _ := mfRepo.CountAll() beego.Info("Total mediafiles in database:", c) } @@ -64,7 +71,7 @@ func processTrack(t *Track) (*models.MediaFile, *models.Album, *models.Artist) { } artist := &models.Artist{ - Name: t.Artist, + Name: t.RealArtist(), } return mf, album, artist @@ -89,7 +96,7 @@ func mergeInfo(mfRepo *repositories.MediaFile, mf *models.MediaFile, albumRepo * beego.Error(err) } artist.AddAlbums(sAlbum, sArtist.Albums) - sArtist, err = artistRepo.Put(artist) + _, err = artistRepo.Put(artist) if err != nil { beego.Error(err) } @@ -99,8 +106,8 @@ func mergeInfo(mfRepo *repositories.MediaFile, mf *models.MediaFile, albumRepo * } } -func collectIndex(m *models.MediaFile, artistIndex map[string]map[string]string) { - name := m.RealArtist() +func collectIndex(a *models.Artist, artistIndex map[string]tempIndex) { + name := a.Name indexName := strings.ToLower(models.NoArticle(name)) if indexName == "" { return @@ -108,8 +115,25 @@ func collectIndex(m *models.MediaFile, artistIndex map[string]map[string]string) initial := strings.ToUpper(indexName[0:1]) artists := artistIndex[initial] if artists == nil { - artists = make(map[string]string) + artists = make(tempIndex) artistIndex[initial] = artists } - artists[indexName] = name + 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 } \ No newline at end of file diff --git a/scanner/track.go b/scanner/track.go index f1730d24f..97169e76a 100644 --- a/scanner/track.go +++ b/scanner/track.go @@ -15,4 +15,14 @@ type Track struct { Compilation bool CreatedAt time.Time UpdatedAt time.Time +} + +func (m *Track) RealArtist() string { + if (m.Compilation) { + return "Various Artists" + } + if (m.AlbumArtist != "") { + return m.AlbumArtist + } + return m.Artist } \ No newline at end of file