-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
204 lines (169 loc) · 3.79 KB
/
bot.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
package main
import (
"strings"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
uuid "github.com/satori/go.uuid"
"go.uber.org/zap"
)
type request struct {
Data *RequestData
Ray string
}
type result struct {
err error
req request
}
func UniqArtists(artists []string) []string {
used := map[string]bool{}
res := []string{}
for _, a := range artists {
la := strings.ToLower(a)
if used[la] {
continue
}
used[la] = true
res = append(res, a)
}
return res
}
func ExtractAudioInfo(meta *VideoMeta) (string, string) {
artists := strings.Split(meta.Artist, ", ")
if len(artists) == 0 {
artists = append(artists, meta.Channel)
}
artist := strings.Join(UniqArtists(artists), ", ")
track := meta.Track
if len(track) == 0 {
track = meta.Title
}
return artist, track
}
func ProxyVideo(bot *tg.BotAPI, req request, done chan<- result) {
url, err := BuildURL(req.Data.Origin, req.Data.VideoID)
if err != nil {
done <- result{
req: req,
err: err,
}
return
}
metadata, err := GetMetadata(url, req.Data.Origin)
if err != nil {
done <- result{
req: req,
err: err,
}
return
}
videoReader, waitC, downloadC := DownloadVideoRoutine(VideoSource{url, req.Data.Type}, req.Data.Origin)
def := tg.NewMessage(req.Data.ChatID, "This should never happen!")
def.ReplyToMessageID = req.Data.MessageID
var message tg.Chattable = def
if req.Data.Type == VideoType {
actual := tg.NewVideo(req.Data.ChatID, &tg.FileReader{
Name: req.Data.VideoID + ".mp4",
Reader: videoReader,
})
actual.Caption = metadata.Title
actual.ReplyToMessageID = req.Data.MessageID
message = actual
} else {
actual := tg.NewAudio(req.Data.ChatID, &tg.FileReader{
Name: req.Data.VideoID + ".mp3",
Reader: videoReader,
})
actual.ReplyToMessageID = req.Data.MessageID
actual.Performer, actual.Title = ExtractAudioInfo(metadata)
message = actual
}
_, sendErr := bot.Send(message)
close(waitC)
err = <-downloadC
if err == nil {
err = sendErr
}
done <- result{
req: req,
err: err,
}
}
func RunBot(token string) error {
L.Info("Starting the bot...")
bot, err := tg.NewBotAPI(token)
if err != nil {
return err
}
u := tg.NewUpdate(0)
u.Timeout = 60
updatesC := bot.GetUpdatesChan(u)
resultsC := make(chan result)
handleRequest := func(reqData *RequestData) {
ray := uuid.NewV4().String()
req := request{
Data: reqData,
Ray: ray,
}
if err := AddReq(reqData); err != nil {
go func() {
resultsC <- result{
req: req,
err: err,
}
}()
return
}
L.Info(
"Handle new request",
zap.String("ray", ray),
zap.String("video_id", reqData.VideoID),
zap.Int64("user_id", reqData.UserID),
zap.Int64("chat_id", reqData.ChatID),
)
go ProxyVideo(bot, req, resultsC)
}
for req := range ListReqs() {
handleRequest(req)
}
L.Info("Bot is online!")
for {
select {
case update := <-updatesC:
L.Info("New message")
if update.Message == nil {
L.Info("But no message")
break
}
for _, src := range GetUniqSources(update.Message.Text) {
handleRequest(&RequestData{
ChatID: update.Message.Chat.ID,
UserID: update.SentFrom().ID,
MessageID: update.Message.MessageID,
VideoID: src.Id,
Type: src.Type,
Origin: src.Origin,
})
}
case result := <-resultsC:
L.Debug("Cleaning up the request", zap.Any("request", result.req.Data))
if err := DelReq(result.req.Data); err != nil {
L.Error(
"Failed to clean up request",
zap.String("ray", result.req.Ray),
zap.Error(result.err),
)
}
if result.err != nil {
L.Error(
"Failed to handle request",
zap.String("ray", result.req.Ray),
zap.Error(result.err),
)
} else {
L.Info(
"Finished",
zap.String("ray", result.req.Ray),
)
}
}
}
}