/
handler_addmovie.go
230 lines (185 loc) · 5.62 KB
/
handler_addmovie.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
package main
import (
"fmt"
"strings"
"github.com/uberjew666/gramarr/radarr"
"path/filepath"
tb "gopkg.in/tucnak/telebot.v2"
)
func (e *Env) HandleAddMovie(m *tb.Message) {
e.CM.StartConversation(NewAddMovieConversation(e), m)
}
func NewAddMovieConversation(e *Env) *AddMovieConversation {
return &AddMovieConversation{env: e}
}
type AddMovieConversation struct {
currentStep Handler
movieQuery string
movieResults []radarr.Movie
folderResults []radarr.Folder
selectedMovie *radarr.Movie
selectedQualityProfile *radarr.Profile
selectedFolder *radarr.Folder
env *Env
}
func (c *AddMovieConversation) Run(m *tb.Message) {
c.currentStep = c.AskMovie(m)
}
func (c *AddMovieConversation) Name() string {
return "addmovie"
}
func (c *AddMovieConversation) CurrentStep() Handler {
return c.currentStep
}
func (c *AddMovieConversation) AskMovie(m *tb.Message) Handler {
Send(c.env.Bot, m.Sender, "What movie do you want to search for?")
return func(m *tb.Message) {
c.movieQuery = m.Text
movies, err := c.env.Radarr.SearchMovies(c.movieQuery)
c.movieResults = movies
// Search Service Failed
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to search movies.")
c.env.CM.StopConversation(c)
return
}
// No Results
if len(movies) == 0 {
msg := fmt.Sprintf("No movie found with the title '%s'", EscapeMarkdown(c.movieQuery))
Send(c.env.Bot, m.Sender, msg)
c.env.CM.StopConversation(c)
return
}
// Found some movies! Yay!
var msg []string
msg = append(msg, fmt.Sprintf("*Found %d movies:*", len(movies)))
for i, movie := range movies {
msg = append(msg, fmt.Sprintf("%d) %s", i+1, EscapeMarkdown(movie.String())))
}
Send(c.env.Bot, m.Sender, strings.Join(msg, "\n"))
c.currentStep = c.AskPickMovie(m)
}
}
func (c *AddMovieConversation) AskPickMovie(m *tb.Message) Handler {
// Send custom reply keyboard
var options []string
for _, movie := range c.movieResults {
options = append(options, fmt.Sprintf("%s", movie))
}
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 movie
for i, opt := range options {
if m.Text == opt {
c.selectedMovie = &c.movieResults[i]
break
}
}
// Not a valid movie selection
if c.selectedMovie == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskPickMovie(m)
return
}
c.currentStep = c.AskPickMovieQuality(m)
}
}
func (c *AddMovieConversation) AskPickMovieQuality(m *tb.Message) Handler {
profiles, err := c.env.Radarr.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.AskPickMovieQuality(m)
return
}
c.currentStep = c.AskFolder(m)
}
}
func (c *AddMovieConversation) AskFolder(m *tb.Message) Handler {
folders, err := c.env.Radarr.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.selectedMovie == nil {
SendError(c.env.Bot, m.Sender, "Invalid selection.")
c.currentStep = c.AskFolder(m)
return
}
c.AddMovie(m)
}
}
func (c *AddMovieConversation) AddMovie(m *tb.Message) {
_, err := c.env.Radarr.AddMovie(*c.selectedMovie, c.selectedQualityProfile.ID, c.selectedFolder.Path)
// Failed to add movie
if err != nil {
SendError(c.env.Bot, m.Sender, "Failed to add movie.")
c.env.CM.StopConversation(c)
return
}
if c.selectedMovie.PosterURL != "" {
photo := &tb.Photo{File: tb.FromURL(c.selectedMovie.PosterURL)}
c.env.Bot.Send(m.Sender, photo)
}
// Notify User
Send(c.env.Bot, m.Sender, "Movie has been added!")
// Notify Admin
adminMsg := fmt.Sprintf("%s added movie '%s'", DisplayName(m.Sender), EscapeMarkdown(c.selectedMovie.String()))
SendAdmin(c.env.Bot, c.env.Users.Admins(), adminMsg)
c.env.CM.StopConversation(c)
}