This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (151 loc) · 4.3 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
package main
import (
"log"
"encoding/json"
"os"
"io/ioutil"
"github.com/tubbebubbe/transmission"
t411 "github.com/Silvanosky/t411-client/t411client"
"bufio"
"time"
"os/signal"
"strconv"
)
type Setting struct {
TransmissionUser string `json:"TransmissionUser"`
TransmissionPass string `json:"TransmissionPass"`
TransmissionURL string `json:"TransmissionUrl"`
T411User string `json:"T411User"`
T411Pass string `json:"T411Pass"`
}
type Data struct {
TransmissionID string `json:"TransmissionID"`
T411ID string `json:"T411ID"`
}
type ListData []Data
func readJson(data interface{}, filename string) (error) {
jsonFile, err := os.Open(filename)
if err != nil {
return err
}
defer jsonFile.Close()
jsonData, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
}
if err = json.Unmarshal(jsonData, data); err != nil {
log.Printf("Error decoding file: %s for: %v", filename, err)
return err
}
return nil
}
func writeJson(data interface{}, filename string) (error) {
rankingsJson, err := json.Marshal(data)
if err != nil {
log.Printf("Error decoding file: %s for: %v", filename, err)
return err
}
err = ioutil.WriteFile(filename, rankingsJson, 0644)
if err != nil {
return err
}
return nil
}
func routineCheckLeave(done *bool) {
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
*done = true
log.Print("Will shutdown on the next loop !")
}
func main() {
var settings Setting
err := readJson(&settings, "config.json")
if err != nil {
log.Fatalln("Error JSON:", err)
}
t411Map := make(map[string]string)
data := make(ListData, 0)
readJson(&data, "data.json")
for _, d := range data{
t411Map[d.T411ID] = d.TransmissionID
}
log.Println("Adding shutdown hook..")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
<-c
log.Println("Saving data..")
data := make(ListData, 0)
for k, v := range t411Map{
data = append(data, Data{v, k})
}
writeJson(data, "data.json")
os.Exit(1)
}()
client := transmission.New(settings.TransmissionURL, settings.TransmissionUser, settings.TransmissionPass)
log.Println("Logged transmission")
var t411Client *t411.T411
for t411Client == nil {
t411Client, err = t411.NewT411ClientWithToken("", settings.T411User, settings.T411Pass, "")
if err != nil {
log.Println(err.Error())
}
}
t411Client.KeepRatio(false)
log.Println("Logged t411")
done := false
go routineCheckLeave(&done)
log.Print("Updating transmission torrents")
_, err = client.GetTorrents()
if err != nil {
log.Panic(err)
}
for !done {
log.Print("Fetching torrents of the day !")
dayTors, err := t411Client.TorrentsOfToday()
if err != nil {
log.Println(err.Error())
log.Print("Waiting 10 sec and retry!")
time.Sleep(30 * time.Second)
continue
}
for _, torrent := range *dayTors{
if _, ok := t411Map[string(torrent.ID)]; ok {
continue
}
if torrent.Owner != "94925822" && torrent.Owner != "99183906" && torrent.Owner != "1348889" && torrent.Owner != "102683783" && torrent.Owner != "103254977" && torrent.Owner != "97121229" && torrent.Owner != "100662827" && torrent.Owner != "5810986"&& torrent.Owner != "1807879" {// Arkhos01 and amzerzo35 profile ID
continue
}
seeders, _ := strconv.Atoi(torrent.Seeders)
//size, _ := strconv.Atoi(torrent.Size)
//leechers,_ := strconv.Atoi(torrent.Leechers)
if seeders == 0 { //Cancel over 18GB 18874368
continue
}
log.Println("Download Torrent:")
log.Println(" ID: ", torrent.ID)
log.Println(" Name: ", torrent.Name)
log.Println(" Added: ", torrent.Added)
log.Println(" IsVerified: ", torrent.IsVerified)
log.Println(" Category: ", torrent.Categoryname)
log.Println(" Owner: ", torrent.Owner)
log.Println(" Seeders: ", torrent.Seeders)
log.Println(" Leechers: ", torrent.Leechers)
log.Println(" Size: ", torrent.Size)
filename, e := t411Client.DownloadTorrent(&torrent)
if e != nil {
log.Println(e.Error())
}else {
cmd, _ := transmission.NewAddCmdByFilename(filename)
tor, e := client.ExecuteAddCommand(cmd)
if e != nil {
log.Println(e.Error())
}else {
t411Map[string(torrent.ID)] = strconv.Itoa(tor.ID)
}
}
}
log.Print("Waiting...")
time.Sleep(5 * time.Minute)
}
}