/* Copyright © 2023 Philipp Wolfer 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 . */ 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 tracks []Track } func (b *JspfBackend) Name() string { return "jspf" } 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") b.tracks = make([]Track, 0) return b } func (b *JspfBackend) StartImport() error { return nil } func (b *JspfBackend) FinishImport() error { err := b.writeJspf(b.tracks) return err } func (b *JspfBackend) ImportLoves(export models.LovesResult, importResult models.ImportResult, progress chan models.Progress) (models.ImportResult, error) { for _, love := range export.Loves { track := loveAsTrack(love) b.tracks = append(b.tracks, track) importResult.ImportCount += 1 importResult.UpdateTimestamp(love.Created) } progress <- models.Progress{}.FromImportResult(importResult) return importResult, nil } func loveAsTrack(love models.Love) Track { 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)) } return track } 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 }