-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagger.go
152 lines (120 loc) · 2.54 KB
/
tagger.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package acoustid
import (
"os"
"path/filepath"
"strings"
id3 "github.com/mikkyang/id3-go"
)
type match struct {
filename string
result Result
album string
}
type tagInfo struct {
Artist string
Title string
Album string
}
func (ti *tagInfo) setOn(path string) {
file, err := id3.Open(path)
defer file.Close()
if err != nil {
panic(err)
}
file.SetArtist(ti.Artist)
file.SetTitle(ti.Title)
if ti.Album != "" {
file.SetAlbum(ti.Album)
}
}
func TagDir(dir string) {
filepath.Walk(dir, tagFile)
}
func tagFile(file string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && strings.HasSuffix(info.Name(), ".mp3") {
//Get fingerprint
fp := NewFingerprint(file)
//Get acoustic Id
resp := MakeAcoustIDRequest(fp)
//Set ID3 tag
SetID3(resp, file, info)
}
return nil
}
func MakeAcoustIDRequest(fp Fingerprint) AcoustIDResponse {
apiKey := os.Getenv("ACOUSTID_API_KEY")
if apiKey == "" {
panic("Acoustid api key not in env ( ACOUSTID_API_KEY )")
}
request := AcoustIDRequest{
Fingerprint: fp.fingerprint,
Duration: fp.duration,
ApiKey: apiKey,
Metadata: "recordings+releasegroups+compress",
}
return request.Do()
}
func SetID3(resp AcoustIDResponse, file string, info os.FileInfo) {
// invalid response?
if resp.Status != "ok" || len(resp.Results) == 0 {
return
}
matches := []match{}
for _, result := range resp.Results {
if len(result.Recordings) == 0 || result.Score < 0.7 {
continue
}
firstRec := result.Recordings[0]
if firstRec.Title == "" || len(firstRec.Artists) == 0 {
continue
}
if firstRec.Artists[0].Name == "" {
continue
}
var bestRg string
Outer:
for _, rg := range firstRec.ReleaseGroups {
// ignore singles
if rg.Type != "Album" {
continue
}
secondaries := rg.SecondaryTypes
// ignore compilations and live releases
for _, st := range secondaries {
if st == "Compilation" || st == "Live" {
continue Outer
}
}
bestRg = rg.Title
break
}
m := match{
result: result,
filename: info.Name(),
album: bestRg,
}
matches = append(matches, m)
}
// no matches
if len(matches) <= 0 {
return
}
var maxScore float64 = -999
var bestMatch match
for _, m := range matches {
if m.result.Score > maxScore {
maxScore = m.result.Score
bestMatch = m
}
}
setTagsFromMatch(file, bestMatch)
}
func setTagsFromMatch(file string, m match) {
rec := m.result.Recordings[0]
tags := tagInfo{
Artist: rec.Artists[0].Name,
Title: rec.Title,
Album: m.album,
}
tags.setOn(file)
}