mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 05:37:05 +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
21
pkg/jspf/COPYING
Normal file
21
pkg/jspf/COPYING
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
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.
|
86
pkg/jspf/extensions.go
Normal file
86
pkg/jspf/extensions.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
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 "time"
|
||||
|
||||
const (
|
||||
// The identifier for the MusicBrainz / ListenBrainz JSPF playlist extension
|
||||
MusicBrainzPlaylistExtensionId = "https://musicbrainz.org/doc/jspf#playlist"
|
||||
// The identifier for the MusicBrainz / ListenBrainz JSPF track extension
|
||||
MusicBrainzTrackExtensionId = "https://musicbrainz.org/doc/jspf#track"
|
||||
)
|
||||
|
||||
// MusicBrainz / ListenBrainz JSPF track extension
|
||||
//
|
||||
// This extension allows storing additional metadata for playlists.
|
||||
//
|
||||
// See "https://musicbrainz.org/doc/jspf#playlist"
|
||||
type MusicBrainzPlaylistExtension struct {
|
||||
// The ListenBrainz user who created this playlist.
|
||||
Creator string `json:"added_by,omitempty"`
|
||||
// Which ListenBrainz user was the playlist generated for? This is for music
|
||||
// recommendation bots generating playlists for users.
|
||||
CreatedFor string `json:"created_for,omitempty"`
|
||||
// Who are the ListenBrainz users who have access to edit this playlist?
|
||||
Collaborators []string `json:"collaborators,omitempty"`
|
||||
// This field identifies a playlist, using the identifier syntax,
|
||||
// from which this playlist was copied from.
|
||||
CopiedFrom string `json:"copied_from,omitempty"`
|
||||
// If the source playlist that this playlist has been copied from has been
|
||||
// deleted, this field will be set to true and the copied_from field will not
|
||||
// be returned.
|
||||
CopiedFromDeleted bool `json:"copied_from_deleted,omitempty"`
|
||||
// Indicates if this playlist is public or private. Must contain the value
|
||||
// "true" or "false".
|
||||
Public string `json:"public,omitempty"`
|
||||
// The timestamp for when this playlist was last modified.
|
||||
LastModifiedAt time.Time `json:"last_modified_at,omitempty"`
|
||||
// This dict allows a playlist creator to submit additional track metadata
|
||||
// that may be used by playlist generation tools. The content of this field
|
||||
// is defined by the playlist generation tools and is beyond the scope of
|
||||
// this document.
|
||||
AdditionalMetadata map[string]any `json:"additional_metadata,omitempty"`
|
||||
}
|
||||
|
||||
// MusicBrainz / ListenBrainz JSPF track extension
|
||||
//
|
||||
// This extension allows storing additional metadata for tracks.
|
||||
//
|
||||
// See "https://musicbrainz.org/doc/jspf#track"
|
||||
type MusicBrainzTrackExtension struct {
|
||||
// The timestamp for when this track was added to the playlist.
|
||||
AddedAt time.Time `json:"added_at,omitempty"`
|
||||
// The ListenBrainz user who added this track.
|
||||
AddedBy string `json:"added_by,omitempty"`
|
||||
// The MusicBrainz ID URI for the release that contained this track.
|
||||
ReleaseIdentifier string `json:"release_identifier,omitempty"`
|
||||
// A list of MusicBrainz Artist URIs that identify the artist that are part
|
||||
// of the MusicBrainz artist credit id for this track.
|
||||
ArtistIdentifiers []string `json:"artist_identifiers,omitempty"`
|
||||
// This dict allows a playlist creator to submit additional track metadata
|
||||
// that may be used by playlist generation tools. The content of this field
|
||||
// is defined by the playlist generation tools and is beyond the scope of
|
||||
// this document.
|
||||
AdditionalMetadata map[string]any `json:"additional_metadata,omitempty"`
|
||||
}
|
74
pkg/jspf/extensions_test.go
Normal file
74
pkg/jspf/extensions_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
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_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"go.uploadedlobster.com/scotty/pkg/jspf"
|
||||
)
|
||||
|
||||
func ExampleMusicBrainzTrackExtension() {
|
||||
pl := jspf.JSPF{
|
||||
Playlist: jspf.Playlist{
|
||||
Date: time.Date(2023, 11, 24, 07, 47, 50, 0, time.UTC),
|
||||
Tracks: []jspf.Track{
|
||||
{
|
||||
Title: "Oweynagat",
|
||||
Extension: map[string]any{
|
||||
jspf.MusicBrainzTrackExtensionId: jspf.MusicBrainzTrackExtension{
|
||||
AddedAt: time.Date(2023, 11, 24, 07, 47, 50, 0, time.UTC),
|
||||
AddedBy: "scotty",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 10))
|
||||
err := pl.Write(buf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print(buf.String())
|
||||
// Output:
|
||||
// {
|
||||
// "playlist": {
|
||||
// "date": "2023-11-24T07:47:50Z",
|
||||
// "track": [
|
||||
// {
|
||||
// "title": "Oweynagat",
|
||||
// "extension": {
|
||||
// "https://musicbrainz.org/doc/jspf#track": {
|
||||
// "added_at": "2023-11-24T07:47:50Z",
|
||||
// "added_by": "scotty"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
}
|
52
pkg/jspf/io.go
Normal file
52
pkg/jspf/io.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
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 for reading and writing music playlists in the JSPF format.
|
||||
//
|
||||
// JSPF is the JSON representation of XSPF, see https://www.xspf.org/jspf
|
||||
package jspf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Reads the JSPF playlist in JSON format from the given io.Reader
|
||||
func (j *JSPF) Read(in io.Reader) error {
|
||||
bytes, err := io.ReadAll(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(bytes, j)
|
||||
return err
|
||||
}
|
||||
|
||||
// Writes the JSPF playlist in JSON format to the given io.Writer
|
||||
func (j *JSPF) Write(out io.Writer) error {
|
||||
jspfJson, err := json.MarshalIndent(j, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = out.Write(jspfJson)
|
||||
return err
|
||||
}
|
86
pkg/jspf/io_test.go
Normal file
86
pkg/jspf/io_test.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
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_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.uploadedlobster.com/scotty/pkg/jspf"
|
||||
)
|
||||
|
||||
func ExampleJSPF_Read() {
|
||||
file, err := os.Open("testdata/simple.jspf")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
pl := jspf.JSPF{}
|
||||
err = pl.Read(file)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Playlist '%v' with %v tracks",
|
||||
pl.Playlist.Title, len(pl.Playlist.Tracks))
|
||||
// Output: Playlist 'Two Songs From Thriller' with 2 tracks
|
||||
}
|
||||
|
||||
func ExampleJSPF_Write() {
|
||||
date, _ := time.Parse(time.RFC3339, "2023-11-23T23:56:00Z")
|
||||
pl := jspf.JSPF{
|
||||
Playlist: jspf.Playlist{
|
||||
Title: "The Playlist",
|
||||
Creator: "Scotty",
|
||||
Date: date,
|
||||
Tracks: []jspf.Track{
|
||||
{
|
||||
Title: "The Track",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 10))
|
||||
err := pl.Write(buf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print(buf.String())
|
||||
// Output:
|
||||
// {
|
||||
// "playlist": {
|
||||
// "title": "The Playlist",
|
||||
// "creator": "Scotty",
|
||||
// "date": "2023-11-23T23:56:00Z",
|
||||
// "track": [
|
||||
// {
|
||||
// "title": "The Track"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
}
|
70
pkg/jspf/models.go
Normal file
70
pkg/jspf/models.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
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 "time"
|
||||
|
||||
// A JSPF playlist
|
||||
//
|
||||
// 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
|
109
pkg/jspf/models_test.go
Normal file
109
pkg/jspf/models_test.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
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_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uploadedlobster.com/scotty/pkg/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])
|
||||
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
|
||||
}
|
91
pkg/jspf/testdata/comprehensive.jspf
vendored
Normal file
91
pkg/jspf/testdata/comprehensive.jspf
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
{
|
||||
"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
pkg/jspf/testdata/lb-playlist.jspf
vendored
Normal file
58
pkg/jspf/testdata/lb-playlist.jspf
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
{
|
||||
"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
pkg/jspf/testdata/simple.jspf
vendored
Normal file
24
pkg/jspf/testdata/simple.jspf
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"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