-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
386 lines (323 loc) · 10.2 KB
/
app.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
//go:generate go run github.com/tc-hib/go-winres@latest make --in assets/winres.json --product-version ${VERSION}
//go:generate sh -c "INPUT=assets/icon22.png OUTPUT=internal/icon/icon_unix.go hack/make_icon_unix.sh"
//go:generate sh -c "INPUT=assets/icon.ico OUTPUT=internal/icon/icon_windows.go hack/make_icon_windows.sh"
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/SkYNewZ/twitch-clip/internal/config"
"github.com/SkYNewZ/twitch-clip/internal/icon"
"github.com/SkYNewZ/twitch-clip/internal/twitch"
"github.com/SkYNewZ/twitch-clip/pkg/notifier"
"github.com/SkYNewZ/twitch-clip/pkg/player"
"github.com/SkYNewZ/twitch-clip/pkg/streamlink"
"github.com/atotto/clipboard"
"github.com/emersion/go-autostart"
"github.com/getlantern/systray"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
)
var (
// These 2 variables will be overwritten at build time using ldflags
twitchClientID string
twitchClientSecret string
)
const (
AppName = "twitchclip"
AppDisplayName = "Twitch Clip"
)
// Application contains all required dependencies
type Application struct {
Name string
DisplayName string
// Main cancel function to stop the program
Cancel context.CancelFunc
// Player to use
Player player.Player
// Twitch client
Twitch *twitch.Client
Streamlink streamlink.Client
Notifier notifier.Notifier
NotificationCallbackCh <-chan string
// Carry our current displayed items
State map[string]*Item
// Each string in this chan will be send to system clipboard
ClipboardListener chan string
config *config.Config
}
// New creates a new Application
func New() *Application {
// Get media player
p, err := player.DefaultPlayer()
if err != nil {
log.Fatalln(err)
}
s, err := streamlink.New()
if err != nil {
log.Fatalln(err)
}
// Get Twitch client
twitchClient, err := twitch.New(&twitch.Config{ClientID: twitchClientID, ClientSecret: twitchClientSecret})
if err != nil {
log.Fatalln(err)
}
// Start the notifier service
n, notificationCh := notifier.New(AppDisplayName)
// Make the app and inject required dependencies
return &Application{
Name: AppName,
DisplayName: AppDisplayName,
Cancel: nil,
Player: p,
Twitch: twitchClient,
Streamlink: s,
Notifier: n,
NotificationCallbackCh: notificationCh,
State: make(map[string]*Item),
ClipboardListener: make(chan string, 1),
config: config.Parse(),
}
}
// Setup must not be called before systray.Run or systray.Register
// app := New()
// systray.Run(app.Setup, app.Stop)
func (a *Application) Setup() {
// Set icon and Application name
systray.SetIcon(icon.Data)
systray.SetTooltip(a.DisplayName)
// Manage auto start
done := make(chan struct{}, 1)
go a.autostart(done)
<-done //wait for "autostart" button displayed before continue
// This context will manage all Application routines cancellation
var ctx context.Context
ctx, a.Cancel = context.WithCancel(context.Background())
// Open Twitch website
openTwitch := systray.AddMenuItem("Open Twitch", "Open https://www.twitch.tv")
go func() {
for {
select {
case <-ctx.Done():
return
case <-openTwitch.ClickedCh:
if err := browser.OpenURL("https://www.twitch.tv"); err != nil {
log.Errorf("unable to open twitch website: %s", err)
}
}
}
}()
// Display "quit" button and listen for click
quit := systray.AddMenuItem("Quit", "Quit the whole app")
systray.AddSeparator()
go func() {
<-quit.ClickedCh
systray.Quit()
}()
// Start Application
a.Start(ctx)
}
// Start show a Item for each online streams
// This will be refresh at each streamsRefreshTime
// The passed context is used to cancel theses routines
func (a *Application) Start(ctx context.Context) {
// We permit only one array at a time
var out = make(chan []*twitch.Stream, 1)
// Listen for notification callback
go a.HandleNotificationCallback(ctx)
// start routines for refreshing streams
go a.RefreshActiveStreams(ctx, out)
// start routine to display these streams
go a.RefreshStreamsMenuItem(ctx, out)
// Listen for clipboard requests
go a.HandleClipboard(ctx)
}
// Stop Application
func (a *Application) Stop() {
a.Cancel() // stop each routine
close(a.ClipboardListener) // stop clipboard listener
if err := a.Notifier.Close(); err != nil { // notification service
log.Errorf("fail to stop notification service: %s", err)
}
}
// autostart make current Application auto start at boot and handle change on the item
func (a *Application) autostart(done chan<- struct{}) {
executable, err := os.Executable()
if err != nil {
log.Warningf("cannot find current executable file path. Application won't start automatically: %s", err)
return
}
app := &autostart.App{
Name: a.Name,
DisplayName: a.DisplayName,
Exec: []string{executable},
}
autostartItem := systray.AddMenuItemCheckbox("Start at login", "Start this app at system startup", app.IsEnabled())
close(done) // autostartItem is displayed, we have done
for {
<-autostartItem.ClickedCh // wait for a click
switch app.IsEnabled() {
case true:
log.Debugln("disable Application autostart")
if err := app.Disable(); err != nil {
log.Errorln(err)
continue
}
autostartItem.Uncheck()
case false:
log.Debugln("enable Application autostart")
if err := app.Enable(); err != nil {
log.Errorln(err)
continue
}
autostartItem.Check()
}
}
}
func (a *Application) HandleClipboard(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Debugln("received context cancel: HandleClipboard")
return // returning not to leak the goroutine
case link := <-a.ClipboardListener:
log.Tracef("setting [%s] to clipboard", link)
if err := clipboard.WriteAll(link); err != nil {
log.Errorln(err)
continue // do not stop this routine in case of error
}
}
}
}
// Refresh hide or show menu items based on currently active streams
func (a *Application) Refresh(activeStreams []*twitch.Stream) {
for _, item := range a.State {
itemIsAnActiveStream := funk.Contains(activeStreams, func(stream *twitch.Stream) bool {
return stream.UserLogin == item.UserLogin
})
item.SetVisible(itemIsAnActiveStream)
}
}
func (a *Application) DisplayConnectedUser() {
me := a.Twitch.Users.Me()
title := fmt.Sprintf("Connected as %s", me.DisplayName)
systray.AddMenuItem(title, "Current user").Disable()
}
// RefreshActiveStreams send active streams to out
func (a *Application) RefreshActiveStreams(ctx context.Context, out chan<- []*twitch.Stream) {
ticker := time.NewTicker(time.Second * 10)
defer ticker.Stop()
job := func() {
log.Debugln("refreshing followed streams infos")
// This simulates /streams/followed endpoint
streams, err := a.Twitch.Streams.GetFollowed()
if err != nil {
log.Errorf("unable to list followed streams: %s", err)
return
}
// job done, notify out for the new stream list
out <- streams
}
// https://stackoverflow.com/a/54752803
for {
job()
select {
case <-ctx.Done():
log.Debugln("received context cancel: RefreshActiveStreams")
return // returning not to leak the goroutine
case <-ticker.C:
continue
}
}
}
// RefreshStreamsMenuItem display a menu Item for each stream received in the channel in
func (a *Application) RefreshStreamsMenuItem(ctx context.Context, in <-chan []*twitch.Stream) {
// not active stream menu Item
menuNoActiveStreams := &Item{
Application: nil,
Item: systray.AddMenuItem("No active stream", "No active stream"),
Visible: true,
UserLogin: "",
mutex: sync.Mutex{},
}
menuNoActiveStreams.Disable()
// display connected user
a.DisplayConnectedUser()
for {
select {
case <-ctx.Done():
log.Debugln("received context cancel: RefreshStreamsMenuItem")
return // returning not to leak the goroutine
case activeStreams := <-in:
log.Debugf("refreshing menu items for %d active followed streams", len(activeStreams))
menuNoActiveStreams.SetVisible(len(activeStreams) == 0)
for _, s := range activeStreams {
// stream already in the stream list. Refresh title and tooltip and show it
if v, ok := a.State[s.UserLogin]; ok {
v.Refresh(s)
continue
}
// stream not already in the stream list, make it!
a.State[s.UserLogin] = a.NewItem(ctx, s)
}
// refresh app
a.Refresh(activeStreams)
}
}
}
// NewItem creates a new menu Item and its underlying routines
func (a *Application) NewItem(ctx context.Context, s *twitch.Stream) *Item {
log.WithFields(map[string]interface{}{
"login": s.UserLogin,
"user_login": s.UserName,
"username": s.UserName,
"game": s.GameName,
}).Tracef("new active stream detected [%s]", s.UserLogin)
// sometimes the Twitch API does not send the username at first call, use the user UserLogin instead
username := s.UserName
if username == "" {
username = s.UserLogin
}
item := &Item{
Application: a,
Item: systray.AddMenuItem(fmt.Sprintf("%s (%s)", username, s.GameName), s.Title),
Visible: true, // Visible by default
UserLogin: s.UserLogin,
Username: s.UserName,
Game: s.GameName,
mutex: sync.Mutex{},
}
// Start routine to pull its icon
go item.SetIcon()
// Start routine click for this Item
go item.Click(ctx)
// New item appear, so notify if configured
if item.ShouldNotify() {
if err := a.Notifier.Notify(username, item.Game, item.UserLogin); err != nil {
log.Errorf("fail to notify for [%s]: %s", item.UserLogin, err)
}
}
return item
}
// HandleNotificationCallback receives notification callback and launch streamlink process
func (a *Application) HandleNotificationCallback(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Debugln("received context cancel: HandleNotificationCallback")
return // returning not to leak the goroutine
case v := <-a.NotificationCallbackCh:
// get menu item matching streamer name
item, ok := a.State[v]
if !ok {
log.Errorf("received notification callback for non-existent stream [%s]", v)
continue
}
// simulate a click
item.Item.ClickedCh <- struct{}{}
}
}
}