-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtarget_nzbget.go
107 lines (95 loc) · 2.68 KB
/
target_nzbget.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
102
103
104
105
106
107
package main
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"fmt"
"regexp"
)
// target functions for NZBGet
// function to get the categories
func nzbget_getCategories() (Categories, error) {
// response structure
type responseStruct struct {
Result []struct {
Name string `json:"Name"`
Value string `json:"Value"`
} `json:"result"`
}
var categories Categories
if response, err := request(conf.Nzbget, "GET", "jsonrpc/config", nil, nil, nil, ""); err != nil {
return nil, err
} else {
var jsonResponse responseStruct
if err := json.Unmarshal(response, &jsonResponse); err != nil {
return nil, err
}
if len(jsonResponse.Result) > 0 {
categoryRegexp := regexp.MustCompile(`Category\d+\.Name`)
for _, item := range jsonResponse.Result {
if categoryRegexp.Match([]byte(item.Name)) {
categories = append(categories, item.Value)
}
}
} else {
return nil, fmt.Errorf("received an empty response")
}
}
return categories, nil
}
// function to push the nzb file to the queue
func nzbget_push(nzb string, category string) error {
fmt.Println()
Log.Info("Pushing the NZB file to NZBGet...")
// response structure
type responseStruct struct {
Result int `json:"result"`
}
// if category is empty set to default category
if category == "" && conf.Nzbget.Category != "" {
category = conf.Nzbget.Category
}
// if category is provided as argument use category from arguments
if args.Category != "" {
category = args.Category
}
// prepare body data
var data = map[string]interface{}{
"version": "1.1",
"id": 0,
"method": "append",
"params": []interface{}{
args.Title + ".nzb", // Filename
b64.StdEncoding.EncodeToString([]byte(nzb)), // Content (NZB File)
category, // Category
0, // Priority
false, // AddToTop
conf.Nzbget.Addpaused, // AddPaused
"", // DupeKey
0, // DupeScore
"ALL", // DupeMode
map[string]interface{}{
"*unpack:password": args.Password, // Post processing parameter: Password
},
},
}
if body, err := json.Marshal(data); err != nil {
return fmt.Errorf("cannot create body data: %v", err)
} else {
if response, err := request(conf.Nzbget, "POST", "jsonrpc", nil, nil, bytes.NewBuffer(body), ""); err != nil {
return err
} else {
var jsonResponse responseStruct
if err := json.Unmarshal(response, &jsonResponse); err != nil {
return err
} else {
if jsonResponse.Result > 0 {
Log.Succ("The NZB file was pushed to NZBGet")
} else {
return fmt.Errorf("received an empty or unknown response")
}
}
}
}
return nil
}