-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
183 lines (142 loc) · 3.51 KB
/
main.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
package main
import (
"fmt"
"github.com/briandowns/spinner"
"github.com/gammazero/workerpool"
"github.com/mattn/go-colorable"
"github.com/spf13/cobra"
"io"
"os"
"sort"
"sync"
"time"
)
type Result struct {
Show *Show
Season *Season
TotalEpisodesCount int
DetectedEpisodesCount int
MissingEpisodesList []*Episode
}
var colors bool
var format string
var timeout int
func main() {
cmd := &cobra.Command{
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
if !isValidFormat(format) {
return fmt.Errorf("\"%s\" is not a known output format, expected one of %s", format, formats)
}
return nil
},
RunE: run,
Use: os.Args[0],
Version: getBuildInfo(),
}
cmd.PersistentFlags().BoolVar(&colors, "colors", true, "whether colors should be used in output or not")
cmd.PersistentFlags().StringVar(&format, "format", "ascii", fmt.Sprintf("preferred output format, should be one of %s", formats))
cmd.PersistentFlags().IntVar(&timeout, "timeout", 10, "timeout for all HTTP requests, in seconds")
err := cmd.Execute()
if err != nil {
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) error {
plex, err := NewPlex(args[0], args[1], time.Second*time.Duration(timeout))
if err != nil {
return err
}
wp := workerpool.New(10)
shows, err := plex.Shows()
if err != nil {
return err
}
var writer io.Writer
if colors {
writer = colorable.NewColorableStdout()
} else {
writer = colorable.NewNonColorable(os.Stdout)
}
s := spinner.New(
spinner.CharSets[14],
250*time.Millisecond,
spinner.WithWriter(os.Stderr),
spinner.WithSuffix(" Fetching metadata..."),
spinner.WithFinalMSG("Metadata fetched.\n"),
spinner.WithWriter(writer),
)
s.Start()
errorsChan := make(chan error)
resultsChan := make(chan *Result)
errors := make([]error, 0)
results := make([]*Result, 0)
var channels sync.WaitGroup
channels.Add(2)
go func() {
defer channels.Done()
for err := range errorsChan {
errors = append(errors, err)
}
}()
go func() {
defer channels.Done()
for result := range resultsChan {
results = append(results, result)
}
}()
for _, show := range shows {
show := show
wp.Submit(func() {
seasons, err := show.Seasons()
if err != nil {
errorsChan <- err
return
}
for _, season := range seasons {
episodes, err := season.Episodes()
if err != nil {
errorsChan <- err
continue
}
result := &Result{
Show: show,
Season: season,
MissingEpisodesList: make([]*Episode, 0),
}
for _, episode := range episodes {
result.TotalEpisodesCount++
if episode.HasIntroMarker() {
result.DetectedEpisodesCount++
} else {
result.MissingEpisodesList = append(result.MissingEpisodesList, episode)
}
}
resultsChan <- result
}
})
}
wp.StopWait()
close(errorsChan)
close(resultsChan)
channels.Wait()
for _, err := range errors {
return err
}
sort.Slice(results, func(i, j int) bool {
if results[i].Show.SortTitle() == results[j].Show.SortTitle() {
if results[i].Show.RatingKey() == results[j].Show.RatingKey() {
return results[i].Season.Number() < results[j].Season.Number()
}
return results[i].Show.RatingKey() < results[j].Show.RatingKey()
}
return results[i].Show.SortTitle() < results[j].Show.SortTitle()
})
s.Stop()
if format == "ascii" {
err = outputAsAscii(results)
} else if format == "csv" {
err = outputAsCsv(results)
}
return err
}