-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_command.go
241 lines (219 loc) · 6.5 KB
/
watch_command.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
// Package watch provides the watch command for watching multiple live FC2 streams.
package watch
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"os"
"os/signal"
"sync"
"syscall"
"time"
// Import the pprof package to enable profiling via HTTP.
_ "net/http/pprof"
"github.com/Darkness4/fc2-live-dl-go/cookie"
"github.com/Darkness4/fc2-live-dl-go/fc2"
"github.com/Darkness4/fc2-live-dl-go/fc2/cleaner"
"github.com/Darkness4/fc2-live-dl-go/notify"
"github.com/Darkness4/fc2-live-dl-go/notify/notifier"
"github.com/Darkness4/fc2-live-dl-go/state"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var (
configPath string
pprofListenAddress string
)
// Command is the command for watching multiple live FC2 streams.
var Command = &cli.Command{
Name: "watch",
Usage: "Automatically download multiple Live FC2 streams.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Required: true,
Usage: `Config file path. (required)`,
Destination: &configPath,
},
&cli.StringFlag{
Name: "pprof.listen-address",
Value: ":3000",
Destination: &pprofListenAddress,
},
},
Action: func(cCtx *cli.Context) error {
ctx, cancel := context.WithCancel(cCtx.Context)
// Trap cleanup
cleanChan := make(chan os.Signal, 1)
signal.Notify(cleanChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-cleanChan
cancel()
}()
configChan := make(chan *Config)
go ObserveConfig(ctx, configPath, configChan)
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
s := state.DefaultState.ReadState()
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Info().Str("listenAddress", pprofListenAddress).Msg("listening")
if err := http.ListenAndServe(pprofListenAddress, nil); err != nil {
log.Fatal().Err(err).Msg("fail to serve http")
}
log.Fatal().Msg("http server stopped")
}()
return ConfigReloader(ctx, configChan, handleConfig)
},
}
func handleConfig(ctx context.Context, config *Config) {
jar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
log.Panic().Err(err).Msg("failed to initialize cookie jar")
}
params := fc2.DefaultParams.Clone()
config.DefaultParams.Override(params)
if params.CookiesFile != "" {
if err := cookie.ParseFromFile(jar, params.CookiesFile); err != nil {
log.Error().Err(err).Msg("failed to load cookies, using unauthenticated")
}
}
client := &http.Client{Jar: jar, Timeout: time.Minute}
if params.CookiesRefreshDuration != 0 && params.CookiesFile != "" {
log.Info().Dur("duration", params.CookiesRefreshDuration).Msg("will refresh cookies")
if err := fc2.Login(ctx, fc2.WithHTTPClient(client)); err != nil {
if err := notifier.NotifyLoginFailed(ctx, err); err != nil {
log.Err(err).Msg("notify failed")
}
log.Err(err).
Msg("failed to login to id.fc2.com, we will try again, but you should extract new cookies")
}
go fc2.LoginLoop(ctx, params.CookiesRefreshDuration, fc2.WithHTTPClient(client))
} else {
log.Info().Msg("cookies refresh duration is zero, will not refresh cookies")
}
if config.Notifier.Enabled {
notifier.Notifier = notify.NewFormatedNotifier(
notify.NewShoutrrr(
config.Notifier.URLs,
notify.IncludeTitleInMessage(config.Notifier.IncludeTitleInMessage),
notify.NoPriority(config.Notifier.NoPriority),
),
config.Notifier.NotificationFormats,
)
log.Info().Msg("using shoutrrr")
if len(config.Notifier.URLs) == 0 {
log.Warn().Msg("using shoutrrr but there is no URLs")
}
} else {
log.Info().Msg("no notifier configured")
}
if err := notifier.NotifyConfigReloaded(ctx); err != nil {
log.Err(err).Msg("notify failed")
}
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
if err := notifier.NotifyPanicked(ctx, err); err != nil {
log.Err(err).Msg("notify failed")
}
os.Exit(1)
}
}()
var wg sync.WaitGroup
wg.Add(len(config.Channels))
for channel, overrideParams := range config.Channels {
channelParams := params.Clone()
overrideParams.Override(channelParams)
// Scan for intermediates .ts used for concatenation
if !channelParams.KeepIntermediates && channelParams.Concat &&
channelParams.ScanDirectory != "" {
wg.Add(1)
go func(params *fc2.Params) {
defer wg.Done()
cleaner.CleanPeriodically(
ctx,
params.ScanDirectory,
time.Hour,
cleaner.WithEligibleAge(params.EligibleForCleaningAge),
)
}(channelParams)
}
go func(channelID string, params *fc2.Params) {
defer wg.Done()
log := log.With().Str("channelID", channelID).Logger()
for {
state.DefaultState.SetChannelState(channelID, state.DownloadStateIdle, nil)
if err := notifier.NotifyIdle(ctx, channelID, params.Labels); err != nil {
log.Err(err).Msg("notify failed")
}
meta, err := handleChannel(ctx, client, channelID, params)
if errors.Is(err, context.Canceled) {
log.Info().Msg("abort watching channel")
if state.DefaultState.GetChannelState(
channelID,
) != state.DownloadStateIdle {
state.DefaultState.SetChannelState(
channelID,
state.DownloadStateCanceled,
nil,
)
if err := notifier.NotifyCanceled(
context.Background(),
channelID,
params.Labels,
); err != nil {
log.Err(err).Msg("notify failed")
}
}
return
} else if err != nil {
log.Error().Err(err).Msg("failed to download")
state.DefaultState.SetChannelError(channelID, err)
if err := notifier.NotifyError(
context.Background(),
channelID,
params.Labels,
err,
); err != nil {
log.Err(err).Msg("notify failed")
}
} else {
state.DefaultState.SetChannelState(channelID, state.DownloadStateFinished, nil)
if err := notifier.NotifyFinished(ctx, channelID, params.Labels, meta); err != nil {
log.Err(err).Msg("notify failed")
}
}
time.Sleep(time.Second)
}
}(channel, channelParams)
}
wg.Wait()
}
func handleChannel(
ctx context.Context,
client *http.Client,
channelID string,
params *fc2.Params,
) (*fc2.GetMetaData, error) {
downloader := fc2.New(client, params, channelID)
meta, err := downloader.Watch(ctx)
if err != nil && err != io.EOF {
return nil, err
}
return meta, nil
}