forked from drakenot/gramarr
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_addtv.go
334 lines (275 loc) · 8.47 KB
/
handler_addtv.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"fmt"
"strings"
"github.com/alcmoraes/gramarr/sonarr"
"path/filepath"
tb "gopkg.in/tucnak/telebot.v2"
)
func (e *Env) HandleAddTVShow(m *tb.Message) {
e.CM.StartConversation(NewAddTVShowConversation(e), m)
}
func NewAddTVShowConversation(e *Env) *AddTVShowConversation {
return &AddTVShowConversation{env: e}
}
type AddTVShowConversation struct {
currentStep Handler
TVQuery string
TVShowResults []sonarr.TVShow
folderResults []sonarr.Folder
selectedTVShow *sonarr.TVShow
selectedTVShowSeasons []sonarr.TVShowSeason
selectedQualityProfile *sonarr.Profile
selectedLanguageProfile *sonarr.Profile
selectedFolder *sonarr.Folder
env *Env
}
func (c *AddTVShowConversation) Run(m *tb.Message) {
c.currentStep = c.AskTVShow(m)
}
func (c *AddTVShowConversation) Name() string {
return "addtv"
}
func (c *AddTVShowConversation) CurrentStep() Handler {
return c.currentStep
}
func (c *AddTVShowConversation) AskTVShow(m *tb.Message) Handler {
Send(c.env.Bot, m.Sender, "What TV Show do you want to search for?")
return func(m *tb.Message) {
c.TVQuery = m.Text
TVShows, err := c.env.Sonarr.SearchTVShows(c.TVQuery)
c.TVShowResults = TVShows
// Search Service Failed
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to search TV Show.")
c.env.CM.StopConversation(c)
return
}
// No Results
if len(TVShows) == 0 {
msg := fmt.Sprintf("No TV Show found with the title '%s'", EscapeMarkdown(c.TVQuery))
Send(c.env.Bot, m.Sender, msg)
c.env.CM.StopConversation(c)
return
}
// Found some TVShows! Yay!
var msg []string
msg = append(msg, fmt.Sprintf("*Found %d TV Shows:*", len(TVShows)))
for i, TV := range TVShows {
msg = append(msg, fmt.Sprintf("%d) %s", i+1, EscapeMarkdown(TV.String())))
}
Send(c.env.Bot, m.Sender, strings.Join(msg, "\n"))
c.currentStep = c.AskPickTVShow(m)
}
}
func (c *AddTVShowConversation) AskPickTVShow(m *tb.Message) Handler {
// Send custom reply keyboard
var options []string
for _, TVShow := range c.TVShowResults {
options = append(options, fmt.Sprintf("%s", TVShow))
}
options = append(options, "/cancel")
SendKeyboardList(c.env.Bot, m.Sender, "Which one would you like to download?", options)
return func(m *tb.Message) {
// Set the selected TVShow
for i := range options {
if m.Text == options[i] {
c.selectedTVShow = &c.TVShowResults[i]
break
}
}
// Not a valid TV selection
if c.selectedTVShow == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskPickTVShow(m)
return
}
c.currentStep = c.AskPickTVShowSeason(m)
}
}
func (c *AddTVShowConversation) AskPickTVShowSeason(m *tb.Message) Handler {
// Send custom reply keyboard
var options []string
if len(c.selectedTVShowSeasons) > 0 {
options = append(options, "Nope. I'm done!")
}
for _, Season := range c.selectedTVShow.Seasons {
if len(c.selectedTVShowSeasons) > 0 {
show := true
for _, TVShowSeason := range c.selectedTVShowSeasons {
if TVShowSeason.SeasonNumber == Season.SeasonNumber {
show = false
}
}
if show {
options = append(options, fmt.Sprintf("%v", Season.SeasonNumber))
}
} else {
options = append(options, fmt.Sprintf("%v", Season.SeasonNumber))
}
}
options = append(options, "/cancel")
if len(c.selectedTVShowSeasons) > 0 {
SendKeyboardList(c.env.Bot, m.Sender, "Any other season?", options)
} else {
SendKeyboardList(c.env.Bot, m.Sender, "Which season would you like to download?", options)
}
return func(m *tb.Message) {
if m.Text == "Nope. I'm done!" {
for _, selectedTVShowSeason := range c.selectedTVShow.Seasons {
selectedTVShowSeason.Monitored = false
for _, chosenSeason := range c.selectedTVShowSeasons {
if chosenSeason.SeasonNumber == selectedTVShowSeason.SeasonNumber {
selectedTVShowSeason.Monitored = true
}
}
}
c.currentStep = c.AskPickTVShowQuality(m)
return
}
// Set the selected TV
for i := range options {
if m.Text == options[i] {
c.selectedTVShowSeasons = append(c.selectedTVShowSeasons, *c.selectedTVShow.Seasons[i])
break
}
}
// Not a valid TV selection
if c.selectedTVShowSeasons == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskPickTVShowSeason(m)
return
}
c.currentStep = c.AskPickTVShowSeason(m)
}
}
func (c *AddTVShowConversation) AskPickTVShowQuality(m *tb.Message) Handler {
profiles, err := c.env.Sonarr.GetProfile("qualityprofile")
// GetProfile Service Failed
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to get quality profiles.")
c.env.CM.StopConversation(c)
return nil
}
// Send custom reply keyboard
var options []string
for _, QualityProfile := range profiles {
options = append(options, fmt.Sprintf("%v", QualityProfile.Name))
}
options = append(options, "/cancel")
SendKeyboardList(c.env.Bot, m.Sender, "Which quality shall I look for?", options)
return func(m *tb.Message) {
// Set the selected option
for i := range options {
if m.Text == options[i] {
c.selectedQualityProfile = &profiles[i]
break
}
}
// Not a valid selection
if c.selectedQualityProfile == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskPickTVShowQuality(m)
return
}
c.currentStep = c.AskPickTVShowLanguage(m)
}
}
func (c *AddTVShowConversation) AskPickTVShowLanguage(m *tb.Message) Handler {
languages, err := c.env.Sonarr.GetProfile("languageprofile")
// GetProfile Service Failed
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to get language profiles.")
c.env.CM.StopConversation(c)
return nil
}
// Send custom reply keyboard
var options []string
for _, LanguageProfile := range languages {
options = append(options, fmt.Sprintf("%v", LanguageProfile.Name))
}
options = append(options, "/cancel")
SendKeyboardList(c.env.Bot, m.Sender, "Which language shall I look for?", options)
return func(m *tb.Message) {
// Set the selected option
for i, opt := range options {
if m.Text == opt {
c.selectedLanguageProfile = &languages[i]
break
}
}
// Not a valid selection
if c.selectedLanguageProfile == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskPickTVShowLanguage(m)
return
}
c.currentStep = c.AskFolder(m)
}
}
func (c *AddTVShowConversation) AskFolder(m *tb.Message) Handler {
folders, err := c.env.Sonarr.GetFolders()
c.folderResults = folders
// GetFolders Service Failed
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to get folders.")
c.env.CM.StopConversation(c)
return nil
}
// No Results
if len(folders) == 0 {
SendError(c.env.Bot, m.Sender, "No destination folders found.")
c.env.CM.StopConversation(c)
return nil
}
// Found folders!
// Send the results
var msg []string
msg = append(msg, fmt.Sprintf("*Found %d folders:*", len(folders)))
for i, folder := range folders {
msg = append(msg, fmt.Sprintf("%d) %s", i+1, EscapeMarkdown(filepath.Base(folder.Path))))
}
Send(c.env.Bot, m.Sender, strings.Join(msg, "\n"))
// Send the custom reply keyboard
var options []string
for _, folder := range folders {
options = append(options, fmt.Sprintf("%s", filepath.Base(folder.Path)))
}
options = append(options, "/cancel")
SendKeyboardList(c.env.Bot, m.Sender, "Which folder should it download to?", options)
return func(m *tb.Message) {
// Set the selected folder
for i, opt := range options {
if m.Text == opt {
c.selectedFolder = &c.folderResults[i]
break
}
}
// Not a valid folder selection
if c.selectedTVShow == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskFolder(m)
return
}
c.AddTVShow(m)
}
}
func (c *AddTVShowConversation) AddTVShow(m *tb.Message) {
_, err := c.env.Sonarr.AddTVShow(*c.selectedTVShow, c.selectedLanguageProfile.ID, c.selectedQualityProfile.ID, c.selectedFolder.Path)
// Failed to add TV
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to add TV.")
c.env.CM.StopConversation(c)
return
}
if c.selectedTVShow.PosterURL != "" {
photo := &tb.Photo{File: tb.FromURL(c.selectedTVShow.PosterURL)}
c.env.Bot.Send(m.Sender, photo)
}
// Notify User
Send(c.env.Bot, m.Sender, "TV Show has been added!")
// Notify Admin
adminMsg := fmt.Sprintf("%s added TV Show '%s'", DisplayName(m.Sender), EscapeMarkdown(c.selectedTVShow.String()))
SendAdmin(c.env.Bot, c.env.Users.Admins(), adminMsg)
c.env.CM.StopConversation(c)
}