-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
78 lines (65 loc) · 1.39 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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/joho/godotenv"
)
var (
googleClientID string
googleClientSecret string
liveID = flag.String("liveid", "", "specify target YouTube Live id")
isJSON = flag.Bool("json", false, "use JSON input output instead of TUI")
)
func main() {
godotenv.Load()
if googleClientID == "" {
googleClientID = os.Getenv("GOOGLE_CLIENT_ID")
}
if googleClientSecret == "" {
googleClientSecret = os.Getenv("GOOGLE_CLIENT_SECRET")
}
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
config := NewConfig()
tm := NewOAuthManager(googleClientID, googleClientSecret, config.TokenStore())
ts, err := tm.TokenSource(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
if *liveID == "" {
fmt.Print("Input live id: ")
var lid string
_, err = fmt.Scanf("%s\n", &lid)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to scan live id: %v\n", err)
return
}
liveID = &lid
}
chMsgList, chErr, err := pollYoutubeLiveChatMessages(ctx, ts, *liveID)
if *isJSON {
jsonIO := NewJSONIO(os.Stdout)
jsonIO.Run(chMsgList)
} else {
tui := NewTUI()
go func() {
err := tui.Run(chMsgList)
cancel()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}()
}
for {
select {
case <-ctx.Done():
return
case err = <-chErr:
fmt.Fprintf(os.Stderr, "%v\n", err)
cancel()
}
}
}