-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (139 loc) · 3.72 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
package main
import (
"github.com/latchmihay/rtapi"
"fmt"
"log"
"strconv"
"time"
"os"
"regexp"
"strings"
"path/filepath"
"errors"
"os/exec"
)
const (
hourTime = 3600
weekTime = 604800
logFileLoc = "/tmp/ks-tor.log"
)
// check error function
func CheckErr(err error) {
if err == nil {
return
}
log.Fatal(err)
}
func checkFolder(path string) error {
_, err := os.Stat(path)
if err != nil {
msg := fmt.Sprintf("Folder %s was not found! Torrent needs extraction!", path)
return errors.New(msg)
}
return nil
}
func extract(from string,to string) error {
script := "/var/www/rutorrent/plugins/unpack/unrar_dir.sh"
arg1 := "/usr/bin/unrar"
out, err := exec.Command(script, arg1, from, to).Output()
if err != nil {
return err
}
log.Printf("\n%s\n", out)
return nil
}
func main() {
f, err := os.OpenFile(logFileLoc, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
// set logging format
log.SetFlags(log.LstdFlags)
log.SetOutput(f)
rt, err := rtapi.NewRtorrent("127.0.0.1:51102") // Or /path/to/socket for "scgi_local".
if err != nil {
CheckErr(err)
}
// Get torrents
torrents, err := rt.Torrents()
if err != nil {
CheckErr(err)
}
torrents.Sort(rtapi.ByAgeLoad)
log.Println("Number of torrents:", len(torrents))
for _, t := range torrents {
// Run only on finished torrents
if t.State == "Seeding" {
ageToString := strconv.FormatUint(t.AgeLoad, 10)
ageStringToInt, err := strconv.ParseInt(ageToString, 10,64)
CheckErr(err)
humanTime := time.Unix(ageStringToInt, 0)
ageToInt, err := strconv.Atoi(ageToString)
CheckErr(err)
ageToIntWeeek := ageToInt + weekTime + hourTime
now := time.Now()
nowUnix := now.Unix()
nowUnixString := strconv.FormatInt(nowUnix, 10)
nowUnixInt, err := strconv.Atoi(nowUnixString)
CheckErr(err)
fmt.Printf("%s [%s] [%s] [%s] [%s]\n",t.Name,t.State,t.Percent, t.Path, humanTime)
if nowUnixInt > ageToIntWeeek {
// Torrent is older then a week, deleting it with its data
log.Printf("Torrent %s is older than a Week and a hour, deleting it now!", t.Name)
err = rt.Delete(true, t)
CheckErr(err)
}
tvRegEx, err := regexp.Compile("(?i)s[0-9][0-9]e[0-9][0-9]")
CheckErr(err)
sesonRegEx, err := regexp.Compile("(?i)s[0-9][0-9]")
CheckErr(err)
movRegEx, err := regexp.Compile("(?i)(bluray)|(hdrip)|(brrip)|(web-dl)|(dvdrip)|(webrip)|(hdtv)|(bdrip)")
CheckErr(err)
sportRegEx, err := regexp.Compile("(?i)(motogp)|(formula)")
CheckErr(err)
parseTorName := strings.Split(t.Name, ".")
loop:for _,v := range parseTorName {
switch {
case tvRegEx.MatchString(v):
log.Printf("Torrent %s matched TV\n", t.Name)
folderName := filepath.Base(t.Path)
err = checkFolder("/TV/" + folderName)
if err != nil {
log.Println(err)
extract(t.Path, "/TV/" )
}
break loop
case sesonRegEx.MatchString(v):
log.Printf("Torrent %s matched TV\n", t.Name)
folderName := filepath.Base(t.Path)
err = checkFolder("/TV/" + folderName)
if err != nil {
log.Println(err)
extract(t.Path, "/TV/" )
}
break loop
case sportRegEx.MatchString(v):
log.Printf("Torrent %s matched Sport\n", t.Name)
folderName := filepath.Base(t.Path)
err = checkFolder("/Sport/" + folderName)
if err != nil {
log.Println(err)
extract(t.Path, "/Sport/")
}
break loop
case movRegEx.MatchString(v):
log.Printf("Torrent %s matched Movie\n", t.Name)
folderName := filepath.Base(t.Path)
err = checkFolder("/Mov/" + folderName)
if err != nil {
log.Println(err)
extract(t.Path, "/Mov/" )
}
break loop
}
}
}
}
}