-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
297 lines (228 loc) · 6.77 KB
/
commands.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
package main
import (
"fmt"
"time"
"github.com/davidderus/christopher/config"
"github.com/davidderus/christopher/dispatcher"
"github.com/davidderus/christopher/feedwatcher"
"github.com/davidderus/christopher/teller"
"github.com/davidderus/christopher/webserver"
"github.com/urfave/cli"
)
var (
appConfig *config.Config
appTeller *teller.Teller
)
func loadRequirements() error {
configError := loadConfig()
if configError != nil {
return configError
}
loadTeller()
return nil
}
func loadConfig() error {
config, configError := config.LoadFromFile(appConfigPath)
if configError != nil {
return configError
}
appConfig = config
return nil
}
func loadTeller() {
appTeller = teller.NewTeller(appConfig.Teller.LogLevel, appConfig.Teller.LogFormatter)
}
//////////////
// Debrider //
//////////////
// DebriderCli defines the cli args for the debrider
var DebriderCli = cli.Command{
Name: "debrid",
Aliases: []string{"de"},
Usage: "Debrids an URI",
Description: "Sends an URI to the debrider and return a debrided URI.",
Action: runDebrider,
ArgsUsage: "<URI>",
}
func runDebrider(ctx *cli.Context) error {
// Loading command requirements
loadError := loadRequirements()
if loadError != nil {
return cli.NewExitError(loadError.Error(), 1)
}
uri := ctx.Args().First()
if uri == "" {
appTeller.Log().Fatalln("No URI given")
}
event := &dispatcher.Event{Origin: "cli", Value: uri}
story := &dispatcher.ChristopherStory{}
story.SetConfig(appConfig).EnableDebrider()
story.SetTeller(appTeller)
scenario := story.Scenario()
scenario.SetInitialStep("config")
scenario.Play(event)
runError := scenario.RunError()
if runError != nil {
appTeller.Log().Fatalln(runError)
}
fmt.Print(event.Value)
return nil
}
////////////////
// Downloader //
////////////////
// DownloaderCli defines the cli args for the downloader
var DownloaderCli = cli.Command{
Name: "download",
Aliases: []string{"do"},
Usage: "Sends an URI to the downloader",
Description: "Sends a URI to the downloader set in the config file.",
Action: runDownloader,
ArgsUsage: "<URI>",
}
func runDownloader(ctx *cli.Context) error {
// Loading command requirements
loadError := loadRequirements()
if loadError != nil {
return cli.NewExitError(loadError.Error(), 1)
}
uri := ctx.Args().First()
if uri == "" {
appTeller.Log().Fatalln("No URI given")
}
event := &dispatcher.Event{Origin: "cli", Value: uri}
// story without debrider
story := &dispatcher.ChristopherStory{}
story.SetConfig(appConfig).EnableDownloader()
story.SetTeller(appTeller)
// Setting a custom notifier
story.SetNotifier(func(event *dispatcher.Event) error {
appTeller.LogWithFields(map[string]interface{}{
"downloadURI": event.Value,
"downloadHandler": appConfig.Downloader.Name,
}).Infoln("URI sent to downloader")
return nil
})
scenario := story.Scenario()
scenario.SetInitialStep("config")
scenario.Play(event)
runError := scenario.RunError()
if runError != nil {
appTeller.Log().Fatalln(runError)
}
return nil
}
/////////////////////////
// Download and Debrid //
/////////////////////////
// DownloadAndDebridCli defines the cli args for a combination of the debrider and the downloader
var DownloadAndDebridCli = cli.Command{
Name: "debrid-download",
Action: downloadAndDebrid,
Aliases: []string{"dedo"},
Usage: "Debrids and downloads an URI",
Description: "If debrider is able to handle the URI, it will be debrided and then sent to downloader. Otherwise, it will only be downloaded.",
ArgsUsage: "<URI>",
}
func downloadAndDebrid(ctx *cli.Context) error {
// Loading command requirements
loadError := loadRequirements()
if loadError != nil {
return cli.NewExitError(loadError.Error(), 1)
}
uri := ctx.Args().First()
if uri == "" {
appTeller.Log().Fatalln("No URI given")
}
event := &dispatcher.Event{Origin: "cli", Value: uri}
story := &dispatcher.ChristopherStory{}
story.SetConfig(appConfig).EnableDebrider().EnableDownloader()
story.SetTeller(appTeller)
scenario := story.Scenario()
scenario.SetInitialStep("config")
scenario.Play(event)
runError := scenario.RunError()
if runError != nil {
appTeller.Log().Fatalln(runError)
}
return nil
}
/////////////////
// FeedWatcher //
/////////////////
// FeedWatcherCli defines the cli args for the feed watcher
var FeedWatcherCli = cli.Command{
Name: "feed-watcher",
Aliases: []string{"fw"},
Usage: "Starts the feed watcher",
Description: "Watch the feeds defined in configuration and send all links to the right service.",
Action: runFeedWatcher,
}
func runFeedWatcher(ctx *cli.Context) error {
// Loading command requirements
loadError := loadRequirements()
if loadError != nil {
return cli.NewExitError(loadError.Error(), 1)
}
// Building FeedWatcher from config
feedWatcherConfig := appConfig.FeedWatcher
watchInterval := time.Duration(feedWatcherConfig.WatchInterval) * time.Minute
feedWatcher, feedWatcherError := feedwatcher.NewFeedWatcher(watchInterval)
if feedWatcherError != nil {
appTeller.Log().Fatalln(feedWatcherError)
}
// Adding Teller
feedWatcher.SetTeller(appTeller)
// Getting feeds
configFeeds := feedWatcherConfig.Feeds
feedWatcherFeeds := make([]feedwatcher.RemoteFeed, len(configFeeds))
for feedIndex, feed := range configFeeds {
feedProvider := feed.Provider
providerOptions := appConfig.Providers[feedProvider]
feedWatcherFeeds[feedIndex] = feedwatcher.RemoteFeed{
Title: feed.Title,
URL: feed.URL,
Provider: feedProvider,
ProviderOptions: providerOptions,
}
}
feedWatcher.Feeds = feedWatcherFeeds
// Artificial SinceDate for now
feedWatcher.SinceDate = time.Now()
// Using default story to process new links
story := &dispatcher.ChristopherStory{}
story.SetConfig(appConfig).EnableDebrider().EnableDownloader()
story.SetTeller(appTeller)
scenario := story.Scenario()
scenario.SetInitialStep("config")
feedWatcher.Scenario = scenario
// Running FeedWatcher for eternity
runSummary, runError := feedWatcher.Run(0)
// Handling run errors
if runError != nil {
appTeller.Log().Fatalln(runError)
}
// Logging output if any (will never be reached on Run(0))
appTeller.Log().Infoln(runSummary)
return nil
}
///////////////
// WebServer //
///////////////
// WebServerCli defines the cli args for the feed watcher
var WebServerCli = cli.Command{
Name: "webserver",
Aliases: []string{"ws"},
Usage: "Starts the webserver",
Action: runWebServer,
}
func runWebServer(ctx *cli.Context) error {
// Loading command requirements
loadError := loadRequirements()
if loadError != nil {
return cli.NewExitError(loadError.Error(), 1)
}
webServer := webserver.NewWebServer(appConfig, appTeller)
appTeller.Log().Fatalln(webServer.Start())
return nil
}