scotty/backends/funkwhale/funkwhale.go
2023-11-19 13:56:06 +01:00

209 lines
5.3 KiB
Go

/*
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 (
"sort"
"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, results chan models.ListensResult, progress chan models.Progress) {
page := 1
perPage := MaxItemsPerGet
defer close(results)
defer close(progress)
// 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.GetHistoryListenings(b.username, page, perPage)
if err != nil {
results <- models.ListensResult{Error: err}
}
count := len(result.Results)
if count == 0 {
break out
}
for _, fwListen := range result.Results {
listen := fwListen.ToListen()
if listen.ListenedAt.Unix() > oldestTimestamp.Unix() {
p.Elapsed += 1
listens = append(listens, listen)
} else {
break out
}
}
if result.Next == "" {
// No further results
p.Total = p.Elapsed
p.Total -= int64(perPage - count)
break out
}
p.Total += int64(perPage)
progress <- p
page += 1
}
sort.Sort(listens)
progress <- p.Complete()
results <- models.ListensResult{Listens: listens}
}
func (b *FunkwhaleApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.Progress) {
page := 1
perPage := MaxItemsPerGet
defer close(results)
defer close(progress)
// We need to gather the full list of listens in order to sort them
loves := make(models.LovesList, 0, 2*perPage)
p := models.Progress{Total: int64(perPage)}
out:
for {
result, err := b.client.GetFavoriteTracks(page, perPage)
if err != nil {
results <- models.LovesResult{Error: err}
return
}
count := len(result.Results)
if count == 0 {
break out
}
for _, favorite := range result.Results {
love := favorite.ToLove()
if love.Created.Unix() > oldestTimestamp.Unix() {
p.Elapsed += 1
loves = append(loves, love)
} else {
break out
}
}
if result.Next == "" {
// No further results
break out
}
p.Total += int64(perPage)
progress <- p
page += 1
}
sort.Sort(loves)
progress <- p.Complete()
results <- models.LovesResult{Loves: loves}
}
func (l Listening) ToListen() models.Listen {
track := l.Track
listen := models.Listen{
UserName: l.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, l.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
}
func (f FavoriteTrack) ToLove() models.Love {
track := f.Track
recordingMbid := models.MBID(track.RecordingMbid)
love := models.Love{
UserName: f.User.UserName,
RecordingMbid: recordingMbid,
Track: models.Track{
TrackName: track.Title,
ReleaseName: track.Album.Title,
ArtistNames: []string{track.Artist.Name},
TrackNumber: track.Position,
RecordingMbid: 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,
},
},
}
created, err := time.Parse(time.RFC3339, f.CreationDate)
if err == nil {
love.Created = created
}
if len(track.Uploads) > 0 {
love.Track.Duration = time.Duration(track.Uploads[0].Duration * int(time.Second))
}
return love
}