-
Notifications
You must be signed in to change notification settings - Fork 0
/
xspf.go
101 lines (95 loc) · 1.86 KB
/
xspf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/xml"
"fmt"
"io"
"net/url"
)
type xspfWriter struct {
wr io.Writer
enc *xml.Encoder
}
func newXSPFWriter(w io.Writer) *xspfWriter {
return &xspfWriter{
wr: w,
}
}
func (x *xspfWriter) Begin() error {
_, err := io.WriteString(x.wr, xml.Header)
if err != nil {
return err
}
x.enc = xml.NewEncoder(x.wr)
x.enc.Indent("", " ")
err = x.enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "playlist"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "version"}, Value: "1"},
{Name: xml.Name{Local: "xmlns"}, Value: "http://xspf.org/ns/0/"},
{Name: xml.Name{Local: "xmlns:vlc"}, Value: "http://www.videolan.org/vlc/playlist/ns/0/"},
},
})
if err != nil {
return err
}
err = x.enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "trackList"},
Attr: []xml.Attr{},
})
if err != nil {
return err
}
return nil
}
func (x *xspfWriter) End() error {
err := x.enc.EncodeToken(xml.EndElement{
Name: xml.Name{Local: "trackList"},
})
if err != nil {
return err
}
err = x.enc.EncodeToken(xml.EndElement{
Name: xml.Name{Local: "playlist"},
})
if err != nil {
return err
}
err = x.enc.Flush()
if err != nil {
return err
}
_, err = fmt.Fprintln(x.wr)
return err
}
func (x *xspfWriter) WriteTrack(filePath string) error {
err := x.enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "track"},
})
if err != nil {
return err
}
err = x.enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "location"},
})
if err != nil {
return err
}
fileURL := url.URL{
Scheme: "file",
Path: filePath,
}
err = x.enc.EncodeToken(xml.CharData(fileURL.String()))
if err != nil {
return err
}
err = x.enc.EncodeToken(xml.EndElement{
Name: xml.Name{Local: "location"},
})
if err != nil {
return err
}
err = x.enc.EncodeToken(xml.EndElement{
Name: xml.Name{Local: "track"},
})
return err
}