-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
95 lines (90 loc) · 2.04 KB
/
xml.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
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"time"
)
// parse netflix-style xml subtitles
func ReadXMLFile(fname string) (Subs, error) {
s, err := ioutil.ReadFile(fname)
if err != nil {
return Subs{}, err
}
return ParseXML(string(s))
}
func ParseXML(s string) (Subs, error) {
decoder := xml.NewDecoder(bytes.NewReader([]byte(s)))
var subs Subs
var sub *Sub
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
switch token := token.(type) {
case xml.StartElement:
if token.Name.Local == "p" {
if sub != nil {
return subs, fmt.Errorf("did not expect <p> inside <p>")
}
var id string
var begin time.Duration = -1
var end time.Duration = -1
var err error
for _, attr := range token.Attr {
// fmt.Fprintln(os.Stderr, "got attr", attr.Name.Local, attr.Value)
if attr.Name.Local == "id" {
id = attr.Value
}
if attr.Name.Local == "begin" {
begin, err = parseXmlTime(attr.Value)
if err != nil {
return subs, err
}
}
if attr.Name.Local == "end" {
end, err = parseXmlTime(attr.Value)
if err != nil {
return subs, err
}
}
}
if id == "" || begin < 0 || end < 0 {
return subs, fmt.Errorf("<p> missing expected attribute")
}
sub = &Sub{
Number: len(subs.Sub),
From: begin,
To: end,
}
}
case xml.EndElement:
if token.Name.Local == "p" {
subs.Sub = append(subs.Sub, sub)
sub = nil
}
case xml.CharData:
if sub != nil {
sub.Lines = append(sub.Lines, strings.TrimSpace(string(token)))
}
}
}
return subs, nil
}
func parseXmlTime(s string) (time.Duration, error) {
if len(s) == 0 || s[len(s)-1] != 't' {
return 0, fmt.Errorf("expected xmlTime to end in 't': %s", s)
}
n, err := strconv.Atoi(s[:len(s)-1])
if err != nil {
return 0, err
}
// assuming xml ticks are 1/10000000s
// (can verify with `ttp:tickRate="10000000"` header)
return time.Duration(time.Duration(n) * (time.Second / 10000000)), nil
}