-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_streams.go
341 lines (285 loc) · 9.59 KB
/
cmd_streams.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
335
336
337
338
339
340
341
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/pboehm/series/config"
idx "github.com/pboehm/series/index"
str "github.com/pboehm/series/streams"
"github.com/spf13/cobra"
"io"
"io/ioutil"
"os"
)
var streamsCmdJsonOutput = false
var streamsCmd = &cobra.Command{
Use: "streams",
Short: "Manage str that are interesting according to the index",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
var streamsUnknownSeriesCmd = &cobra.Command{
Use: "unknown",
Short: "list all series which are unknown by the streaming site",
Run: func(cmd *cobra.Command, args []string) {
withIndexStreamsAndWatchedSeries(func(index *idx.SeriesIndex, streams *str.Streams, watched []str.WatchedSeries) {
existingSeries := map[string]idx.Series{}
for _, series := range index.SeriesList {
existingSeries[series.Name] = series
}
for _, series := range watched {
delete(existingSeries, series.SeriesNameInIndex)
}
for _, series := range existingSeries {
fmt.Println(series.Name)
}
})
},
}
var streamsFetchLinksCmd = &cobra.Command{
Use: "links",
Short: "fetch Links for unwatched episodes of series",
Run: func(cmd *cobra.Command, args []string) {
withIndexStreamsAndWatchedSeries(func(index *idx.SeriesIndex, streams *str.Streams, watched []str.WatchedSeries) {
linkSet := str.NewLinkSet(appConfig, streams, index)
linkSet.GrabLinksFor(watched)
if streamsCmdJsonOutput {
entries := linkSet.Entries()
bytes, err := json.MarshalIndent(entries, "", " ")
HandleError(err)
fmt.Println(string(bytes))
} else {
grouped := linkSet.GroupedEntries()
for group, groupedEntries := range grouped {
fmt.Printf(">>>> %s\n", group)
for _, entry := range groupedEntries {
fmt.Printf(">> %s [%s]\n", entry.Filename, entry.Id)
for i, link := range entry.Links {
if i >= 2 {
break
}
fmt.Printf(" %s\t [%s]\n", link.Link, link.Hoster)
}
}
}
}
})
},
}
func markEpisodeAsWatched(index *idx.SeriesIndex, episodeId string) (*str.Identifier, error) {
var err error
id, err := str.IdentifierFromString(episodeId)
if err != nil {
return nil, err
}
filename := fmt.Sprintf("S%02dE%02d - Episode %d.mov", id.Season, id.Episode, id.Episode)
_, err = index.AddEpisodeManually(id.Series, id.Language, id.Season, id.Episode, filename)
return id, err
}
var streamsMarkWatchedCmd = &cobra.Command{
Use: "mark-watched [id, ....]",
Short: "mark links as watched",
Run: func(cmd *cobra.Command, args []string) {
callPreProcessingHook()
loadIndex()
for _, arg := range args {
id, err := markEpisodeAsWatched(seriesIndex, arg)
if id == nil && err != nil {
HandleError(err)
}
seasonWithEpisode := fmt.Sprintf("S%02dE%02d", id.Season, id.Episode)
if err == nil {
LOG.Printf("Marking %s of %s [%s] as watched\n", seasonWithEpisode, id.Series, id.Language)
} else {
LOG.Printf("Could not mark %s of %s [%s] as watched: %s\n", seasonWithEpisode, id.Series, id.Language, err)
}
}
writeIndex()
callPostProcessingHook()
},
}
var streamsRunActionCmd = &cobra.Command{
Use: "run-action action linkId",
Short: "run link actions on the supplied link ids",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Help()
return
}
actionId := args[0]
linkIdString := args[1]
var action = config.StreamAction{}
for _, a := range appConfig.StreamsLinkActions {
if a.Id == actionId {
action = a
}
}
if action.Id == "" {
HandleError(errors.New(fmt.Sprintf("action '%s' not found", actionId)))
}
withIndexStreamsAndWatchedSeries(func(index *idx.SeriesIndex, streams *str.Streams, watched []str.WatchedSeries) {
identifier, linkId, err := str.LinkIdentifierFromString(linkIdString)
HandleError(err)
HandleError(runAction(os.Stderr, streams, action, identifier, linkId))
})
},
}
func runAction(output io.Writer, streams *str.Streams, action config.StreamAction, id *str.Identifier, linkId int) error {
var err error
var session, videoUrl string
if session, err = streams.Login(appConfig.StreamsAccountEmail, appConfig.StreamsAccountPassword); err != nil {
return err
}
if videoUrl, err = streams.ResolveLink(linkId, session); err != nil {
return err
}
return SystemV(action.Command, []string{
fmt.Sprintf("SERIES_SERIES=%s", id.Series),
fmt.Sprintf("SERIES_SERIES_SLUG=%s", id.SeriesSlug),
fmt.Sprintf("SERIES_SERIES_ID=%d", id.SeriesId),
fmt.Sprintf("SERIES_LANGUAGE=%s", id.Language),
fmt.Sprintf("SERIES_SEASON=%d", id.Season),
fmt.Sprintf("SERIES_EPISODE=%d", id.Episode),
fmt.Sprintf("SERIES_EPISODE_NAME=%s", id.EpisodeName),
fmt.Sprintf("SERIES_FILENAME=S%02dE%02d - %s.mov", id.Season, id.Episode, id.EpisodeName),
fmt.Sprintf("SERIES_LINK_ID=%d", linkId),
fmt.Sprintf("SERIES_REDIRECT_URL=%s", streams.LinkUrl(linkId)),
fmt.Sprintf("SERIES_VIDEO_URL=%s", videoUrl),
fmt.Sprintf("SERIES_SESSION=%s", session),
fmt.Sprintf("SERIES_API_TOKEN=%s", streams.Config.StreamsAPIToken),
fmt.Sprintf("SERIES_CONFIG_FILE=%s", configFile),
}, output, output)
}
var streamsServerOptionListen string
var streamsServerOptionIndexHtml string
var streamsServerCmd = &cobra.Command{
Use: "server",
Short: "run an HTTP server serving an API and a frontend for streams",
Run: func(cmd *cobra.Command, args []string) {
indexHtmlContent := func() []byte {
if streamsServerOptionIndexHtml != "" {
indexFileBytes, err := ioutil.ReadFile(streamsServerOptionIndexHtml)
if err != nil {
HandleError(err)
}
return indexFileBytes
} else {
return []byte(str.ServerStaticHtml)
}
}
var currentLinkSet *str.LinkSet
var currentStreams *str.Streams
loadLinkSet := func() {
withIndexStreamsAndWatchedSeries(func(index *idx.SeriesIndex, streams *str.Streams, watched []str.WatchedSeries) {
linkSet := str.NewLinkSet(appConfig, streams, index)
linkSet.GrabLinksFor(watched)
currentLinkSet = linkSet
currentStreams = streams
})
}
go loadLinkSet()
api := str.API{
Config: appConfig,
Jobs: str.NewJobPool(),
HtmlContent: indexHtmlContent,
LinkSetRefresh: loadLinkSet,
LinkSet: func() *str.LinkSet {
return currentLinkSet
},
MarkWatched: func(episodeIds []string) ([]string, []string) {
var successes, failures []string
callPreProcessingHook()
loadIndex()
for _, episodeId := range episodeIds {
_, err := markEpisodeAsWatched(seriesIndex, episodeId)
if err == nil {
successes = append(successes, episodeId)
} else {
failures = append(failures, episodeId)
}
}
writeIndex()
callPostProcessingHook()
loadLinkSet()
return successes, failures
},
ExecuteLinkAction: func(action config.StreamAction, identifier *str.Identifier, i int) *str.Job {
return str.NewJob(func(output io.Writer) error {
multiWriter := io.MultiWriter(output, os.Stderr)
if currentStreams == nil {
return errors.New("streams not initialized")
}
return runAction(multiWriter, currentStreams, action, identifier, i)
})
},
ExecuteGlobalAction: func(action config.StreamAction) *str.Job {
return str.NewJob(func(output io.Writer) error {
multiWriter := io.MultiWriter(output, os.Stderr)
return SystemV(action.Command, []string{}, multiWriter, multiWriter)
})
},
ResolveLink: func(linkId int) (string, error) {
var session, videoUrl string
var err error
if currentStreams == nil {
return "", errors.New("streams not initialized")
}
if session, err = currentStreams.Login(appConfig.StreamsAccountEmail, appConfig.StreamsAccountPassword); err != nil {
return "", err
}
if videoUrl, err = currentStreams.ResolveLink(linkId, session); err != nil {
return "", err
}
return videoUrl, nil
},
}
HandleError(api.Run(streamsServerOptionListen))
},
}
func withIndexStreamsAndWatchedSeries(handler func(*idx.SeriesIndex, *str.Streams, []str.WatchedSeries)) {
if appConfig.StreamsAPIToken == "" {
HandleError(errors.New(fmt.Sprintf("`StreamsAPIToken` not configured in %s", configFile)))
}
if appConfig.StreamsAccountEmail == "" || appConfig.StreamsAccountPassword == "" {
HandleError(errors.New(fmt.Sprintf(
"`StreamsAccountEmail` or `StreamsAccountPassword` is not configured in %s", configFile)))
}
callPreProcessingHook()
loadIndex()
streams := str.NewStreams(appConfig)
availableSeries := streams.AvailableSeries()
var watched []str.WatchedSeries
for _, series := range availableSeries {
nameInIndex := seriesIndex.SeriesNameInIndex(series.Name)
if nameInIndex != "" {
languages := seriesIndex.SeriesLanguages(nameInIndex)
watched = append(watched, str.WatchedSeries{
Series: series,
SeriesNameInIndex: nameInIndex,
SeriesLanguages: mapLanguagesToIds(languages),
})
}
}
handler(seriesIndex, streams, watched)
}
func mapLanguagesToIds(languages []string) map[string]int {
mapped := map[string]int{}
for _, language := range languages {
switch language {
case "de":
mapped[language] = 1
case "en":
mapped[language] = 2
default:
continue
}
}
return mapped
}
func init() {
streamsFetchLinksCmd.Flags().BoolVarP(&streamsCmdJsonOutput, "json", "j", false, "output as JSON")
streamsServerCmd.Flags().StringVarP(&streamsServerOptionListen, "listen", "l", ":8080", "where should the server listen")
streamsServerCmd.Flags().StringVarP(&streamsServerOptionIndexHtml, "index", "i", "", "a custom index.html that should be used")
streamsCmd.AddCommand(streamsUnknownSeriesCmd, streamsFetchLinksCmd, streamsMarkWatchedCmd, streamsRunActionCmd, streamsServerCmd)
}