mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-24 21:47:55 +02:00
Move the JSPF playlist code into public module
This commit is contained in:
parent
0f5cb49b4c
commit
e1c9fb6076
11 changed files with 367 additions and 56 deletions
|
@ -18,12 +18,12 @@ 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"
|
||||
"go.uploadedlobster.com/scotty/pkg/jspf"
|
||||
)
|
||||
|
||||
type JSPFBackend struct {
|
||||
|
@ -31,7 +31,7 @@ type JSPFBackend struct {
|
|||
title string
|
||||
creator string
|
||||
identifier string
|
||||
tracks []Track
|
||||
tracks []jspf.Track
|
||||
}
|
||||
|
||||
func (b *JSPFBackend) Name() string { return "jspf" }
|
||||
|
@ -41,7 +41,7 @@ func (b *JSPFBackend) FromConfig(config *viper.Viper) models.Backend {
|
|||
b.title = config.GetString("title")
|
||||
b.creator = config.GetString("username")
|
||||
b.identifier = config.GetString("identifier")
|
||||
b.tracks = make([]Track, 0)
|
||||
b.tracks = make([]jspf.Track, 0)
|
||||
return b
|
||||
}
|
||||
|
||||
|
@ -75,13 +75,13 @@ func (b *JSPFBackend) ImportLoves(export models.LovesResult, importResult models
|
|||
return importResult, nil
|
||||
}
|
||||
|
||||
func listenAsTrack(l models.Listen) Track {
|
||||
func listenAsTrack(l models.Listen) jspf.Track {
|
||||
l.FillAdditionalInfo()
|
||||
track := trackAsTrack(l.Track)
|
||||
extension := makeMusicBrainzExtension(l.Track)
|
||||
extension.AddedAt = l.ListenedAt
|
||||
extension.AddedBy = l.UserName
|
||||
track.Extension[MusicBrainzTrackExtensionId] = extension
|
||||
track.Extension[jspf.MusicBrainzTrackExtensionId] = extension
|
||||
|
||||
if l.RecordingMbid != "" {
|
||||
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(l.RecordingMbid))
|
||||
|
@ -90,13 +90,13 @@ func listenAsTrack(l models.Listen) Track {
|
|||
return track
|
||||
}
|
||||
|
||||
func loveAsTrack(l models.Love) Track {
|
||||
func loveAsTrack(l models.Love) jspf.Track {
|
||||
l.FillAdditionalInfo()
|
||||
track := trackAsTrack(l.Track)
|
||||
extension := makeMusicBrainzExtension(l.Track)
|
||||
extension.AddedAt = l.Created
|
||||
extension.AddedBy = l.UserName
|
||||
track.Extension[MusicBrainzTrackExtensionId] = extension
|
||||
track.Extension[jspf.MusicBrainzTrackExtensionId] = extension
|
||||
|
||||
if l.RecordingMbid != "" {
|
||||
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(l.RecordingMbid))
|
||||
|
@ -105,8 +105,8 @@ func loveAsTrack(l models.Love) Track {
|
|||
return track
|
||||
}
|
||||
|
||||
func trackAsTrack(t models.Track) Track {
|
||||
track := Track{
|
||||
func trackAsTrack(t models.Track) jspf.Track {
|
||||
track := jspf.Track{
|
||||
Title: t.TrackName,
|
||||
Album: t.ReleaseName,
|
||||
Creator: t.ArtistName(),
|
||||
|
@ -117,8 +117,8 @@ func trackAsTrack(t models.Track) Track {
|
|||
return track
|
||||
}
|
||||
|
||||
func makeMusicBrainzExtension(t models.Track) MusicBrainzTrackExtension {
|
||||
extension := MusicBrainzTrackExtension{
|
||||
func makeMusicBrainzExtension(t models.Track) jspf.MusicBrainzTrackExtension {
|
||||
extension := jspf.MusicBrainzTrackExtension{
|
||||
AdditionalMetadata: t.AdditionalInfo,
|
||||
ArtistIdentifiers: make([]string, len(t.ArtistMbids)),
|
||||
}
|
||||
|
@ -137,9 +137,9 @@ func makeMusicBrainzExtension(t models.Track) MusicBrainzTrackExtension {
|
|||
return extension
|
||||
}
|
||||
|
||||
func (b JSPFBackend) writeJSPF(tracks []Track) error {
|
||||
playlist := JSPF{
|
||||
Playlist: Playlist{
|
||||
func (b JSPFBackend) writeJSPF(tracks []jspf.Track) error {
|
||||
playlist := jspf.JSPF{
|
||||
Playlist: jspf.Playlist{
|
||||
Title: b.title,
|
||||
Creator: b.creator,
|
||||
Identifier: b.identifier,
|
||||
|
@ -154,11 +154,5 @@ func (b JSPFBackend) writeJSPF(tracks []Track) error {
|
|||
}
|
||||
|
||||
defer file.Close()
|
||||
jspfJson, err := json.MarshalIndent(playlist, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = file.Write(jspfJson)
|
||||
return err
|
||||
return playlist.Write(file)
|
||||
}
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
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 "time"
|
||||
|
||||
// See https://xspf.org/jspf
|
||||
type JSPF struct {
|
||||
Playlist Playlist `json:"playlist"`
|
||||
}
|
||||
|
||||
type Playlist struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Creator string `json:"creator,omitempty"`
|
||||
Annotation string `json:"annotation,omitempty"`
|
||||
Info string `json:"info,omitempty"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Identifier string `json:"identifier,omitempty"`
|
||||
Image string `json:"image,omitempty"`
|
||||
Date time.Time `json:"date,omitempty"`
|
||||
License string `json:"license,omitempty"`
|
||||
Attribution []Attribution `json:"attribution,omitempty"`
|
||||
Links []Link `json:"link,omitempty"`
|
||||
Meta []Meta `json:"meta,omitempty"`
|
||||
Extension map[string]any `json:"extension,omitempty"`
|
||||
Tracks []Track `json:"track"`
|
||||
}
|
||||
|
||||
type Track struct {
|
||||
Location []string `json:"location,omitempty"`
|
||||
Identifier []string `json:"identifier,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Creator string `json:"creator,omitempty"`
|
||||
Annotation string `json:"annotation,omitempty"`
|
||||
Info string `json:"info,omitempty"`
|
||||
Album string `json:"album,omitempty"`
|
||||
TrackNum int `json:"trackNum,omitempty"`
|
||||
Duration int `json:"duration,omitempty"`
|
||||
Links []Link `json:"link,omitempty"`
|
||||
Meta []Meta `json:"meta,omitempty"`
|
||||
Extension map[string]any `json:"extension,omitempty"`
|
||||
}
|
||||
|
||||
type Attribution map[string]string
|
||||
|
||||
type Link map[string]string
|
||||
|
||||
type Meta map[string]string
|
||||
|
||||
// Extension for "https://musicbrainz.org/doc/jspf#track"
|
||||
// as used by ListenBrainz.
|
||||
const MusicBrainzTrackExtensionId = "https://musicbrainz.org/doc/jspf#track"
|
||||
|
||||
type MusicBrainzTrackExtension struct {
|
||||
AddedAt time.Time `json:"added_at,omitempty"`
|
||||
AddedBy string `json:"added_by,omitempty"`
|
||||
ReleaseIdentifier string `json:"release_identifier,omitempty"`
|
||||
ArtistIdentifiers []string `json:"artist_identifiers,omitempty"`
|
||||
AdditionalMetadata map[string]any `json:"additional_metadata,omitempty"`
|
||||
}
|
|
@ -1,106 +0,0 @@
|
|||
/*
|
||||
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_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/jspf"
|
||||
)
|
||||
|
||||
func TestUnmarshalSimple(t *testing.T) {
|
||||
data, err := readSampleJson("testdata/simple.jspf")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert := assert.New(t)
|
||||
playlist := data.Playlist
|
||||
assert.Equal("Two Songs From Thriller", playlist.Title)
|
||||
assert.Equal("MJ Fan", playlist.Creator)
|
||||
require.Len(t, playlist.Tracks, 2)
|
||||
track1 := playlist.Tracks[0]
|
||||
require.Len(t, track1.Location, 1)
|
||||
assert.Equal("http://example.com/billiejean.mp3", track1.Location[0])
|
||||
assert.Equal("Billie Jean", track1.Title)
|
||||
assert.Equal("Michael Jackson", track1.Creator)
|
||||
assert.Equal("Thriller", track1.Album)
|
||||
}
|
||||
|
||||
func TestUnmarshalComprehensive(t *testing.T) {
|
||||
data, err := readSampleJson("testdata/comprehensive.jspf")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert := assert.New(t)
|
||||
playlist := data.Playlist
|
||||
assert.Equal("http://example.com/", playlist.License)
|
||||
require.Len(t, playlist.Attribution, 2)
|
||||
assert.Equal("http://example.com/", playlist.Attribution[0]["identifier"])
|
||||
assert.Equal("http://example.com/", playlist.Attribution[1]["location"])
|
||||
require.Len(t, playlist.Meta, 2)
|
||||
assert.Equal("345", playlist.Meta[1]["http://example.com/rel/2/"])
|
||||
require.Len(t, playlist.Links, 2)
|
||||
assert.Equal("http://example.com/body/1/", playlist.Links[0]["http://example.com/rel/1/"])
|
||||
}
|
||||
|
||||
func TestUnmarshalListenBrainzPlaylist(t *testing.T) {
|
||||
data, err := readSampleJson("testdata/lb-playlist.jspf")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert := assert.New(t)
|
||||
playlist := data.Playlist
|
||||
assert.Equal(
|
||||
"https://listenbrainz.org/playlist/96485e27-967a-492a-9d04-c5a819baa2f3",
|
||||
playlist.Identifier)
|
||||
expectedPlaylistDate, err := time.Parse(time.RFC3339, "2023-07-04T21:03:52.317148+00:00")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(expectedPlaylistDate, playlist.Date)
|
||||
assert.NotNil(playlist.Extension["https://musicbrainz.org/doc/jspf#playlist"])
|
||||
|
||||
require.Len(t, playlist.Tracks, 2)
|
||||
track1 := playlist.Tracks[0]
|
||||
assert.Equal(
|
||||
"https://musicbrainz.org/recording/3f2bdbbd-063e-478c-a394-6da0cb303302",
|
||||
track1.Identifier[0])
|
||||
fmt.Printf("Ext: %v\n", track1.Extension["https://musicbrainz.org/doc/jspf#track"])
|
||||
extension := track1.Extension["https://musicbrainz.org/doc/jspf#track"].(map[string]any)
|
||||
assert.NotNil(extension)
|
||||
assert.Equal("outsidecontext", extension["added_by"])
|
||||
}
|
||||
|
||||
func readSampleJson(path string) (jspf.JSPF, error) {
|
||||
var result jspf.JSPF
|
||||
jsonFile, err := os.Open(path)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(byteValue, &result)
|
||||
return result, err
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
{
|
||||
"playlist": {
|
||||
"title": "JSPF example",
|
||||
"creator": "Name of playlist author",
|
||||
"annotation": "Super playlist",
|
||||
"info": "http://example.com/",
|
||||
"location": "http://example.com/",
|
||||
"identifier": "http://example.com/",
|
||||
"image": "http://example.com/",
|
||||
"date": "2005-01-08T17:10:47-05:00",
|
||||
"license": "http://example.com/",
|
||||
"attribution": [
|
||||
{
|
||||
"identifier": "http://example.com/"
|
||||
},
|
||||
{
|
||||
"location": "http://example.com/"
|
||||
}
|
||||
],
|
||||
"link": [
|
||||
{
|
||||
"http://example.com/rel/1/": "http://example.com/body/1/"
|
||||
},
|
||||
{
|
||||
"http://example.com/rel/2/": "http://example.com/body/2/"
|
||||
}
|
||||
],
|
||||
"meta": [
|
||||
{
|
||||
"http://example.com/rel/1/": "my meta 14"
|
||||
},
|
||||
{
|
||||
"http://example.com/rel/2/": "345"
|
||||
}
|
||||
],
|
||||
"extension": {
|
||||
"http://example.com/app/1/": [
|
||||
"ARBITRARY_EXTENSION_BODY",
|
||||
{}
|
||||
],
|
||||
"http://example.com/app/2/": [
|
||||
"ARBITRARY_EXTENSION_BODY"
|
||||
]
|
||||
},
|
||||
"track": [
|
||||
{
|
||||
"location": [
|
||||
"http://example.com/1.ogg",
|
||||
"http://example.com/2.mp3"
|
||||
],
|
||||
"identifier": [
|
||||
"http://example.com/1/",
|
||||
"http://example.com/2/"
|
||||
],
|
||||
"title": "Track title",
|
||||
"creator": "Artist name",
|
||||
"annotation": "Some text",
|
||||
"info": "http://example.com/",
|
||||
"image": "http://example.com/",
|
||||
"album": "Album name",
|
||||
"trackNum": 1,
|
||||
"duration": 0,
|
||||
"link": [
|
||||
{
|
||||
"http://example.com/rel/1/": "http://example.com/body/1/"
|
||||
},
|
||||
{
|
||||
"http://example.com/rel/2/": "http://example.com/body/2/"
|
||||
}
|
||||
],
|
||||
"meta": [
|
||||
{
|
||||
"http://example.com/rel/1/": "my meta 14"
|
||||
},
|
||||
{
|
||||
"http://example.com/rel/2/": "345"
|
||||
}
|
||||
],
|
||||
"extension": {
|
||||
"http://example.com/app/1/": [
|
||||
"ARBITRARY_EXTENSION_BODY",
|
||||
{}
|
||||
],
|
||||
"http://example.com/app/2/": [
|
||||
"ARBITRARY_EXTENSION_BODY"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
58
internal/backends/jspf/testdata/lb-playlist.jspf
vendored
58
internal/backends/jspf/testdata/lb-playlist.jspf
vendored
|
@ -1,58 +0,0 @@
|
|||
{
|
||||
"playlist": {
|
||||
"creator": "outsidecontext",
|
||||
"date": "2023-07-04T21:03:52.317148+00:00",
|
||||
"extension": {
|
||||
"https://musicbrainz.org/doc/jspf#playlist": {
|
||||
"creator": "outsidecontext",
|
||||
"last_modified_at": "2023-07-10T10:03:48.833282+00:00",
|
||||
"public": false
|
||||
}
|
||||
},
|
||||
"identifier": "https://listenbrainz.org/playlist/96485e27-967a-492a-9d04-c5a819baa2f3",
|
||||
"title": "Fundst\u00fccke",
|
||||
"track": [
|
||||
{
|
||||
"creator": "Airghoul feat. Priest",
|
||||
"extension": {
|
||||
"https://musicbrainz.org/doc/jspf#track": {
|
||||
"added_at": "2023-07-04T21:05:59.492439+00:00",
|
||||
"added_by": "outsidecontext",
|
||||
"additional_metadata": {
|
||||
"caa_id": 32981136309,
|
||||
"caa_release_mbid": "fb7d69d6-0b4b-4f99-a77a-c3a0d786b52c"
|
||||
},
|
||||
"artist_identifiers": [
|
||||
"https://musicbrainz.org/artist/554a5819-6c3f-4734-ae4c-11eabb7ca2e0",
|
||||
"https://musicbrainz.org/artist/56ff293f-ec9a-4741-9d14-0537c4fb8f97"
|
||||
]
|
||||
}
|
||||
},
|
||||
"identifier": [
|
||||
"https://musicbrainz.org/recording/3f2bdbbd-063e-478c-a394-6da0cb303302"
|
||||
],
|
||||
"title": "Orange Forest"
|
||||
},
|
||||
{
|
||||
"creator": "Crippled Black Phoenix",
|
||||
"extension": {
|
||||
"https://musicbrainz.org/doc/jspf#track": {
|
||||
"added_at": "2023-07-10T10:03:48.833330+00:00",
|
||||
"added_by": "outsidecontext",
|
||||
"additional_metadata": {
|
||||
"caa_id": 28699084533,
|
||||
"caa_release_mbid": "92e7cf6c-e626-4409-81b0-0fcb8a0c3699"
|
||||
},
|
||||
"artist_identifiers": [
|
||||
"https://musicbrainz.org/artist/055b6082-b9cc-4688-85c4-8153c0ef2d70"
|
||||
]
|
||||
}
|
||||
},
|
||||
"identifier": [
|
||||
"https://musicbrainz.org/recording/14d612f0-4022-4adc-8cef-87a569e2d65c"
|
||||
],
|
||||
"title": "Lost"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
24
internal/backends/jspf/testdata/simple.jspf
vendored
24
internal/backends/jspf/testdata/simple.jspf
vendored
|
@ -1,24 +0,0 @@
|
|||
{
|
||||
"playlist": {
|
||||
"title": "Two Songs From Thriller",
|
||||
"creator": "MJ Fan",
|
||||
"track": [
|
||||
{
|
||||
"location": [
|
||||
"http://example.com/billiejean.mp3"
|
||||
],
|
||||
"title": "Billie Jean",
|
||||
"creator": "Michael Jackson",
|
||||
"album": "Thriller"
|
||||
},
|
||||
{
|
||||
"location": [
|
||||
"http://example.com/thegirlismine.mp3"
|
||||
],
|
||||
"title": "The Girl Is Mine",
|
||||
"creator": "Michael Jackson",
|
||||
"album": "Thriller"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue