mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
Scanning artists and albums too
This commit is contained in:
parent
bccfeec2d3
commit
14e52576a7
11 changed files with 209 additions and 51 deletions
|
@ -1,12 +1,34 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Album struct {
|
type Album struct {
|
||||||
Id string
|
Id string
|
||||||
Name string
|
Name string
|
||||||
Artist *Artist
|
ArtistId string
|
||||||
CoverArtPath string
|
CoverArtPath string
|
||||||
Year int
|
Year int
|
||||||
Compilation bool
|
Compilation bool
|
||||||
Rating int
|
Rating int
|
||||||
|
MediaFiles map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Album) AdMediaFiles(mfs ...*MediaFile) {
|
||||||
|
for _, mf := range mfs {
|
||||||
|
a.MediaFiles[mf.Id] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Album) AddMediaFiles(mfs ...interface{}) {
|
||||||
|
if a.MediaFiles == nil {
|
||||||
|
a.MediaFiles = make(map[string]bool)
|
||||||
|
}
|
||||||
|
for _, v := range mfs {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case *MediaFile:
|
||||||
|
a.MediaFiles[v.Id] = true
|
||||||
|
case map[string]bool:
|
||||||
|
for k, _ := range v {
|
||||||
|
a.MediaFiles[k] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,8 +6,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Artist struct {
|
type Artist struct {
|
||||||
Id string
|
Id string
|
||||||
Name string
|
Name string
|
||||||
|
Albums map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NoArticle(name string) string {
|
func NoArticle(name string) string {
|
||||||
|
@ -19,4 +20,20 @@ func NoArticle(name string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return name
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Artist) AddAlbums(albums ...interface{}) {
|
||||||
|
if a.Albums == nil {
|
||||||
|
a.Albums = make(map[string]bool)
|
||||||
|
}
|
||||||
|
for _, v := range albums {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case *Album:
|
||||||
|
a.Albums[v.Id] = true
|
||||||
|
case map[string]bool:
|
||||||
|
for k, _ := range v {
|
||||||
|
a.Albums[k] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
34
repositories/album_repository.go
Normal file
34
repositories/album_repository.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/deluan/gosonic/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Album struct {
|
||||||
|
BaseRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAlbumRepository() *Album {
|
||||||
|
r := &Album{}
|
||||||
|
r.key = "album"
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Album) Put(m *models.Album) (*models.Album, error) {
|
||||||
|
if m.Id == "" {
|
||||||
|
m.Id = r.NewId(m.Name)
|
||||||
|
}
|
||||||
|
return m, r.saveOrUpdate(m.Id, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Album) Get(id string) (*models.Album, error) {
|
||||||
|
rec := &models.Album{}
|
||||||
|
err := readStruct(r.key, id, rec)
|
||||||
|
return rec, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Album) GetByName(name string) (*models.Album, error) {
|
||||||
|
id := r.NewId(name)
|
||||||
|
return r.Get(id)
|
||||||
|
}
|
||||||
|
|
34
repositories/artist_repository.go
Normal file
34
repositories/artist_repository.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/deluan/gosonic/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Artist struct {
|
||||||
|
BaseRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewArtistRepository() *Artist {
|
||||||
|
r := &Artist{}
|
||||||
|
r.key = "artist"
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Artist) Put(m *models.Artist) (*models.Artist, error) {
|
||||||
|
if m.Id == "" {
|
||||||
|
m.Id = r.NewId(m.Name)
|
||||||
|
}
|
||||||
|
return m, r.saveOrUpdate(m.Id, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Artist) Get(id string) (*models.Artist, error) {
|
||||||
|
rec := &models.Artist{}
|
||||||
|
err := readStruct(r.key, id, rec)
|
||||||
|
return rec, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Artist) GetByName(name string) (*models.Artist, error) {
|
||||||
|
id := r.NewId(name)
|
||||||
|
return r.Get(id)
|
||||||
|
}
|
||||||
|
|
|
@ -1,18 +1,28 @@
|
||||||
package repositories
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"crypto/md5"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type BaseRepository struct {
|
type BaseRepository struct {
|
||||||
key string
|
key string // TODO Rename to 'table'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *BaseRepository) NewId(fields ...string) string {
|
||||||
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
|
s := fmt.Sprintf("%s\\%s", strings.ToUpper(r.key), strings.Join(fields, ""))
|
||||||
return saveStruct(r.key, id, rec)
|
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *BaseRepository) CountAll() (int, error) {
|
func (r *BaseRepository) CountAll() (int, error) {
|
||||||
return count(r.key)
|
return count(r.key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
|
||||||
|
return saveStruct(r.key, id, rec)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *BaseRepository) Dump() {
|
func (r *BaseRepository) Dump() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,8 +46,9 @@ func saveStruct(key, id string, data interface{}) error {
|
||||||
return db().HMset([]byte(kh), fvList...)
|
return db().HMset([]byte(kh), fvList...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readStruct(key string) (interface{}, error) {
|
func readStruct(key, id string, rec interface{}) error {
|
||||||
fvs, _ := db().HGetAll([]byte(key))
|
kh := key + "_id_" + id
|
||||||
|
fvs, _ := db().HGetAll([]byte(kh))
|
||||||
var m = make(map[string]interface{}, len(fvs))
|
var m = make(map[string]interface{}, len(fvs))
|
||||||
for _, fv := range fvs {
|
for _, fv := range fvs {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
|
@ -55,15 +56,10 @@ func readStruct(key string) (interface{}, error) {
|
||||||
m[string(fv.Field)] = v
|
m[string(fv.Field)] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils.ToStruct(m)
|
return utils.ToStruct(m, rec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func count(key string) (int, error) {
|
func count(key string) (int, error) {
|
||||||
ids, err := db().SMembers([]byte(key + "_ids"))
|
ids, err := db().SMembers([]byte(key + "_ids"))
|
||||||
return len(ids), err
|
return len(ids), err
|
||||||
}
|
|
||||||
|
|
||||||
func hset(key, field, value string) error {
|
|
||||||
_, err := db().HSet([]byte(key), []byte(field), []byte(value))
|
|
||||||
return err
|
|
||||||
}
|
}
|
|
@ -2,8 +2,6 @@ package repositories
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/deluan/gosonic/models"
|
"github.com/deluan/gosonic/models"
|
||||||
"fmt"
|
|
||||||
"crypto/md5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MediaFile struct {
|
type MediaFile struct {
|
||||||
|
@ -16,9 +14,6 @@ func NewMediaFileRepository() *MediaFile {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *MediaFile) Add(m *models.MediaFile) error {
|
func (r *MediaFile) Put(m *models.MediaFile) error {
|
||||||
if m.Id == "" {
|
|
||||||
m.Id = fmt.Sprintf("%x", md5.Sum([]byte(m.Path)))
|
|
||||||
}
|
|
||||||
return r.saveOrUpdate(m.Id, m)
|
return r.saveOrUpdate(m.Id, m)
|
||||||
}
|
}
|
|
@ -24,6 +24,7 @@ func (s *ItunesScanner) LoadFolder(path string) []Track {
|
||||||
mediaFiles[i].Artist = t.Artist
|
mediaFiles[i].Artist = t.Artist
|
||||||
mediaFiles[i].AlbumArtist = t.AlbumArtist
|
mediaFiles[i].AlbumArtist = t.AlbumArtist
|
||||||
mediaFiles[i].Compilation = t.Compilation
|
mediaFiles[i].Compilation = t.Compilation
|
||||||
|
mediaFiles[i].Year = t.Year
|
||||||
path, _ = url.QueryUnescape(t.Location)
|
path, _ = url.QueryUnescape(t.Location)
|
||||||
mediaFiles[i].Path = strings.TrimPrefix(path, "file://")
|
mediaFiles[i].Path = strings.TrimPrefix(path, "file://")
|
||||||
mediaFiles[i].CreatedAt = t.DateAdded
|
mediaFiles[i].CreatedAt = t.DateAdded
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/deluan/gosonic/repositories"
|
"github.com/deluan/gosonic/repositories"
|
||||||
"github.com/deluan/gosonic/models"
|
"github.com/deluan/gosonic/models"
|
||||||
"strings"
|
"strings"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Scanner interface {
|
type Scanner interface {
|
||||||
|
@ -18,38 +19,86 @@ func StartImport() {
|
||||||
func doImport(mediaFolder string, scanner Scanner) {
|
func doImport(mediaFolder string, scanner Scanner) {
|
||||||
beego.Info("Starting iTunes import from:", mediaFolder)
|
beego.Info("Starting iTunes import from:", mediaFolder)
|
||||||
files := scanner.LoadFolder(mediaFolder)
|
files := scanner.LoadFolder(mediaFolder)
|
||||||
updateDatastore(files)
|
importLibrary(files)
|
||||||
beego.Info("Finished importing", len(files), "files")
|
beego.Info("Finished importing", len(files), "files")
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateDatastore(files []Track) {
|
func importLibrary(files []Track) {
|
||||||
mfRepo := repositories.NewMediaFileRepository()
|
mfRepo := repositories.NewMediaFileRepository()
|
||||||
|
albumRepo := repositories.NewAlbumRepository()
|
||||||
|
artistRepo := repositories.NewArtistRepository()
|
||||||
var artistIndex = make(map[string]map[string]string)
|
var artistIndex = make(map[string]map[string]string)
|
||||||
|
|
||||||
for _, t := range files {
|
for _, t := range files {
|
||||||
m := &models.MediaFile{
|
mf, album, artist := processTrack(&t)
|
||||||
Id: t.Id,
|
mergeInfo(mfRepo, mf, albumRepo, album, artistRepo, artist)
|
||||||
Album: t.Album,
|
fmt.Printf("%#v\n", album)
|
||||||
Artist: t.Artist,
|
fmt.Printf("%#v\n\n", artist)
|
||||||
AlbumArtist: t.AlbumArtist,
|
collectIndex(mf, artistIndex)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
//mfRepo.Dump()
|
|
||||||
//j,_ := json.MarshalIndent(artistIndex, "", " ")
|
//j,_ := json.MarshalIndent(artistIndex, "", " ")
|
||||||
//fmt.Println(string(j))
|
//fmt.Println(string(j))
|
||||||
c, _ := mfRepo.CountAll()
|
c, _ := mfRepo.CountAll()
|
||||||
|
|
||||||
beego.Info("Total mediafiles in database:", c)
|
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) {
|
func collectIndex(m *models.MediaFile, artistIndex map[string]map[string]string) {
|
||||||
name := m.RealArtist()
|
name := m.RealArtist()
|
||||||
indexName := strings.ToLower(models.NoArticle(name))
|
indexName := strings.ToLower(models.NoArticle(name))
|
||||||
|
|
|
@ -11,6 +11,7 @@ type Track struct {
|
||||||
Album string
|
Album string
|
||||||
Artist string
|
Artist string
|
||||||
AlbumArtist string
|
AlbumArtist string
|
||||||
|
Year int
|
||||||
Compilation bool
|
Compilation bool
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
|
|
|
@ -121,17 +121,16 @@ func ToMap(rec interface{}) (map[string]interface{}, error) {
|
||||||
return m, err
|
return m, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToStruct(m map[string]interface{}) (interface{}, error) {
|
func ToStruct(m map[string]interface{}, rec interface{}) error {
|
||||||
// Convert to JSON...
|
// Convert to JSON...
|
||||||
b, err := json.Marshal(m);
|
b, err := json.Marshal(m);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... then convert to map
|
// ... then convert to struct
|
||||||
var rec interface{}
|
|
||||||
err = json.Unmarshal(b, &rec)
|
err = json.Unmarshal(b, &rec)
|
||||||
return rec, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Flatten(input interface{}) (map[string]interface{}, error) {
|
func Flatten(input interface{}) (map[string]interface{}, error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue