forked from izolight/GoPostStuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nzb.go
87 lines (75 loc) · 2.25 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
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/xml"
"io/ioutil"
"regexp"
"strings"
"sort"
)
const (
NzbHeader = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
NzbDoctype = `<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.1//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd">` + "\n"
)
type NzbFiles []NzbFile
func (s NzbFiles) Len() int { return len(s) }
func (s NzbFiles) Less(i, j int) bool { return s[i].Subject < s[j].Subject }
func (s NzbFiles) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type NzbSegments []NzbSegment
func (s NzbSegments) Len() int { return len(s) }
func (s NzbSegments) Less(i, j int) bool { return s[i].Number < s[j].Number }
func (s NzbSegments) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type Nzb struct {
XMLName xml.Name `xml:"nzb"`
XMLns string `xml:"xmlns,attr"`
Head []Meta `xml:"head>meta"`
File NzbFiles `xml:"file"`
}
type Meta struct {
Type string `xml:"type,attr"`
Value string `xml:",innerxml"`
}
type NzbFile struct {
Poster string `xml:"poster,attr"`
Date int64 `xml:"date,attr"`
Subject string `xml:"subject,attr"`
Groups []string `xml:"groups>group"`
Segments NzbSegments `xml:"segments>segment"`
}
type NzbSegment struct {
XMLName xml.Name `xml:"segment"`
Bytes int64 `xml:"bytes,attr"`
Number int64 `xml:"number,attr"`
MessageId string `xml:",innerxml"`
}
func CreateNzb(filename string, nzb *Nzb) error {
sort.Sort(nzb.File)
for i, _ := range nzb.File {
sort.Sort(nzb.File[i].Segments)
}
nzb.XMLns = "http://www.newzbin.com/DTD/2003/nzb"
if output, err := xml.MarshalIndent(nzb, "", " "); err == nil {
output = []byte(NzbHeader + NzbDoctype + string(output))
err := ioutil.WriteFile(filename, output, 0755)
if err != nil {
return err
}
}
return nil
}
func SafeFileName(str string) string {
name := strings.ToLower(str)
//name = path.Clean(path.Base(name))
name = strings.Trim(name, " ")
separators, err := regexp.Compile(`[ &_=+:]`)
if err == nil {
name = separators.ReplaceAllString(name, "-")
}
legal, err := regexp.Compile(`[^[:alnum:]-.]|-yenc-\d*.+`)
if err == nil {
name = legal.ReplaceAllString(name, "")
}
for strings.Contains(name, "--") {
name = strings.Replace(name, "--", "-", -1)
}
return name
}