mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19:28 +02:00
124 lines
3.4 KiB
Go
124 lines
3.4 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 jspf
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
)
|
|
|
|
type JspfBackend struct {
|
|
filePath string
|
|
title string
|
|
creator string
|
|
identifier string
|
|
}
|
|
|
|
func (b JspfBackend) FromConfig(config *viper.Viper) models.Backend {
|
|
b.filePath = config.GetString("file-path")
|
|
b.title = config.GetString("title")
|
|
b.creator = config.GetString("username")
|
|
b.identifier = config.GetString("identifier")
|
|
return b
|
|
}
|
|
|
|
func (b JspfBackend) ImportLoves(loves []models.Love, oldestTimestamp time.Time) (models.ImportResult, error) {
|
|
result := models.ImportResult{
|
|
TotalCount: len(loves),
|
|
ImportCount: 0,
|
|
LastTimestamp: oldestTimestamp,
|
|
}
|
|
|
|
tracks := make([]Track, 0, result.TotalCount)
|
|
for _, love := range loves {
|
|
if love.Created.Unix() > result.LastTimestamp.Unix() {
|
|
result.LastTimestamp = love.Created
|
|
}
|
|
|
|
extension := MusicBrainzTrackExtension{
|
|
AddedAt: love.Created,
|
|
AddedBy: love.UserName,
|
|
AdditionalMetadata: love.AdditionalInfo,
|
|
ArtistIdentifiers: make([]string, len(love.ArtistMbids)),
|
|
}
|
|
|
|
for i, mbid := range love.ArtistMbids {
|
|
extension.ArtistIdentifiers[i] = "https://musicbrainz.org/artist/" + string(mbid)
|
|
}
|
|
|
|
if love.ReleaseMbid != "" {
|
|
extension.ReleaseIdentifier = "https://musicbrainz.org/release/" + string(love.ReleaseMbid)
|
|
}
|
|
|
|
track := Track{
|
|
Title: love.TrackName,
|
|
Album: love.ReleaseName,
|
|
Creator: love.ArtistName(),
|
|
TrackNum: love.TrackNumber,
|
|
Extension: map[string]any{
|
|
"https://musicbrainz.org/doc/jspf#track": extension,
|
|
},
|
|
}
|
|
|
|
if love.RecordingMbid != "" {
|
|
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(love.RecordingMbid))
|
|
}
|
|
|
|
tracks = append(tracks, track)
|
|
|
|
result.ImportCount += 1
|
|
}
|
|
|
|
err := b.writeJspf(tracks)
|
|
return result, err
|
|
}
|
|
|
|
func (b JspfBackend) writeJspf(tracks []Track) error {
|
|
playlist := Jspf{
|
|
Playlist: Playlist{
|
|
Title: b.title,
|
|
Creator: b.creator,
|
|
Identifier: b.identifier,
|
|
Date: time.Now(),
|
|
Tracks: tracks,
|
|
},
|
|
}
|
|
|
|
file, err := os.Create(b.filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer file.Close()
|
|
jspfJson, err := json.MarshalIndent(playlist, "", "\t")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = file.Write(jspfJson)
|
|
return err
|
|
}
|