Restructured code, moved all modules into internal

For now all modules are considered internal. This might change later
This commit is contained in:
Philipp Wolfer 2023-11-24 00:07:47 +01:00
parent f94e0f1e85
commit 857661ebf9
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
76 changed files with 121 additions and 68 deletions

View file

@ -0,0 +1,79 @@
/*
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 maloja
import (
"errors"
"strconv"
"github.com/go-resty/resty/v2"
)
const MaxItemsPerGet = 1000
type Client struct {
HttpClient *resty.Client
token string
}
func NewClient(serverUrl string, token string) Client {
client := resty.New()
client.SetBaseURL(serverUrl)
client.SetHeader("Accept", "application/json")
client.SetRetryCount(5)
return Client{
HttpClient: client,
token: token,
}
}
func (c Client) GetScrobbles(page int, perPage int) (result GetScrobblesResult, err error) {
const path = "/apis/mlj_1/scrobbles"
response, err := c.HttpClient.R().
SetQueryParams(map[string]string{
"page": strconv.Itoa(page),
"perpage": strconv.Itoa(perPage),
}).
SetResult(&result).
Get(path)
if response.StatusCode() != 200 {
err = errors.New(response.String())
return
}
return
}
func (c Client) NewScrobble(scrobble NewScrobble) (result NewScrobbleResult, err error) {
const path = "/apis/mlj_1/newscrobble"
scrobble.Key = c.token
response, err := c.HttpClient.R().
SetBody(scrobble).
SetResult(&result).
Post(path)
if response.StatusCode() != 200 {
err = errors.New(response.String())
return
}
return
}

View 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 maloja_test
import (
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/scotty/internal/backends/maloja"
)
func TestNewClient(t *testing.T) {
serverUrl := "https://maloja.example.com"
token := "foobar123"
client := maloja.NewClient(serverUrl, token)
assert.Equal(t, serverUrl, client.HttpClient.BaseURL)
}
func TestGetScrobbles(t *testing.T) {
defer httpmock.DeactivateAndReset()
serverUrl := "https://maloja.example.com"
token := "thetoken"
client := maloja.NewClient(serverUrl, token)
setupHttpMock(t, client.HttpClient.GetClient(),
"https://maloja.example.com/apis/mlj_1/scrobbles",
"testdata/scrobbles.json")
result, err := client.GetScrobbles(0, 2)
require.NoError(t, err)
assert := assert.New(t)
require.Len(t, result.List, 2)
assert.Equal("Way to Eden", result.List[0].Track.Title)
assert.Equal(int64(558), result.List[0].Duration)
}
func TestNewScrobble(t *testing.T) {
server := "https://maloja.example.com"
client := maloja.NewClient(server, "thetoken")
httpmock.ActivateNonDefault(client.HttpClient.GetClient())
responder, err := httpmock.NewJsonResponder(200, httpmock.File("testdata/newscrobble-result.json"))
if err != nil {
t.Fatal(err)
}
url := server + "/apis/mlj_1/newscrobble"
httpmock.RegisterResponder("POST", url, responder)
scrobble := maloja.NewScrobble{
Title: "Oweynagat",
Artist: "Dool",
Time: 1699574369,
}
result, err := client.NewScrobble(scrobble)
require.NoError(t, err)
assert.Equal(t, "success", result.Status)
}
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {
httpmock.ActivateNonDefault(client)
responder, err := httpmock.NewJsonResponder(200, httpmock.File(testDataPath))
if err != nil {
t.Fatal(err)
}
httpmock.RegisterResponder("GET", url, responder)
}

View file

@ -0,0 +1,138 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
This file is part of Scotty.
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package maloja
import (
"errors"
"sort"
"strings"
"time"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/models"
)
type MalojaApiBackend struct {
client Client
nofix bool
}
func (b *MalojaApiBackend) Name() string { return "maloja" }
func (b *MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
b.client = NewClient(
config.GetString("server-url"),
config.GetString("token"),
)
b.nofix = config.GetBool("nofix")
return b
}
func (b *MalojaApiBackend) StartImport() error { return nil }
func (b *MalojaApiBackend) FinishImport() error { return nil }
func (b *MalojaApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.Progress) {
page := 0
perPage := MaxItemsPerGet
defer close(results)
// We need to gather the full list of listens in order to sort them
listens := make(models.ListensList, 0, 2*perPage)
p := models.Progress{Total: int64(perPage)}
out:
for {
result, err := b.client.GetScrobbles(page, perPage)
if err != nil {
progress <- p.Complete()
results <- models.ListensResult{Error: err}
return
}
count := len(result.List)
if count == 0 {
break
}
for _, scrobble := range result.List {
if scrobble.ListenedAt > oldestTimestamp.Unix() {
p.Elapsed += 1
listens = append(listens, scrobble.AsListen())
} else {
break out
}
}
p.Total += int64(perPage)
progress <- p
page += 1
}
sort.Sort(listens)
progress <- p.Complete()
results <- models.ListensResult{Listens: listens}
}
func (b *MalojaApiBackend) ImportListens(export models.ListensResult, importResult models.ImportResult, progress chan models.Progress) (models.ImportResult, error) {
for _, listen := range export.Listens {
scrobble := NewScrobble{
Title: listen.TrackName,
Artists: listen.ArtistNames,
Album: listen.ReleaseName,
Duration: int64(listen.PlaybackDuration.Seconds()),
Length: int64(listen.Duration.Seconds()),
Time: listen.ListenedAt.Unix(),
Nofix: b.nofix,
}
resp, err := b.client.NewScrobble(scrobble)
if err != nil {
return importResult, err
} else if resp.Status != "success" {
return importResult, errors.New(resp.Error.Description)
}
importResult.UpdateTimestamp(listen.ListenedAt)
importResult.ImportCount += 1
progress <- models.Progress{}.FromImportResult(importResult)
}
return importResult, nil
}
func (s Scrobble) AsListen() models.Listen {
track := s.Track
listen := models.Listen{
ListenedAt: time.Unix(s.ListenedAt, 0),
PlaybackDuration: time.Duration(s.Duration * int64(time.Second)),
Track: models.Track{
TrackName: track.Title,
ReleaseName: track.Album.Title,
ArtistNames: track.Artists,
Duration: time.Duration(track.Length * int64(time.Second)),
AdditionalInfo: map[string]any{},
},
}
client, found := strings.CutPrefix(s.Origin, "client:")
if found {
listen.AdditionalInfo["media_player"] = client
}
return listen
}

View file

@ -0,0 +1,56 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
This file is part of Scotty.
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package maloja_test
import (
"testing"
"time"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uploadedlobster.com/scotty/internal/backends/maloja"
)
func TestFromConfig(t *testing.T) {
config := viper.New()
config.Set("token", "thetoken")
backend := (&maloja.MalojaApiBackend{}).FromConfig(config)
assert.IsType(t, &maloja.MalojaApiBackend{}, backend)
}
func TestScrobbleAsListen(t *testing.T) {
scrobble := maloja.Scrobble{
ListenedAt: 1699289873,
Track: maloja.Track{
Title: "Oweynagat",
Album: maloja.Album{
Title: "Here Now, There Then",
},
Artists: []string{"Dool"},
Length: 414,
},
Origin: "client:Funkwhale",
}
listen := scrobble.AsListen()
assert := assert.New(t)
assert.Equal(time.Unix(1699289873, 0), listen.ListenedAt)
assert.Equal(time.Duration(414*time.Second), listen.Duration)
assert.Equal(scrobble.Track.Title, listen.TrackName)
assert.Equal(scrobble.Track.Album.Title, listen.ReleaseName)
assert.Equal(scrobble.Track.Artists, listen.ArtistNames)
assert.Equal("Funkwhale", listen.AdditionalInfo["media_player"])
}

View file

@ -0,0 +1,86 @@
/*
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 maloja
type GenericResult struct {
Status string `json:"status"`
}
type GetScrobblesResult struct {
GenericResult
List []Scrobble `json:"list"`
Pagination Pagination `json:"pagination"`
}
type NewScrobbleResult struct {
GenericResult
Track Track `json:"track"`
Description string `json:"desc"`
Error Error `json:"error"`
}
type Scrobble struct {
ListenedAt int64 `json:"time"`
Duration int64 `json:"duration"`
// Maloja sets Origin to the name of the API key
// prefixed with "client:"
Origin string `json:"origin"`
Track Track `json:"track"`
}
type NewScrobble struct {
Key string `json:"key"`
Artist string `json:"artist,omitempty"`
Artists []string `json:"artists,omitempty"`
Title string `json:"title"`
Album string `json:"album,omitempty"`
AlbumArtists []string `json:"albumartists,omitempty"`
Duration int64 `json:"duration,omitempty"`
Length int64 `json:"length,omitempty"`
Time int64 `json:"time,omitempty"`
Nofix bool `json:"nofix,omitempty"`
}
type Track struct {
Title string `json:"title"`
Artists []string `json:"artists"`
Album Album `json:"album"`
Length int64 `json:"length"`
}
type Album struct {
Title string `json:"albumtitle"`
Artists []string `json:"artists"`
}
type Pagination struct {
Page int `json:"page"`
PerPage int `json:"perpage"`
NextPage string `json:"next_page"`
PrevPage string `json:"prev_page"`
}
type Error struct {
Type string `json:"type"`
Value string `json:"value"`
Description string `json:"desc"`
}

View file

@ -0,0 +1,10 @@
{
"status": "success",
"track": {
"artists": [
"Dool"
],
"title": "Oweynagat"
},
"desc": "Scrobbled Oweynagat by Dool"
}

View file

@ -0,0 +1,47 @@
{
"status": "ok",
"list": [
{
"time": 1699574369,
"track": {
"artists": [
"Hazeshuttle"
],
"title": "Way to Eden",
"album": {
"artists": [
"Hazeshuttle"
],
"albumtitle": "Hazeshuttle"
},
"length": 567
},
"duration": 558,
"origin": "client:Funkwhale"
},
{
"time": 1699573362,
"track": {
"artists": [
"Hazeshuttle"
],
"title": "Homosativa",
"album": {
"artists": [
"Hazeshuttle"
],
"albumtitle": "Hazeshuttle"
},
"length": 1007
},
"duration": null,
"origin": "client:Funkwhale"
}
],
"pagination": {
"page": 0,
"perpage": 2,
"next_page": "/apis/mlj_1/scrobbles?page=1&perpage=2",
"prev_page": null
}
}