ListenBrainz: Support import of loves without recording MBID

This commit is contained in:
Philipp Wolfer 2023-11-13 18:49:20 +01:00
parent 1b5ea241b6
commit 4d18a207ee
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
6 changed files with 68 additions and 5 deletions

View file

@ -125,3 +125,22 @@ func (c Client) SendFeedback(feedback Feedback) (result StatusResult, err error)
}
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
}

View file

@ -100,6 +100,23 @@ func TestSendFeedback(t *testing.T) {
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) {
httpmock.ActivateNonDefault(client)

View file

@ -132,15 +132,23 @@ func (b ListenBrainzApiBackend) ImportLoves(loves []models.Love, oldestTimestamp
continue
}
// TODO: Support love import without recording MBID
if love.RecordingMbid != "" {
recordingMbid := string(love.RecordingMbid)
if recordingMbid == "" {
lookup, err := b.client.Lookup(love.TrackName, love.ArtistName())
if err == nil {
recordingMbid = lookup.RecordingMbid
}
}
if recordingMbid != "" {
ok := false
errMsg := ""
if existingMbids[string(love.RecordingMbid)] {
if existingMbids[recordingMbid] {
ok = true
} else {
resp, err := b.client.SendFeedback(Feedback{
RecordingMbid: string(love.RecordingMbid),
RecordingMbid: recordingMbid,
Score: 1,
})
ok = err == nil && resp.Status == "ok"

View file

@ -85,6 +85,15 @@ type Feedback struct {
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 {
Status string `json:"status"`
}

View 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"
}