mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19:28 +02:00
Implemented subsonic loves import
This commit is contained in:
parent
c4ae2eda48
commit
baed52dacc
5 changed files with 159 additions and 0 deletions
|
@ -33,6 +33,7 @@ import (
|
||||||
"go.uploadedlobster.com/scotty/backends/listenbrainz"
|
"go.uploadedlobster.com/scotty/backends/listenbrainz"
|
||||||
"go.uploadedlobster.com/scotty/backends/maloja"
|
"go.uploadedlobster.com/scotty/backends/maloja"
|
||||||
"go.uploadedlobster.com/scotty/backends/scrobblerlog"
|
"go.uploadedlobster.com/scotty/backends/scrobblerlog"
|
||||||
|
"go.uploadedlobster.com/scotty/backends/subsonic"
|
||||||
"go.uploadedlobster.com/scotty/models"
|
"go.uploadedlobster.com/scotty/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -81,6 +82,7 @@ var knownBackends = map[string]func() models.Backend{
|
||||||
"listenbrainz-api": func() models.Backend { return &listenbrainz.ListenBrainzApiBackend{} },
|
"listenbrainz-api": func() models.Backend { return &listenbrainz.ListenBrainzApiBackend{} },
|
||||||
"maloja-api": func() models.Backend { return &maloja.MalojaApiBackend{} },
|
"maloja-api": func() models.Backend { return &maloja.MalojaApiBackend{} },
|
||||||
"scrobbler-log": func() models.Backend { return &scrobblerlog.ScrobblerLogBackend{} },
|
"scrobbler-log": func() models.Backend { return &scrobblerlog.ScrobblerLogBackend{} },
|
||||||
|
"subsonic-api": func() models.Backend { return &subsonic.SubsonicApiBackend{} },
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveBackend(config *viper.Viper) (string, models.Backend, error) {
|
func resolveBackend(config *viper.Viper) (string, models.Backend, error) {
|
||||||
|
|
91
backends/subsonic/subsonic.go
Normal file
91
backends/subsonic/subsonic.go
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
/*
|
||||||
|
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 subsonic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/delucks/go-subsonic"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"go.uploadedlobster.com/scotty/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SubsonicApiBackend struct {
|
||||||
|
client subsonic.Client
|
||||||
|
password string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b SubsonicApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||||
|
b.client = subsonic.Client{
|
||||||
|
Client: &http.Client{},
|
||||||
|
BaseUrl: config.GetString("server-url"),
|
||||||
|
User: config.GetString("username"),
|
||||||
|
ClientName: "Scotty",
|
||||||
|
}
|
||||||
|
b.password = config.GetString("token")
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b SubsonicApiBackend) ExportLoves(oldestTimestamp time.Time) ([]models.Love, error) {
|
||||||
|
err := b.client.Authenticate(b.password)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := b.client.GetStarred2(map[string]string{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
loves := make([]models.Love, 0)
|
||||||
|
out:
|
||||||
|
for _, song := range result.Song {
|
||||||
|
love := SongToLove(*song, b.client.User)
|
||||||
|
if love.Created.Unix() > oldestTimestamp.Unix() {
|
||||||
|
loves = append(loves, love)
|
||||||
|
} else {
|
||||||
|
break out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Sort by creation date ascending
|
||||||
|
return loves, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SongToLove(song subsonic.Child, username string) models.Love {
|
||||||
|
love := models.Love{
|
||||||
|
UserName: username,
|
||||||
|
Created: song.Starred,
|
||||||
|
Track: models.Track{
|
||||||
|
TrackName: song.Title,
|
||||||
|
ReleaseName: song.Album,
|
||||||
|
ArtistNames: []string{song.Artist},
|
||||||
|
TrackNumber: song.Track,
|
||||||
|
Tags: []string{song.Genre},
|
||||||
|
AdditionalInfo: map[string]any{},
|
||||||
|
Duration: time.Duration(song.Duration * int(time.Second)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return love
|
||||||
|
}
|
63
backends/subsonic/subsonic_test.go
Normal file
63
backends/subsonic/subsonic_test.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
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 subsonic_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
go_subsonic "github.com/delucks/go-subsonic"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"go.uploadedlobster.com/scotty/backends/subsonic"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFromConfig(t *testing.T) {
|
||||||
|
config := viper.New()
|
||||||
|
config.Set("server-url", "https://subsonic.example.com")
|
||||||
|
config.Set("token", "thetoken")
|
||||||
|
backend := subsonic.SubsonicApiBackend{}.FromConfig(config)
|
||||||
|
assert.IsType(t, subsonic.SubsonicApiBackend{}, backend)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSongToLove(t *testing.T) {
|
||||||
|
user := "outsidecontext"
|
||||||
|
song := go_subsonic.Child{
|
||||||
|
Starred: time.Unix(1699574369, 0),
|
||||||
|
Title: "Oweynagat",
|
||||||
|
Album: "Here Now, There Then",
|
||||||
|
Artist: "Dool",
|
||||||
|
Duration: 414,
|
||||||
|
Genre: "psychedelic rock",
|
||||||
|
DiscNumber: 1,
|
||||||
|
Track: 5,
|
||||||
|
}
|
||||||
|
love := subsonic.SongToLove(song, user)
|
||||||
|
assert.Equal(t, time.Unix(1699574369, 0).Unix(), love.Created.Unix())
|
||||||
|
assert.Equal(t, user, love.UserName)
|
||||||
|
assert.Equal(t, time.Duration(414*time.Second), love.Duration)
|
||||||
|
assert.Equal(t, song.Title, love.TrackName)
|
||||||
|
assert.Equal(t, song.Album, love.ReleaseName)
|
||||||
|
assert.Equal(t, []string{song.Artist}, love.ArtistNames)
|
||||||
|
assert.Equal(t, song.Track, love.Track.TrackNumber)
|
||||||
|
assert.Equal(t, []string{song.Genre}, love.Track.Tags)
|
||||||
|
}
|
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.21.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||||
|
github.com/delucks/go-subsonic v0.0.0-20220915164742-2744002c4be5 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||||
github.com/go-resty/resty/v2 v2.10.0 // indirect
|
github.com/go-resty/resty/v2 v2.10.0 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -51,6 +51,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/delucks/go-subsonic v0.0.0-20220915164742-2744002c4be5 h1:RuuxidatioSKGOiBzL1mTY4X22DQD8weEbS3iRLHnAg=
|
||||||
|
github.com/delucks/go-subsonic v0.0.0-20220915164742-2744002c4be5/go.mod h1:vnbEuj6Z20PLcHB4rrLQAOXGMjtULfMGhRVSFPcSdUo=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||||
|
|
Loading…
Add table
Reference in a new issue