forked from krol44/tg-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object-torrent.go
198 lines (162 loc) · 4.83 KB
/
object-torrent.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
package main
import (
"context"
"fmt"
"github.com/anacrolix/torrent"
tgbotapi "github.com/krol44/telegram-bot-api"
log "github.com/sirupsen/logrus"
"path"
"strconv"
"strings"
"time"
)
func (o *ObjectTorrent) Download() bool {
if o.Task.Limit("torrent") {
log.Debug("limit exceeded")
return false
}
if !o.Task.AllocTorrent("torrent") {
o.Task.App.SendLogToChannel(o.Task.Message.From, "mess", "❗️ return - error alloc")
return false
}
o.Task.Torrent.Process = o.TorrentProcess
var fileChosen *torrent.File
for _, val := range o.Task.Torrent.Process.Files() {
recoveryPath := val.Path() + " ~ " + strconv.FormatInt(val.Length()>>20, 10) + " MB"
if strings.Contains(recoveryPath, o.Task.Message.Text) {
if val.Length() > 1999e6 { // more 2 GB
o.Task.Send(tgbotapi.NewMessage(o.Task.Message.Chat.ID,
fmt.Sprintf("😔 "+o.Task.Lang("File is bigger 2 GB"))))
o.Task.App.SendLogToChannel(o.Task.Message.From, "mess", "File is bigger 2 GB")
_, err := Postgres.Exec(`DELETE FROM limits WHERE id = any
(array(SELECT id FROM limits WHERE telegram_id = $1 AND type_object = $2
ORDER BY date_create DESC LIMIT 1))`,
o.Task.Message.From.ID, "torrent")
if err != nil {
log.Error(err)
}
return false
}
val.SetPriority(torrent.PiecePriorityNow)
fileChosen = val
} else {
val.SetPriority(torrent.PiecePriorityNone)
}
}
if fileChosen == nil {
log.Error("file chosen is empty")
return false
}
// if name not correct
o.Task.Torrent.Process.SetDisplayName(o.Task.UniqueId("temp-torrent-name"))
o.Task.Torrent.Process.SetMaxEstablishedConns(200)
<-o.Task.Torrent.Process.GotInfo()
o.Task.Torrent.Name = fileChosen.DisplayPath()
ctx, cancelProgress := context.WithCancel(context.Background())
go func(ctx context.Context, fileChosen *torrent.File) {
for {
select {
case <-ctx.Done():
return
default:
if _, bo := o.Task.App.ChatsWork.StopTasks.Load(o.Task.Message.Chat.ID); bo {
return
}
stat, percent := o.Task.StatDlTor(fileChosen)
if percent == 100 {
return
}
go func(ctx context.Context, t *Task) {
select {
case <-ctx.Done():
return
default:
if time.Now().Second()%2 == 0 && t.MessageTextLast != stat {
t.Send(tgbotapi.NewEditMessageText(t.Message.Chat.ID, t.MessageEditID, stat))
t.MessageTextLast = stat
}
}
}(ctx, o.Task)
}
time.Sleep(time.Second)
}
}(ctx, fileChosen)
o.Task.Torrent.Process.AllowDataDownload()
timeStartToWork := time.Now().Unix()
maxTimeWork := int64(1800)
var sizeCheck int
for {
if time.Now().Unix() > timeStartToWork+maxTimeWork {
break
}
if _, bo := o.Task.App.ChatsWork.StopTasks.Load(o.Task.Message.Chat.ID); bo {
break
}
if fileChosen.FileInfo().Length == fileChosen.BytesCompleted() {
break
}
if sizeCheck > 60 && fileChosen.BytesCompleted() == 0 {
maxTimeWork = 0
break
}
sizeCheck++
time.Sleep(time.Second)
}
cancelProgress()
if time.Now().Unix() > timeStartToWork+maxTimeWork {
o.Task.Send(tgbotapi.NewMessage(o.Task.Message.Chat.ID,
fmt.Sprintf("😔 "+o.Task.Lang("Didn't have time to download, maximum 30 minutes or speed is low"))))
o.Task.App.SendLogToChannel(o.Task.Message.From, "mess",
"didn't have time to download torrent")
o.Task.Torrent.Process.Drop()
return false
}
if _, bo := o.Task.App.ChatsWork.StopTasks.Load(o.Task.Message.Chat.ID); bo {
o.Task.Torrent.Process.Drop()
return false
}
o.Task.Send(tgbotapi.NewEditMessageText(o.Task.Message.Chat.ID, o.Task.MessageEditID,
"✅ "+o.Task.Lang("Torrent downloaded, wait next step")))
pathway := path.Clean(config.DirBot + "/torrent-client/" + fileChosen.Path())
cache := Cache{Task: o.Task}
if cache.TrySend("video", pathway) {
return false
}
if cache.TrySendThroughMd5(pathway) {
return false
}
if cache.TrySend("doc", o.Task.Torrent.Name+".torrent") {
return false
}
o.Task.File = pathway
return true
}
func (o *ObjectTorrent) Convert() bool {
var c = Convert{Task: o.Task, IsTorrent: true}
if c.Task.IsAllowFormatForConvert(c.Task.File) {
o.Task.FileConverted = c.Run()
return true
} else {
return false
}
}
func (o *ObjectTorrent) Send() bool {
if path.Ext(o.Task.File) == ".flac" ||
path.Ext(o.Task.File) == ".mp3" ||
path.Ext(o.Task.File) == ".ogg" ||
path.Ext(o.Task.File) == ".wav" {
o.Task.Files = []string{o.Task.File}
return o.Task.SendAudio()
}
if path.Ext(o.Task.FileConverted.FilePath) == ".mp4" {
return o.Task.SendVideo(true)
}
//if o.Task.UserFromDB.Premium == 0 {
// o.Task.Send(tgbotapi.NewMessage(o.Task.Message.Chat.ID,
// fmt.Sprintf("❗️ "+o.Task.Lang("Available only for users who support us"))))
//}
return o.Task.SendDoc()
}
func (o *ObjectTorrent) Clean() {
o.Task.RemoveMessageEdit()
}