diff --git a/backends/backends.go b/backends/backends.go index 3cb3444..1e058de 100644 --- a/backends/backends.go +++ b/backends/backends.go @@ -33,6 +33,7 @@ import ( "go.uploadedlobster.com/scotty/backends/listenbrainz" "go.uploadedlobster.com/scotty/backends/maloja" "go.uploadedlobster.com/scotty/backends/scrobblerlog" + "go.uploadedlobster.com/scotty/backends/subsonic" "go.uploadedlobster.com/scotty/models" ) @@ -81,6 +82,7 @@ var knownBackends = map[string]func() models.Backend{ "listenbrainz-api": func() models.Backend { return &listenbrainz.ListenBrainzApiBackend{} }, "maloja-api": func() models.Backend { return &maloja.MalojaApiBackend{} }, "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) { diff --git a/backends/subsonic/subsonic.go b/backends/subsonic/subsonic.go new file mode 100644 index 0000000..6555bb8 --- /dev/null +++ b/backends/subsonic/subsonic.go @@ -0,0 +1,91 @@ +/* +Copyright © 2023 Philipp Wolfer + +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 +} diff --git a/backends/subsonic/subsonic_test.go b/backends/subsonic/subsonic_test.go new file mode 100644 index 0000000..b7b8d76 --- /dev/null +++ b/backends/subsonic/subsonic_test.go @@ -0,0 +1,63 @@ +/* +Copyright © 2023 Philipp Wolfer + +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) +} diff --git a/go.mod b/go.mod index 7192c42..b1972be 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.1 require ( 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/go-resty/resty/v2 v2.10.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect diff --git a/go.sum b/go.sum index c1d9ba9..912ec55 100644 --- a/go.sum +++ b/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.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/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.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=