mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19:28 +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
|
package jspf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.uploadedlobster.com/scotty/internal/models"
|
"go.uploadedlobster.com/scotty/internal/models"
|
||||||
|
"go.uploadedlobster.com/scotty/pkg/jspf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type JSPFBackend struct {
|
type JSPFBackend struct {
|
||||||
|
@ -31,7 +31,7 @@ type JSPFBackend struct {
|
||||||
title string
|
title string
|
||||||
creator string
|
creator string
|
||||||
identifier string
|
identifier string
|
||||||
tracks []Track
|
tracks []jspf.Track
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *JSPFBackend) Name() string { return "jspf" }
|
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.title = config.GetString("title")
|
||||||
b.creator = config.GetString("username")
|
b.creator = config.GetString("username")
|
||||||
b.identifier = config.GetString("identifier")
|
b.identifier = config.GetString("identifier")
|
||||||
b.tracks = make([]Track, 0)
|
b.tracks = make([]jspf.Track, 0)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,13 +75,13 @@ func (b *JSPFBackend) ImportLoves(export models.LovesResult, importResult models
|
||||||
return importResult, nil
|
return importResult, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func listenAsTrack(l models.Listen) Track {
|
func listenAsTrack(l models.Listen) jspf.Track {
|
||||||
l.FillAdditionalInfo()
|
l.FillAdditionalInfo()
|
||||||
track := trackAsTrack(l.Track)
|
track := trackAsTrack(l.Track)
|
||||||
extension := makeMusicBrainzExtension(l.Track)
|
extension := makeMusicBrainzExtension(l.Track)
|
||||||
extension.AddedAt = l.ListenedAt
|
extension.AddedAt = l.ListenedAt
|
||||||
extension.AddedBy = l.UserName
|
extension.AddedBy = l.UserName
|
||||||
track.Extension[MusicBrainzTrackExtensionId] = extension
|
track.Extension[jspf.MusicBrainzTrackExtensionId] = extension
|
||||||
|
|
||||||
if l.RecordingMbid != "" {
|
if l.RecordingMbid != "" {
|
||||||
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(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
|
return track
|
||||||
}
|
}
|
||||||
|
|
||||||
func loveAsTrack(l models.Love) Track {
|
func loveAsTrack(l models.Love) jspf.Track {
|
||||||
l.FillAdditionalInfo()
|
l.FillAdditionalInfo()
|
||||||
track := trackAsTrack(l.Track)
|
track := trackAsTrack(l.Track)
|
||||||
extension := makeMusicBrainzExtension(l.Track)
|
extension := makeMusicBrainzExtension(l.Track)
|
||||||
extension.AddedAt = l.Created
|
extension.AddedAt = l.Created
|
||||||
extension.AddedBy = l.UserName
|
extension.AddedBy = l.UserName
|
||||||
track.Extension[MusicBrainzTrackExtensionId] = extension
|
track.Extension[jspf.MusicBrainzTrackExtensionId] = extension
|
||||||
|
|
||||||
if l.RecordingMbid != "" {
|
if l.RecordingMbid != "" {
|
||||||
track.Identifier = append(track.Identifier, "https://musicbrainz.org/recording/"+string(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
|
return track
|
||||||
}
|
}
|
||||||
|
|
||||||
func trackAsTrack(t models.Track) Track {
|
func trackAsTrack(t models.Track) jspf.Track {
|
||||||
track := Track{
|
track := jspf.Track{
|
||||||
Title: t.TrackName,
|
Title: t.TrackName,
|
||||||
Album: t.ReleaseName,
|
Album: t.ReleaseName,
|
||||||
Creator: t.ArtistName(),
|
Creator: t.ArtistName(),
|
||||||
|
@ -117,8 +117,8 @@ func trackAsTrack(t models.Track) Track {
|
||||||
return track
|
return track
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeMusicBrainzExtension(t models.Track) MusicBrainzTrackExtension {
|
func makeMusicBrainzExtension(t models.Track) jspf.MusicBrainzTrackExtension {
|
||||||
extension := MusicBrainzTrackExtension{
|
extension := jspf.MusicBrainzTrackExtension{
|
||||||
AdditionalMetadata: t.AdditionalInfo,
|
AdditionalMetadata: t.AdditionalInfo,
|
||||||
ArtistIdentifiers: make([]string, len(t.ArtistMbids)),
|
ArtistIdentifiers: make([]string, len(t.ArtistMbids)),
|
||||||
}
|
}
|
||||||
|
@ -137,9 +137,9 @@ func makeMusicBrainzExtension(t models.Track) MusicBrainzTrackExtension {
|
||||||
return extension
|
return extension
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b JSPFBackend) writeJSPF(tracks []Track) error {
|
func (b JSPFBackend) writeJSPF(tracks []jspf.Track) error {
|
||||||
playlist := JSPF{
|
playlist := jspf.JSPF{
|
||||||
Playlist: Playlist{
|
Playlist: jspf.Playlist{
|
||||||
Title: b.title,
|
Title: b.title,
|
||||||
Creator: b.creator,
|
Creator: b.creator,
|
||||||
Identifier: b.identifier,
|
Identifier: b.identifier,
|
||||||
|
@ -154,11 +154,5 @@ func (b JSPFBackend) writeJSPF(tracks []Track) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
jspfJson, err := json.MarshalIndent(playlist, "", "\t")
|
return playlist.Write(file)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = file.Write(jspfJson)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
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"
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
|
@ -1,24 +1,31 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||||
|
|
||||||
This file is part of Scotty.
|
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:
|
||||||
|
|
||||||
Scotty is free software: you can redistribute it and/or modify it under the
|
The above copyright notice and this permission notice shall be included in
|
||||||
terms of the GNU General Public License as published by the Free Software
|
all copies or substantial portions of the 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
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
You should have received a copy of the GNU General Public License along with
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package jspf
|
package jspf
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
// A JSPF playlist
|
||||||
|
//
|
||||||
// See https://xspf.org/jspf
|
// See https://xspf.org/jspf
|
||||||
type JSPF struct {
|
type JSPF struct {
|
||||||
Playlist Playlist `json:"playlist"`
|
Playlist Playlist `json:"playlist"`
|
||||||
|
@ -61,15 +68,3 @@ type Attribution map[string]string
|
||||||
type Link map[string]string
|
type Link map[string]string
|
||||||
|
|
||||||
type Meta 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,25 +1,29 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||||
|
|
||||||
This file is part of Scotty.
|
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:
|
||||||
|
|
||||||
Scotty is free software: you can redistribute it and/or modify it under the
|
The above copyright notice and this permission notice shall be included in
|
||||||
terms of the GNU General Public License as published by the Free Software
|
all copies or substantial portions of the 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
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
You should have received a copy of the GNU General Public License along with
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package jspf_test
|
package jspf_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -27,7 +31,7 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uploadedlobster.com/scotty/internal/backends/jspf"
|
"go.uploadedlobster.com/scotty/pkg/jspf"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUnmarshalSimple(t *testing.T) {
|
func TestUnmarshalSimple(t *testing.T) {
|
||||||
|
@ -82,7 +86,6 @@ func TestUnmarshalListenBrainzPlaylist(t *testing.T) {
|
||||||
assert.Equal(
|
assert.Equal(
|
||||||
"https://musicbrainz.org/recording/3f2bdbbd-063e-478c-a394-6da0cb303302",
|
"https://musicbrainz.org/recording/3f2bdbbd-063e-478c-a394-6da0cb303302",
|
||||||
track1.Identifier[0])
|
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)
|
extension := track1.Extension["https://musicbrainz.org/doc/jspf#track"].(map[string]any)
|
||||||
assert.NotNil(extension)
|
assert.NotNil(extension)
|
||||||
assert.Equal("outsidecontext", extension["added_by"])
|
assert.Equal("outsidecontext", extension["added_by"])
|
Loading…
Add table
Reference in a new issue