-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (190 loc) · 5.34 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
var (
folderPath = os.Getenv("TORBOX_WATCH_FOLDER")
torboxAPIKey = os.Getenv("TORBOX_API_KEY")
deleteAfterUpload bool
)
const (
maxRetries = 3
retryDelay = 5 * time.Second
torboxAPIBase = "https://api.torbox.app"
torboxAPIVersion = "v1"
)
type TorBoxResponse struct {
Success bool `json:"success"`
Detail string `json:"detail"`
}
func init() {
deleteAfterUpload = strings.ToLower(os.Getenv("DELETE_AFTER_UPLOAD")) == "true"
}
func uploadToTorBox(filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
for attempt := 0; attempt < maxRetries; attempt++ {
var err error
if strings.HasSuffix(filename, ".nzb") {
err = tryUploadUsenet(file, filename)
} else {
err = tryUploadTorrent(file, filename)
}
if err == nil {
log.Printf("Successfully uploaded %s to TorBox", filename)
if deleteAfterUpload {
if err := os.Remove(filename); err != nil {
log.Printf("Warning: Failed to delete file %s: %v", filename, err)
} else {
log.Printf("Deleted file: %s", filename)
}
}
return nil
}
log.Printf("Attempt %d failed: %v. Retrying in %v...", attempt+1, err, retryDelay)
time.Sleep(retryDelay)
// Rewind the file for the next attempt
_, err = file.Seek(0, 0)
if err != nil {
return fmt.Errorf("error rewinding file: %v", err)
}
}
return fmt.Errorf("failed to upload after %d attempts", maxRetries)
}
func tryUploadTorrent(file *os.File, filename string) error {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(filename))
if err != nil {
return fmt.Errorf("error creating form file: %v", err)
}
_, err = io.Copy(part, file)
if err != nil {
return fmt.Errorf("error copying file to form: %v", err)
}
writer.WriteField("seed", "1")
writer.WriteField("allow_zip", "true")
err = writer.Close()
if err != nil {
return fmt.Errorf("error closing multipart writer: %v", err)
}
url := fmt.Sprintf("%s/%s/api/torrents/createtorrent", torboxAPIBase, torboxAPIVersion)
return sendRequest(url, writer.FormDataContentType(), body)
}
func tryUploadUsenet(file *os.File, filename string) error {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(filename))
if err != nil {
return fmt.Errorf("error creating form file: %v", err)
}
_, err = io.Copy(part, file)
if err != nil {
return fmt.Errorf("error copying file to form: %v", err)
}
// Use the filename without the path and .nzb extension
name := strings.TrimSuffix(filepath.Base(filename), ".nzb")
err = writer.WriteField("name", name)
if err != nil {
return fmt.Errorf("error writing name field: %v", err)
}
err = writer.Close()
if err != nil {
return fmt.Errorf("error closing multipart writer: %v", err)
}
url := fmt.Sprintf("%s/%s/api/usenet/createusenetdownload", torboxAPIBase, torboxAPIVersion)
return sendRequest(url, writer.FormDataContentType(), body)
}
func sendRequest(url, contentType string, body *bytes.Buffer) error {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+torboxAPIKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var torboxResp TorBoxResponse
err = json.NewDecoder(resp.Body).Decode(&torboxResp)
if err != nil {
return fmt.Errorf("error decoding response: %v", err)
}
if !torboxResp.Success {
return fmt.Errorf("TorBox API error: %s", torboxResp.Detail)
}
return nil
}
func watchFolder() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create == fsnotify.Create {
if strings.HasSuffix(event.Name, ".torrent") || strings.HasSuffix(event.Name, ".magnet") || strings.HasSuffix(event.Name, ".nzb") {
log.Println("New file detected:", event.Name)
err := uploadToTorBox(event.Name)
if err != nil {
log.Printf("Error uploading %s: %v", event.Name, err)
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error:", err)
}
}
}()
err = watcher.Add(folderPath)
if err != nil {
log.Fatal(err)
}
log.Printf("Watching folder: %s", folderPath)
<-done
}
func main() {
log.Println("Krantorbox Auto-Upload Started")
log.Printf("Watching folder: %s", folderPath)
log.Printf("TorBox API Base: %s", torboxAPIBase)
log.Printf("TorBox API Version: %s", torboxAPIVersion)
if deleteAfterUpload {
log.Println("File deletion after upload is enabled")
} else {
log.Println("File deletion after upload is disabled")
}
if folderPath == "" || torboxAPIKey == "" {
log.Fatal("Please set all required environment variables")
}
watchFolder()
}