-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmission.go
125 lines (115 loc) · 3.1 KB
/
transmission.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
package main
import (
"regexp"
"strconv"
"strings"
"errors"
"github.com/lnguyen/go-transmission/transmission"
)
const (
StatusStopped = iota
StatusCheckPending
StatusChecking
StatusDownloadPending
StatusDownloading
StatusSeedPending
StatusSeeding
)
type CmdConfigTransmission struct {
St_list string
St_clean string
Reg_del *regexp.Regexp
}
var bt_client transmission.TransmissionClient
func NewCmdTransmission(config *CmdConfigTransmission, url string, user string, pass string) {
config.St_list = "/bt_list"
config.St_clean = "/bt_clean"
config.Reg_del = regexp.MustCompile(`^/bt_del [0-9]+$`)
bt_client = transmission.New(url, user, pass)
}
func BtDown (hash string) (string){
torrent, err := bt_client.AddTorrentByURL("https://yts.ag/torrent/download/" + hash, "")
var stemp string
if err != nil{
stemp = "Error!\n\n" + err.Error()
}else if torrent.Name == ""{
stemp = "Error!\n\nYou don't have permission to download this torrent. Try manually, but you will probably need to be logged in."
}else{
stemp = "Success!\n\nAdded: " + torrent.Name + "\nID: " + strconv.Itoa(torrent.ID)
}
return stemp
}
func BtList() (string, error){
torrents, err := bt_client.GetTorrents()
if err != nil{
return "", err
}
if len(torrents) == 0{
return "No torrents in the list!", nil
}
var s_eta string
s := "<--- Torrent list --->\n"
for _, torrent := range torrents{
if torrent.Eta == -1{
s_eta = "Not available"
}else if torrent.Eta == -2{
s_eta = "Unknown"
}else{
s_eta = strconv.Itoa(torrent.Eta/60) + " Min"
}
s += "\n<" + strconv.Itoa(torrent.ID) + "> " + torrent.Name + "\n" + BtStatus(&torrent) +
" - Remains " + strconv.Itoa(torrent.LeftUntilDone/1000000) + "MB - Complete " +
strconv.FormatFloat(torrent.PercentDone*100, 'f', -1, 32) + "% - ETA " +
s_eta
}
return s, nil
}
func BtStatus (t *transmission.Torrent) string {
switch t.Status {
case StatusStopped:
return "Stopped"
case StatusCheckPending:
return "Check waiting"
case StatusChecking:
return "Checking"
case StatusDownloadPending:
return "Download waiting"
case StatusDownloading:
return "Downloading"
case StatusSeedPending:
return "Seed waiting"
case StatusSeeding:
return "Seeding"
default:
return "unknown"
}
}
func BtRemove(a string) (string, error){
b := strings.SplitN(a, " ", 2)
if len(b) != 2{
return "", errors.New("Error parsing bt_del command")
}
c, err := strconv.Atoi(b[1])
if err != nil{
return "", err
}
d, err := bt_client.RemoveTorrent(c, true)//Removes the torrent AND the file
return d, err
}
func BtClean() (string, error){//Removes complete torrents from the list (not the data)
torrents, err := bt_client.GetTorrents()
if err != nil{
return "", err
}
res := "<--- Removed torrents --->\n"
for _, torrent := range torrents{
if torrent.PercentDone == 1{
_, err := bt_client.RemoveTorrent(torrent.ID, false)
if err != nil{
return "", err
}
res += "\n- " + torrent.Name
}
}
return res, nil
}