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
|
@ -35,7 +35,7 @@ type Client struct {
|
|||
token string
|
||||
}
|
||||
|
||||
func New(serverUrl string, token string) Client {
|
||||
func NewClient(serverUrl string, token string) Client {
|
||||
resty := resty.New()
|
||||
resty.SetBaseURL(serverUrl)
|
||||
resty.SetAuthScheme("Bearer")
|
||||
|
|
|
@ -34,7 +34,7 @@ import (
|
|||
func TestNewClient(t *testing.T) {
|
||||
serverUrl := "https://funkwhale.example.com"
|
||||
token := "foobar123"
|
||||
client := funkwhale.New(serverUrl, token)
|
||||
client := funkwhale.NewClient(serverUrl, token)
|
||||
assert.Equal(t, serverUrl, client.HttpClient.BaseURL)
|
||||
assert.Equal(t, token, client.HttpClient.Token)
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ func TestGetHistoryListenings(t *testing.T) {
|
|||
|
||||
token := "thetoken"
|
||||
serverUrl := "https://funkwhale.example.com"
|
||||
client := funkwhale.New(serverUrl, token)
|
||||
client := funkwhale.NewClient(serverUrl, token)
|
||||
setupHttpMock(t, client.HttpClient.GetClient(),
|
||||
"https://funkwhale.example.com/api/v1/history/listenings",
|
||||
"testdata/listenings.json")
|
||||
|
|
116
backends/funkwhale/funkwhale.go
Normal file
116
backends/funkwhale/funkwhale.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
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 funkwhale
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/models"
|
||||
)
|
||||
|
||||
const FunkwhaleClientName = "Funkwhale"
|
||||
|
||||
type FunkwhaleApiBackend struct {
|
||||
client Client
|
||||
username string
|
||||
}
|
||||
|
||||
func (b FunkwhaleApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.client = NewClient(
|
||||
config.GetString("server-url"),
|
||||
config.GetString("token"),
|
||||
)
|
||||
b.username = config.GetString("username")
|
||||
return b
|
||||
}
|
||||
|
||||
func (b FunkwhaleApiBackend) ExportListens(oldestTimestamp time.Time) ([]models.Listen, error) {
|
||||
page := 1
|
||||
perPage := MaxItemsPerGet
|
||||
|
||||
listens := make([]models.Listen, 0)
|
||||
|
||||
out:
|
||||
for {
|
||||
result, err := b.client.GetHistoryListenings(b.username, page, perPage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count := len(result.Results)
|
||||
if count == 0 {
|
||||
break out
|
||||
}
|
||||
|
||||
for _, fwListen := range result.Results {
|
||||
listen := ListenFromFunkwhale(fwListen)
|
||||
if listen.ListenedAt.Unix() > oldestTimestamp.Unix() {
|
||||
listens = append(listens, listen)
|
||||
} else {
|
||||
break out
|
||||
}
|
||||
}
|
||||
|
||||
if result.Next == "" {
|
||||
// No further results
|
||||
break out
|
||||
}
|
||||
|
||||
page += 1
|
||||
}
|
||||
|
||||
slices.Reverse(listens)
|
||||
return listens, nil
|
||||
}
|
||||
|
||||
func ListenFromFunkwhale(fwListen Listening) models.Listen {
|
||||
track := fwListen.Track
|
||||
listen := models.Listen{
|
||||
UserName: fwListen.User.UserName,
|
||||
Track: models.Track{
|
||||
TrackName: track.Title,
|
||||
ReleaseName: track.Album.Title,
|
||||
ArtistNames: []string{track.Artist.Name},
|
||||
TrackNumber: track.Position,
|
||||
RecordingMbid: models.MBID(track.RecordingMbid),
|
||||
ReleaseMbid: models.MBID(track.Album.ReleaseMbid),
|
||||
ArtistMbids: []models.MBID{models.MBID(track.Artist.ArtistMbid)},
|
||||
Tags: track.Tags,
|
||||
AdditionalInfo: map[string]any{
|
||||
"media_player": FunkwhaleClientName,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
listenedAt, err := time.Parse(time.RFC3339, fwListen.CreationDate)
|
||||
if err == nil {
|
||||
listen.ListenedAt = listenedAt
|
||||
}
|
||||
|
||||
if len(track.Uploads) > 0 {
|
||||
listen.Track.Duration = time.Duration(track.Uploads[0].Duration * int(time.Second))
|
||||
}
|
||||
|
||||
return listen
|
||||
}
|
74
backends/funkwhale/funkwhale_test.go
Normal file
74
backends/funkwhale/funkwhale_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
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 funkwhale_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/backends/funkwhale"
|
||||
"go.uploadedlobster.com/scotty/models"
|
||||
)
|
||||
|
||||
func TestListenFromFunkwhale(t *testing.T) {
|
||||
fwListen := funkwhale.Listening{
|
||||
CreationDate: "2023-11-09T23:59:29.022005Z",
|
||||
User: funkwhale.User{
|
||||
UserName: "outsidecontext",
|
||||
},
|
||||
Track: funkwhale.Track{
|
||||
Title: "Oweynagat",
|
||||
RecordingMbid: "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12",
|
||||
Position: 5,
|
||||
DiscNumber: 1,
|
||||
Tags: []string{"foo", "bar"},
|
||||
Artist: funkwhale.Artist{
|
||||
Name: "Dool",
|
||||
ArtistMbid: "24412926-c7bd-48e8-afad-8a285b42e131",
|
||||
},
|
||||
Album: funkwhale.Album{
|
||||
Title: "Here Now, There Then",
|
||||
ReleaseMbid: "d7f22677-9803-4d21-ba42-081b633a6f68",
|
||||
},
|
||||
Uploads: []funkwhale.Upload{
|
||||
{
|
||||
Duration: 414,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
listen := funkwhale.ListenFromFunkwhale(fwListen)
|
||||
assert.Equal(t, time.Unix(1699574369, 0).Unix(), listen.ListenedAt.Unix())
|
||||
assert.Equal(t, fwListen.User.UserName, listen.UserName)
|
||||
assert.Equal(t, time.Duration(414*time.Second), listen.Duration)
|
||||
assert.Equal(t, fwListen.Track.Title, listen.TrackName)
|
||||
assert.Equal(t, fwListen.Track.Album.Title, listen.ReleaseName)
|
||||
assert.Equal(t, []string{fwListen.Track.Artist.Name}, listen.ArtistNames)
|
||||
assert.Equal(t, fwListen.Track.Position, listen.Track.TrackNumber)
|
||||
assert.Equal(t, fwListen.Track.Tags, listen.Track.Tags)
|
||||
// assert.Equal(t, backends.FunkwhaleClientName, listen.AdditionalInfo["disc_number"])
|
||||
assert.Equal(t, models.MBID(fwListen.Track.RecordingMbid), listen.RecordingMbid)
|
||||
assert.Equal(t, models.MBID(fwListen.Track.Album.ReleaseMbid), listen.ReleaseMbid)
|
||||
assert.Equal(t, models.MBID(fwListen.Track.Artist.ArtistMbid), listen.ArtistMbids[0])
|
||||
assert.Equal(t, funkwhale.FunkwhaleClientName, listen.AdditionalInfo["media_player"])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue