/
movie.go
230 lines (152 loc) · 4.6 KB
/
movie.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 (
"encoding/json"
"fmt"
"log"
"gitlab.com/haath/goirate/pkg/movies"
"gitlab.com/haath/goirate/pkg/torrents"
)
// MovieCommand defines the movie command and holds its options.
type MovieCommand struct {
torrentSearchArgs
Year uint `short:"y" long:"year" description:"The release year of the movie. Used when searching for the movie by title instead of by IMDbID."`
Download bool `short:"d" long:"download" description:"Send the movie to the qBittorret client for download using the RPC configuration."`
NoTorrent bool `long:"no-torrent" description:"Do not search for torrents."`
Args moviePositionalArgs `positional-args:"1" required:"1"`
}
type moviePositionalArgs struct {
Query string `positional-arg-name:"<imdb url | imdbID | title>"`
}
// Execute is the callback of the movie command.
func (m *MovieCommand) Execute(args []string) error {
movie, err := m.getMovie()
if err != nil {
return err
}
var perQualityTorrents []torrents.Torrent
var topTorrent *torrents.Torrent
if !m.NoTorrent {
perQualityTorrents, err = movie.GetTorrents(*m.GetFilters())
if len(perQualityTorrents) > 0 {
topTorrent, err = torrents.PickVideoTorrent(perQualityTorrents, *m.GetFilters())
if err != nil {
return err
}
if m.Download && topTorrent != nil {
// Send the torrent to the qBittorrent daemon for download
err = m.downloadMovieTorrent(movie, topTorrent)
if err != nil {
return err
}
}
}
}
if Options.JSON {
movieObj := struct {
movies.Movie
Torrents []torrents.Torrent `json:"torrents"`
}{
*movie,
perQualityTorrents,
}
movieJSON, err := json.MarshalIndent(movieObj, "", " ")
if err != nil {
return err
}
log.Println(string(movieJSON))
} else {
if m.MagnetLink && !m.NoTorrent {
if topTorrent == nil {
return fmt.Errorf("no torrent found for: %v", movie.Title)
}
log.Println(topTorrent.Magnet)
} else if m.TorrentURL && !m.NoTorrent {
if topTorrent == nil {
return fmt.Errorf("no torrent found for: %v", movie.Title)
}
log.Println(topTorrent.FullURL())
} else {
log.Printf("%v\n", movie.Title)
if movie.AltTitle != "" {
log.Printf("Orig. Title:\t%v\n", movie.AltTitle)
}
log.Printf("IMDbID:\t\t%v\n", movie.IMDbID)
log.Printf("Year:\t\t%v\n", movie.Year)
log.Printf("Rating:\t\t%v\n", movie.Rating)
log.Printf("Genres:\t\t%v\n", movie.GetGenresString())
if movie.FormattedDuration() != "" {
log.Printf("Duration:\t%v\n", movie.FormattedDuration())
}
if movie.PosterURL != "" {
log.Printf("Poster:\t\t%v\n", movie.PosterURL)
}
log.Println("")
if !m.NoTorrent {
if topTorrent == nil {
log.Println("No torrent found")
} else {
log.Println(topTorrent.Title)
log.Printf("URL:\t\t%v\n", topTorrent.FullURL())
log.Printf("Seeds/Peers:\t%v\n", topTorrent.PeersString())
log.Printf("Size:\t\t%v\n", topTorrent.SizeString())
log.Printf("Trusted:\t%v\n", topTorrent.VerifiedUploader)
log.Printf("Magnet:\n%v\n", topTorrent.Magnet)
}
}
}
}
return nil
}
func (m *MovieCommand) getMovie() (*movies.Movie, error) {
var err error
var imdbID string
omdb := Config.OMDBCredentials
if movies.IsIMDbID(m.Args.Query) {
imdbID, err = movies.FormatIMDbID(m.Args.Query)
} else if movies.IsIMDbURL(m.Args.Query) {
imdbID, err = movies.ExtractIMDbID(m.Args.Query)
} else {
imdbID, err = m.searchMovie()
}
if err != nil {
return nil, err
}
var movie *movies.Movie
if omdb.IsEnabled() {
movie, err = omdb.GetMovie(imdbID)
} else {
// OMDb API key not configured, fall back to IMDb.
movie, err = movies.GetMovie(imdbID)
}
return movie, err
}
func (m *MovieCommand) searchMovie() (string, error) {
var searchResults []movies.MovieID
var err error
omdb := Config.OMDBCredentials
if omdb.IsEnabled() {
searchResults, err = omdb.Search(m.Args.Query)
} else {
searchResults, err = movies.Search(m.Args.Query)
}
if err != nil {
return "", err
}
for _, movie := range searchResults {
if m.Year == 0 || m.Year == movie.Year {
return movie.IMDbID, nil
}
}
return "", fmt.Errorf("movie not found: %v", m.Args.Query)
}
func (m *MovieCommand) downloadMovieTorrent(movie *movies.Movie, torrent *torrents.Torrent) error {
qbt, err := Config.QBittorrentConfig.GetClient()
if err != nil {
return err
}
downloadPath := Config.DownloadDir.Movies
if !Options.JSON && !m.MagnetLink && !m.TorrentURL {
log.Printf("Downloading: %s (%s)\n", movie.Title, downloadPath)
}
return qbt.AddTorrent(torrent.Magnet, downloadPath)
}