Skip to content

Commit

Permalink
Add marshall Function
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-emmanuelJ authored and jamesnetherton committed Dec 13, 2019
1 parent 9bff291 commit a464a14
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
40 changes: 37 additions & 3 deletions m3u.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package m3u

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
Expand All @@ -18,7 +20,7 @@ type Playlist struct {

// A Tag is a simple key/value pair
type Tag struct {
Name string
Name string
Value string
}

Expand All @@ -40,7 +42,7 @@ func Parse(fileName string) (playlist Playlist, err error) {
} else {
f, err = os.Open(fileName)
}

if err != nil {
err = errors.New("Unable to open playlist file")
return
Expand All @@ -50,7 +52,7 @@ func Parse(fileName string) (playlist Playlist, err error) {
onFirstLine := true
scanner := bufio.NewScanner(f)
tagsRegExp, _ := regexp.Compile("([a-zA-Z0-9-]+?)=\"([^\"]+)\"")

for scanner.Scan() {
line := scanner.Text()
if onFirstLine && !strings.HasPrefix(line, "#EXTM3U") {
Expand Down Expand Up @@ -92,3 +94,35 @@ func Parse(fileName string) (playlist Playlist, err error) {

return playlist, nil
}

// Marshall Playlist to an m3u file.
func Marshall(p Playlist) (io.Reader, error) {
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)
if err := MarshallInto(p, w); err != nil {
return nil, err
}

return buf, nil
}

// MarshallInto a *bufio.Writer a Playlist.
func MarshallInto(p Playlist, into *bufio.Writer) error {
into.WriteString("#EXTM3U\n")
for _, track := range p.Tracks {
into.WriteString("#EXTINF:")
into.WriteString(fmt.Sprintf("%d ", track.Length))
for i := range track.Tags {
if i == len(track.Tags)-1 {
into.WriteString(fmt.Sprintf("%s=%q", track.Tags[i].Name, track.Tags[i].Value))
continue
}
into.WriteString(fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value))
}
into.WriteString(", ")

into.WriteString(fmt.Sprintf("%s\n%s\n", track.Name, track.URI))
}

return into.Flush()
}
63 changes: 55 additions & 8 deletions m3u_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package m3u

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
)

Expand All @@ -25,13 +28,13 @@ func TestPlaylist(t *testing.T) {
t.Fatalf("Expected track URI to be Track%d.mp4 but was '%s'", i+1, playlist.Tracks[i].URI)
}

if playlist.Tracks[i].Tags[0].Name != "group-title" {
if playlist.Tracks[i].Tags[0].Name != "group-title" {
t.Fatalf("Expected tag to be group-title but was '%s'", playlist.Tracks[i].Tags[0].Name)
}
}

if playlist.Tracks[i].Tags[0].Value != "Album1" {
if playlist.Tracks[i].Tags[0].Value != "Album1" {
t.Fatalf("Expected group-title tag value to be Album1 but was '%s'", playlist.Tracks[i].Tags[0].Value)
}
}
}
}

Expand All @@ -55,13 +58,13 @@ func TestRemotePlaylist(t *testing.T) {
t.Fatalf("Expected track URI to be Track%d.mp4 but was '%s'", i+1, playlist.Tracks[i].URI)
}

if playlist.Tracks[i].Tags[0].Name != "group-title" {
if playlist.Tracks[i].Tags[0].Name != "group-title" {
t.Fatalf("Expected tag to be group-title but was '%s'", playlist.Tracks[i].Tags[0].Name)
}
}

if playlist.Tracks[i].Tags[0].Value != "Album1" {
if playlist.Tracks[i].Tags[0].Value != "Album1" {
t.Fatalf("Expected group-title tag value to be Album1 but was '%s'", playlist.Tracks[i].Tags[0].Value)
}
}
}
}

Expand Down Expand Up @@ -92,3 +95,47 @@ func TestPlaylistMissingInf(t *testing.T) {
t.Fatalf("Expected parse error")
}
}

func TestMarshallPlaylist(t *testing.T) {
playlist, err := Parse("testdata/playlist.m3u")
if err != nil {
t.Fatal(err)
}

reader, err := Marshall(playlist)
if err != nil {
t.Fatal(err)
}

b := reader.(*bytes.Buffer)

ioutil.WriteFile("/tmp/test_m3u_marshalling.m3u", b.Bytes(), os.ModePerm)

playlist, _ = Parse("/tmp/test_m3u_marshalling.m3u")

if len(playlist.Tracks) != 5 {
t.Fatalf("Expected track count to be 5")
}

for i := 0; i < 5; i++ {
if playlist.Tracks[i].Length != i+1 {
t.Fatalf("Expected track Length to be %d but was %d", i+1, playlist.Tracks[i].Length)
}

if playlist.Tracks[i].Name != fmt.Sprintf("Track %d", i+1) {
t.Fatalf("Expected track name to be Track %d but was '%s'", i+1, playlist.Tracks[i].Name)
}

if playlist.Tracks[i].URI != fmt.Sprintf("Track%d.mp4", i+1) {
t.Fatalf("Expected track URI to be Track%d.mp4 but was '%s'", i+1, playlist.Tracks[i].URI)
}

if playlist.Tracks[i].Tags[0].Name != "group-title" {
t.Fatalf("Expected tag to be group-title but was '%s'", playlist.Tracks[i].Tags[0].Name)
}

if playlist.Tracks[i].Tags[0].Value != "Album1" {
t.Fatalf("Expected group-title tag value to be Album1 but was '%s'", playlist.Tracks[i].Tags[0].Value)
}
}
}

0 comments on commit a464a14

Please sign in to comment.