scotty/backends/funkwhale/funkwhale.go
Philipp Wolfer 117014a977
Change project license to GPLv3
Individual files, mainly the models and the HTTP clients stay under MIT
2023-11-22 08:05:23 +01:00

206 lines
5 KiB
Go

/*
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 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) Name() string { return "funkwhale" }
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
}