mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-28 06:57:56 +02:00
Restructured code, moved all modules into internal
For now all modules are considered internal. This might change later
This commit is contained in:
parent
f94e0f1e85
commit
857661ebf9
76 changed files with 121 additions and 68 deletions
164
internal/backends/jspf/jspf.go
Normal file
164
internal/backends/jspf/jspf.go
Normal file
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
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 jspf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/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) ImportListens(export models.ListensResult, importResult models.ImportResult, progress chan models.Progress) (models.ImportResult, error) {
|
||||
for _, listen := range export.Listens {
|
||||
track := listenAsTrack(listen)
|
||||
b.tracks = append(b.tracks, track)
|
||||
importResult.ImportCount += 1
|
||||
importResult.UpdateTimestamp(listen.ListenedAt)
|
||||
}
|
||||
|
||||
progress <- models.Progress{}.FromImportResult(importResult)
|
||||
return importResult, nil
|
||||
}
|
||||
|
||||
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 listenAsTrack(l models.Listen) Track {
|
||||
l.FillAdditionalInfo()
|
||||
track := trackAsTrack(l.Track)
|
||||
extension := makeMusicBrainzExtension(l.Track)
|
||||
extension.AddedAt = l.ListenedAt
|
||||
extension.AddedBy = l.UserName
|
||||
track.Extension[MusicBrainzTrackExtensionId] = extension
|
||||
|
||||
if l.RecordingMbid != "" {
|
||||
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(l.RecordingMbid))
|
||||
}
|
||||
|
||||
return track
|
||||
}
|
||||
|
||||
func loveAsTrack(l models.Love) Track {
|
||||
l.FillAdditionalInfo()
|
||||
track := trackAsTrack(l.Track)
|
||||
extension := makeMusicBrainzExtension(l.Track)
|
||||
extension.AddedAt = l.Created
|
||||
extension.AddedBy = l.UserName
|
||||
track.Extension[MusicBrainzTrackExtensionId] = extension
|
||||
|
||||
if l.RecordingMbid != "" {
|
||||
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(l.RecordingMbid))
|
||||
}
|
||||
|
||||
return track
|
||||
}
|
||||
|
||||
func trackAsTrack(t models.Track) Track {
|
||||
track := Track{
|
||||
Title: t.TrackName,
|
||||
Album: t.ReleaseName,
|
||||
Creator: t.ArtistName(),
|
||||
TrackNum: t.TrackNumber,
|
||||
Extension: map[string]any{},
|
||||
}
|
||||
|
||||
return track
|
||||
}
|
||||
|
||||
func makeMusicBrainzExtension(t models.Track) MusicBrainzTrackExtension {
|
||||
extension := MusicBrainzTrackExtension{
|
||||
AdditionalMetadata: t.AdditionalInfo,
|
||||
ArtistIdentifiers: make([]string, len(t.ArtistMbids)),
|
||||
}
|
||||
|
||||
for i, mbid := range t.ArtistMbids {
|
||||
extension.ArtistIdentifiers[i] = "https://musicbrainz.org/artist/" + string(mbid)
|
||||
}
|
||||
|
||||
if t.ReleaseMbid != "" {
|
||||
extension.ReleaseIdentifier = "https://musicbrainz.org/release/" + string(t.ReleaseMbid)
|
||||
}
|
||||
|
||||
// The tracknumber tag would be redundant
|
||||
delete(extension.AdditionalMetadata, "tracknumber")
|
||||
|
||||
return extension
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue