quintodrome/scanner/itunes_scanner.go

59 lines
1.5 KiB
Go
Raw Normal View History

package scanner
import (
2016-03-02 21:07:13 -09:00
"github.com/deluan/itl"
"net/url"
"os"
2016-03-02 19:51:26 -09:00
"path/filepath"
2016-03-02 20:07:10 -09:00
"strconv"
2016-03-02 20:46:23 -09:00
"strings"
)
2016-03-02 09:18:39 -09:00
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 {
2016-03-01 15:50:20 -09:00
if strings.HasPrefix(t.Location, "file://") && strings.Contains(t.Kind, "audio") {
mediaFiles[i].Id = id
2016-03-01 14:19:57 -09:00
mediaFiles[i].Album = unescape(t.Album)
mediaFiles[i].Title = unescape(t.Name)
mediaFiles[i].Artist = unescape(t.Artist)
mediaFiles[i].AlbumArtist = unescape(t.AlbumArtist)
mediaFiles[i].Genre = unescape(t.Genre)
2016-02-26 23:35:01 -09:00
mediaFiles[i].Compilation = t.Compilation
2016-03-02 21:07:13 -09:00
mediaFiles[i].Loved = t.Loved
mediaFiles[i].AlbumLoved = t.AlbumLoved
2016-02-28 09:50:05 -09:00
mediaFiles[i].Year = t.Year
mediaFiles[i].TrackNumber = t.TrackNumber
mediaFiles[i].DiscNumber = t.DiscNumber
2016-03-02 19:51:26 -09:00
if t.Size > 0 {
mediaFiles[i].Size = strconv.Itoa(t.Size)
}
if t.TotalTime > 0 {
mediaFiles[i].Duration = t.TotalTime / 1000
}
mediaFiles[i].BitRate = t.BitRate
path, _ = url.QueryUnescape(t.Location)
2016-03-03 06:34:17 -09:00
path = strings.TrimPrefix(unescape(path), "file://")
2016-03-02 19:51:26 -09:00
mediaFiles[i].Path = path
mediaFiles[i].Suffix = strings.TrimPrefix(filepath.Ext(path), ".")
mediaFiles[i].CreatedAt = t.DateAdded
mediaFiles[i].UpdatedAt = t.DateModified
i++
}
}
return mediaFiles[0:i]
}
2016-03-02 20:07:10 -09:00
func unescape(str string) string {
s := strings.Replace(str, "&", "&", -1)
return s
2016-03-01 14:19:57 -09:00
}
2016-03-02 09:18:39 -09:00
var _ Scanner = (*ItunesScanner)(nil)