-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata_provider.go
48 lines (42 loc) · 938 Bytes
/
metadata_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package metadata
import (
"github.com/LogicalOverflow/music-sync/playback"
"github.com/LogicalOverflow/music-sync/util"
"github.com/dhowden/tag"
"os"
"path/filepath"
)
// Provider is used to get metadata for songs
type Provider interface {
CollectMetadata(song string) SongMetadata
}
// SongMetadata holds the metadata for a song
type SongMetadata struct {
Title string
Artist string
Album string
}
// GetProvider returns a new Provider
func GetProvider() Provider {
return basicProvider{}
}
type basicProvider struct{}
func (basicProvider) CollectMetadata(song string) SongMetadata {
path := filepath.Join(playback.AudioDir, song)
if !util.IsFile(path) {
return SongMetadata{}
}
f, err := os.Open(path)
if err != nil {
return SongMetadata{}
}
md, err := tag.ReadFrom(f)
if err != nil {
return SongMetadata{}
}
return SongMetadata{
Title: md.Title(),
Artist: md.Artist(),
Album: md.Album(),
}
}