-
Notifications
You must be signed in to change notification settings - Fork 0
/
nzb.go
74 lines (63 loc) · 1.69 KB
/
nzb.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
package main
import (
"encoding/xml"
"bytes"
"io"
)
// a slice of NzbFiles extended to allow sorting
type NzbFileSlice []*NzbFile
func (s NzbFileSlice) Len() int { return len(s) }
func (s NzbFileSlice) Less(i, j int) bool { return s[i].Part < s[j].Part }
func (s NzbFileSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type Nzb struct {
Meta map[string]string
Files NzbFileSlice
}
func NewString(data string) (*Nzb, error) {
return New(bytes.NewBufferString(data))
}
func New(buf io.Reader) (*Nzb, error) {
xnzb := new(xNzb)
err := xml.NewDecoder(buf).Decode(xnzb)
if err != nil {
return nil, err
}
// convert to nicer format
nzb := new(Nzb)
// convert metadata
nzb.Meta = make(map[string]string)
for _, md := range xnzb.Metadata {
nzb.Meta[md.Type] = md.Value
}
// copy files into (sortable) NzbFileSlice
nzb.Files = make(NzbFileSlice, 0)
for i, _ := range xnzb.File {
nzb.Files = append(nzb.Files, &xnzb.File[i])
}
return nzb, nil
}
// used only for unmarshalling xml
type xNzb struct {
XMLName xml.Name `xml:"nzb"`
Metadata []xNzbMeta `xml:"head>meta"`
File []NzbFile `xml:"file"` // xml:tag name doesn't work?
}
// used only in unmarshalling xml
type xNzbMeta struct {
Type string `xml:"type,attr"`
Value string `xml:",innerxml"`
}
type NzbFile struct {
Groups []string `xml:"groups>group"`
Segments []NzbSegment `xml:"segments>segment"`
Poster string `xml:"poster,attr"`
Date int `xml:"date,attr"`
Subject string `xml:"subject,attr"`
Part int
}
type NzbSegment struct {
XMLName xml.Name `xml:"segment"`
Bytes int `xml:"bytes,attr"`
Number int `xml:"number,attr"`
Id string `xml:",innerxml"`
}