mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-28 06:57:56 +02:00
Implemented Funkwhale API listens export
This commit is contained in:
parent
3ac3fac317
commit
cae7d22a36
7 changed files with 668 additions and 0 deletions
117
backends/funkwhale.go
Normal file
117
backends/funkwhale.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
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 backends
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/backends/funkwhale"
|
||||
"go.uploadedlobster.com/scotty/models"
|
||||
)
|
||||
|
||||
const FunkwhaleClientName = "Funkwhale"
|
||||
|
||||
type FunkwhaleApiBackend struct {
|
||||
client funkwhale.Client
|
||||
username string
|
||||
}
|
||||
|
||||
func (b FunkwhaleApiBackend) FromConfig(config *viper.Viper) Backend {
|
||||
b.client = funkwhale.New(
|
||||
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 := funkwhale.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 funkwhale.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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue