mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19:28 +02:00
ListenBrainz: Support import of loves without recording MBID
This commit is contained in:
parent
1b5ea241b6
commit
4d18a207ee
6 changed files with 68 additions and 5 deletions
|
@ -125,3 +125,22 @@ func (c Client) SendFeedback(feedback Feedback) (result StatusResult, err error)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Client) Lookup(recordingName string, artistName string) (result LookupResult, err error) {
|
||||||
|
const path = "/metadata/lookup"
|
||||||
|
errorResult := ErrorResult{}
|
||||||
|
response, err := c.HttpClient.R().
|
||||||
|
SetQueryParams(map[string]string{
|
||||||
|
"recording_name": recordingName,
|
||||||
|
"artist_name": artistName,
|
||||||
|
}).
|
||||||
|
SetResult(&result).
|
||||||
|
SetError(&errorResult).
|
||||||
|
Get(path)
|
||||||
|
|
||||||
|
if response.StatusCode() != 200 {
|
||||||
|
err = errors.New(errorResult.Error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -100,6 +100,23 @@ func TestSendFeedback(t *testing.T) {
|
||||||
assert.Equal(t, "ok", result.Status)
|
assert.Equal(t, "ok", result.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLookup(t *testing.T) {
|
||||||
|
defer httpmock.DeactivateAndReset()
|
||||||
|
|
||||||
|
client := listenbrainz.NewClient("thetoken")
|
||||||
|
setupHttpMock(t, client.HttpClient.GetClient(),
|
||||||
|
"https://api.listenbrainz.org/1/metadata/lookup",
|
||||||
|
"testdata/lookup.json")
|
||||||
|
|
||||||
|
result, err := client.Lookup("Paradise Lost", "Say Just Words")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert := assert.New(t)
|
||||||
|
assert.Equal("Say Just Words", result.RecordingName)
|
||||||
|
assert.Equal("Paradise Lost", result.ArtistCreditName)
|
||||||
|
assert.Equal("569436a1-234a-44bc-a370-8f4d252bef21", result.RecordingMbid)
|
||||||
|
}
|
||||||
|
|
||||||
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {
|
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {
|
||||||
httpmock.ActivateNonDefault(client)
|
httpmock.ActivateNonDefault(client)
|
||||||
|
|
||||||
|
|
|
@ -132,15 +132,23 @@ func (b ListenBrainzApiBackend) ImportLoves(loves []models.Love, oldestTimestamp
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Support love import without recording MBID
|
recordingMbid := string(love.RecordingMbid)
|
||||||
if love.RecordingMbid != "" {
|
|
||||||
|
if recordingMbid == "" {
|
||||||
|
lookup, err := b.client.Lookup(love.TrackName, love.ArtistName())
|
||||||
|
if err == nil {
|
||||||
|
recordingMbid = lookup.RecordingMbid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if recordingMbid != "" {
|
||||||
ok := false
|
ok := false
|
||||||
errMsg := ""
|
errMsg := ""
|
||||||
if existingMbids[string(love.RecordingMbid)] {
|
if existingMbids[recordingMbid] {
|
||||||
ok = true
|
ok = true
|
||||||
} else {
|
} else {
|
||||||
resp, err := b.client.SendFeedback(Feedback{
|
resp, err := b.client.SendFeedback(Feedback{
|
||||||
RecordingMbid: string(love.RecordingMbid),
|
RecordingMbid: recordingMbid,
|
||||||
Score: 1,
|
Score: 1,
|
||||||
})
|
})
|
||||||
ok = err == nil && resp.Status == "ok"
|
ok = err == nil && resp.Status == "ok"
|
||||||
|
|
|
@ -85,6 +85,15 @@ type Feedback struct {
|
||||||
UserName string `json:"user_id,omitempty"`
|
UserName string `json:"user_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LookupResult struct {
|
||||||
|
ArtistCreditName string `json:"artist_credit_name"`
|
||||||
|
ReleaseName string `json:"release_name"`
|
||||||
|
RecordingName string `json:"recording_name"`
|
||||||
|
RecordingMbid string `json:"recording_mbid"`
|
||||||
|
ReleaseMbid string `json:"release_mbid"`
|
||||||
|
ArtistMbids []string `json:"artist_mbids"`
|
||||||
|
}
|
||||||
|
|
||||||
type StatusResult struct {
|
type StatusResult struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
10
backends/listenbrainz/testdata/lookup.json
vendored
Normal file
10
backends/listenbrainz/testdata/lookup.json
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"artist_credit_name": "Paradise Lost",
|
||||||
|
"artist_mbids": [
|
||||||
|
"10bf95b6-30e3-44f1-817f-45762cdc0de0"
|
||||||
|
],
|
||||||
|
"recording_mbid": "569436a1-234a-44bc-a370-8f4d252bef21",
|
||||||
|
"recording_name": "Say Just Words",
|
||||||
|
"release_mbid": "90b2d144-e5f3-3192-9da5-0d72d67c61be",
|
||||||
|
"release_name": "One Second"
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue