Implemented first repository using tiedot
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"github.com/dhowden/itl"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ItunesScanner struct {}
|
||||
|
||||
func (s *ItunesScanner) LoadFolder(path string) []Track {
|
||||
xml, _ := os.Open(path)
|
||||
l, _ := itl.ReadFromXML(xml)
|
||||
|
||||
mediaFiles := make([]Track, len(l.Tracks))
|
||||
i := 0
|
||||
for id, t := range l.Tracks {
|
||||
if t.Location != "" && strings.Contains(t.Kind, "audio") {
|
||||
mediaFiles[i].Id = id
|
||||
mediaFiles[i].Album = t.Album
|
||||
mediaFiles[i].Title = t.Name
|
||||
mediaFiles[i].Artist = t.Artist
|
||||
path, _ = url.QueryUnescape(t.Location)
|
||||
mediaFiles[i].Path = strings.TrimPrefix(path, "file://")
|
||||
mediaFiles[i].CreatedAt = t.DateAdded
|
||||
mediaFiles[i].UpdatedAt = t.DateModified
|
||||
i++
|
||||
}
|
||||
}
|
||||
return mediaFiles[0:i]
|
||||
}
|
||||
|
||||
var _ Scanner = (*ItunesScanner)(nil)
|
||||
@@ -0,0 +1,42 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/repositories"
|
||||
"github.com/deluan/gosonic/models"
|
||||
)
|
||||
|
||||
type Scanner interface {
|
||||
LoadFolder(path string) []Track
|
||||
}
|
||||
|
||||
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)
|
||||
updateDatastore(files)
|
||||
beego.Info("Finished importing", len(files), "files")
|
||||
}
|
||||
|
||||
func updateDatastore(files []Track) {
|
||||
mfRepo := repositories.NewMediaFileRepository()
|
||||
for _, t := range files {
|
||||
m := &models.MediaFile{
|
||||
Id: t.Id,
|
||||
Album: t.Album,
|
||||
Artist: t.Artist,
|
||||
Title: t.Title,
|
||||
Path: t.Path,
|
||||
CreatedAt: t.CreatedAt,
|
||||
UpdatedAt: t.UpdatedAt,
|
||||
}
|
||||
err := mfRepo.Add(m)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
}
|
||||
}
|
||||
mfRepo.Dump()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Track struct {
|
||||
Id string
|
||||
Path string
|
||||
Album string
|
||||
Artist string
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
Reference in New Issue
Block a user