-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicbrainz.go
203 lines (167 loc) · 4.88 KB
/
musicbrainz.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bufio"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"io/ioutil"
"github.com/michiwend/gomusicbrainz"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
var errNoRelease = errors.New("couldn't find a release")
type musicBrainzRecording struct {
year string
albumTitle string
cdNum int64
trackNum int64
trackCount int64
albumArtist string
trackArtists []string
trackTitle string
}
type musicBrainzRelease struct {
artist string
title string
year string
tracks []*gomusicbrainz.Track
}
func scanLines(scanTo map[string]*string) {
for k, v := range scanTo {
fmt.Printf("%s [%s]: ", k, *v)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
s := scanner.Text()
if s != "" {
*v = s
}
}
}
func getArtistAlbumOrTrack(query string) (artist string, release string) {
info := strings.Split(query, "-")
if len(info) > 1 {
artist = strings.TrimSpace(info[0])
release = strings.TrimSpace(info[1])
}
return
}
func getTrackInfo(client *gomusicbrainz.WS2Client, query string) (musicBrainzRecording, error) {
var recording musicBrainzRecording
artist, track := getArtistAlbumOrTrack(query)
scanTo := map[string]*string{
"Artist": &artist,
"Track": &track,
}
scanLines(scanTo)
query = fmt.Sprintf(`artist:"%s" AND %s`, strings.TrimSpace(artist), strings.TrimSpace(track))
req, err := http.NewRequest("GET", "https://musicbrainz.org/ws/2/recording/", nil)
if err != nil {
return recording, err
}
q := req.URL.Query()
q.Add("query", query)
q.Add("fmt", "json")
req.URL.RawQuery = q.Encode()
req.Header.Set("User-Agent", fmt.Sprintf("%s/%s ( %s )", appName, version, contactURL))
fmt.Println("\nSearching track on musicbrainz: ")
httpClient := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := httpClient.Do(req)
if err != nil {
return recording, err
}
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return recording, errors.Wrap(err, "reading response body failed")
}
proceed := true
value := gjson.GetBytes(json, "recordings")
value.ForEach(func(key, value gjson.Result) bool {
var artists []string
value.Get("artist-credit.#.artist.name").ForEach(func(key, value gjson.Result) bool {
artists = append(artists, value.String())
return true
})
trackTitle := value.Get("title").String()
value.Get("releases").ForEach(func(key, release gjson.Result) bool {
release.Get("media").ForEach(func(key, media gjson.Result) bool {
recording = musicBrainzRecording{
albumArtist: artist,
trackArtists: artists,
cdNum: media.Get("position").Int(),
trackCount: media.Get("track-count").Int(),
trackNum: media.Get("track-offset").Int() + 1,
albumTitle: release.Get("title").String(),
year: release.Get("date").String(),
trackTitle: trackTitle,
}
if len(recording.year) >= 4 {
recording.year = recording.year[0:4]
}
fmt.Printf("Release: %s (%s)\nFormat: %s\nTrack: %.2d/%.2d %s - %s\n",
recording.albumTitle, recording.year, media.Get("format").String(), recording.trackNum, recording.trackCount, artists, recording.trackTitle)
if askForConfirmation("Choose track?") {
proceed = false
}
fmt.Println()
return proceed
})
return proceed
})
return proceed
})
if proceed == false {
return recording, nil
}
return recording, errNoRelease
}
func getAlbumInfo(client *gomusicbrainz.WS2Client, query string) (musicBrainzRelease, error) {
var mbr musicBrainzRelease
artist, release := getArtistAlbumOrTrack(query)
scanTo := map[string]*string{
"Artist": &artist,
"Release": &release,
}
scanLines(scanTo)
query = fmt.Sprintf(`artist:"%s" AND %s`, strings.TrimSpace(artist), strings.TrimSpace(release))
fmt.Println("\nSearching release on musicbrainz: ")
resp, err := client.SearchRelease(query, 5, -1)
if err != nil {
return mbr, errors.Wrap(err, "SearchRelease failed")
}
for _, release := range resp.Releases {
rec, _ := client.LookupRelease(release.Id(), "artist-credits", "labels", "discids", "recordings")
var label string
if len(rec.LabelInfos) > 0 {
label = rec.LabelInfos[0].Label.Name
}
fmt.Printf("Label: %s \nRelease: %s (%d)\n", label, release.Title, release.Date.Year())
for _, v := range rec.Mediums {
fmt.Println("Format: " + v.Format)
for _, t := range v.Tracks {
fmt.Printf("\t%s: %s - %s\n", t.Number, getArtists(t.Recording.ArtistCredit.NameCredits), t.Recording.Title)
}
if askForConfirmation("Choose release?") {
mbr.artist = artist
mbr.title = release.Title
mbr.year = strconv.Itoa(rec.Date.Year())
mbr.tracks = v.Tracks
return mbr, nil
}
fmt.Println()
}
}
return mbr, errNoRelease
}
func getArtists(nc []gomusicbrainz.NameCredit) []string {
artists := make([]string, 0, len(nc))
for _, v := range nc {
artists = append(artists, v.Artist.Name)
}
return artists
}