mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 13:47:05 +02:00
Moved specifc backends into separate packages
This commit is contained in:
parent
dfaf21b234
commit
48c8843f91
17 changed files with 127 additions and 98 deletions
|
@ -38,7 +38,7 @@ type Client struct {
|
|||
MaxResults int
|
||||
}
|
||||
|
||||
func New(token string) Client {
|
||||
func NewClient(token string) Client {
|
||||
resty := resty.New()
|
||||
resty.SetBaseURL(listenBrainzBaseURL)
|
||||
resty.SetAuthScheme("Token")
|
||||
|
|
|
@ -34,7 +34,7 @@ import (
|
|||
|
||||
func TestNewClient(t *testing.T) {
|
||||
token := "foobar123"
|
||||
client := listenbrainz.New(token)
|
||||
client := listenbrainz.NewClient(token)
|
||||
assert.Equal(t, token, client.HttpClient.Token)
|
||||
assert.Equal(t, listenbrainz.DefaultItemsPerGet, client.MaxResults)
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ func TestGetListens(t *testing.T) {
|
|||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
token := "thetoken"
|
||||
client := listenbrainz.New(token)
|
||||
client := listenbrainz.NewClient(token)
|
||||
setupHttpMock(t, client.HttpClient.GetClient(),
|
||||
"https://api.listenbrainz.org/1/user/outsidecontext/listens",
|
||||
"testdata/listens.json")
|
||||
|
|
98
backends/listenbrainz/listenbrainz.go
Normal file
98
backends/listenbrainz/listenbrainz.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package listenbrainz
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/models"
|
||||
)
|
||||
|
||||
type ListenBrainzApiBackend struct {
|
||||
client Client
|
||||
username string
|
||||
}
|
||||
|
||||
func (b ListenBrainzApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.client = NewClient(config.GetString("token"))
|
||||
b.client.MaxResults = MaxItemsPerGet
|
||||
b.username = config.GetString("username")
|
||||
return b
|
||||
}
|
||||
|
||||
func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]models.Listen, error) {
|
||||
maxTime := time.Now()
|
||||
minTime := time.Unix(0, 0)
|
||||
listens := make([]models.Listen, 0)
|
||||
|
||||
out:
|
||||
for {
|
||||
result, err := b.client.GetListens(b.username, maxTime, minTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count := len(result.Payload.Listens)
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Set maxTime to the oldest returned listen
|
||||
maxTime = time.Unix(result.Payload.Listens[count-1].ListenedAt, 0)
|
||||
|
||||
for _, listen := range result.Payload.Listens {
|
||||
if listen.ListenedAt > oldestTimestamp.Unix() {
|
||||
listens = append(listens, ListenFromListenBrainz(listen))
|
||||
} else {
|
||||
// result contains listens older then oldestTimestamp,
|
||||
// we can stop requesting more
|
||||
break out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slices.Reverse(listens)
|
||||
return listens, nil
|
||||
}
|
||||
|
||||
func ListenFromListenBrainz(lbListen Listen) models.Listen {
|
||||
track := lbListen.TrackMetadata
|
||||
listen := models.Listen{
|
||||
ListenedAt: time.Unix(lbListen.ListenedAt, 0),
|
||||
UserName: lbListen.UserName,
|
||||
Track: models.Track{
|
||||
TrackName: track.TrackName,
|
||||
ReleaseName: track.ReleaseName,
|
||||
ArtistNames: []string{track.ArtistName},
|
||||
Duration: track.Duration(),
|
||||
TrackNumber: track.TrackNumber(),
|
||||
RecordingMbid: models.MBID(track.RecordingMbid()),
|
||||
ReleaseMbid: models.MBID(track.ReleaseMbid()),
|
||||
ReleaseGroupMbid: models.MBID(track.ReleaseGroupMbid()),
|
||||
Isrc: track.Isrc(),
|
||||
AdditionalInfo: track.AdditionalInfo,
|
||||
},
|
||||
}
|
||||
return listen
|
||||
}
|
65
backends/listenbrainz/listenbrainz_test.go
Normal file
65
backends/listenbrainz/listenbrainz_test.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package listenbrainz_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/backends/listenbrainz"
|
||||
"go.uploadedlobster.com/scotty/models"
|
||||
)
|
||||
|
||||
func TestListenFromListenBrainz(t *testing.T) {
|
||||
lbListen := listenbrainz.Listen{
|
||||
ListenedAt: 1699289873,
|
||||
UserName: "outsidecontext",
|
||||
TrackMetadata: listenbrainz.Track{
|
||||
TrackName: "The Track",
|
||||
ArtistName: "The Artist",
|
||||
ReleaseName: "The Release",
|
||||
AdditionalInfo: map[string]any{
|
||||
"duration_ms": 528235,
|
||||
"foo": "bar",
|
||||
"isrc": "DES561720901",
|
||||
"tracknumber": 8,
|
||||
"recording_mbid": "e225fb84-dc9a-419e-adcd-9890f59ec432",
|
||||
"release_group_mbid": "80aca1ee-aa51-41be-9f75-024710d92ff4",
|
||||
"release_mbid": "d7f22677-9803-4d21-ba42-081b633a6f68",
|
||||
},
|
||||
},
|
||||
}
|
||||
listen := listenbrainz.ListenFromListenBrainz(lbListen)
|
||||
assert.Equal(t, time.Unix(1699289873, 0), listen.ListenedAt)
|
||||
assert.Equal(t, lbListen.UserName, listen.UserName)
|
||||
assert.Equal(t, time.Duration(528235*time.Millisecond), listen.Duration)
|
||||
assert.Equal(t, lbListen.TrackMetadata.TrackName, listen.TrackName)
|
||||
assert.Equal(t, lbListen.TrackMetadata.ReleaseName, listen.ReleaseName)
|
||||
assert.Equal(t, []string{lbListen.TrackMetadata.ArtistName}, listen.ArtistNames)
|
||||
assert.Equal(t, 8, listen.TrackNumber)
|
||||
assert.Equal(t, models.MBID("e225fb84-dc9a-419e-adcd-9890f59ec432"), listen.RecordingMbid)
|
||||
assert.Equal(t, models.MBID("d7f22677-9803-4d21-ba42-081b633a6f68"), listen.ReleaseMbid)
|
||||
assert.Equal(t, models.MBID("80aca1ee-aa51-41be-9f75-024710d92ff4"), listen.ReleaseGroupMbid)
|
||||
assert.Equal(t, "DES561720901", listen.Isrc)
|
||||
assert.Equal(t, lbListen.TrackMetadata.AdditionalInfo["foo"], listen.AdditionalInfo["foo"])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue