forked from WabisabiNeet/CollectSuperChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectChat.go
125 lines (107 loc) · 2.44 KB
/
CollectChat.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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sort"
"strconv"
"sync"
"time"
"github.com/WabisabiNeet/CollectSuperChat/chromedp"
"github.com/WabisabiNeet/CollectSuperChat/collector"
"github.com/WabisabiNeet/CollectSuperChat/currency"
"github.com/WabisabiNeet/CollectSuperChat/log"
"github.com/WabisabiNeet/CollectSuperChat/notifier"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
// MaxKeys is api keys.
const MaxKeys = 9
var collectors = []*collector.Collector{}
func init() {
for i := 1; i < MaxKeys; i++ {
apikey := os.Getenv(fmt.Sprintf("YOUTUBE_WATCH_LIVE_KEY%v", i))
// apikey = os.Getenv("YOUTUBE_WATCH_LIVE_KEY")
if apikey == "" {
break
}
ctx := context.Background()
ys, err := youtube.NewService(ctx, option.WithAPIKey(apikey))
if err != nil {
log.Fatal(err.Error())
}
collectors = append(collectors, &collector.Collector{
ID: strconv.FormatInt(int64(i), 10),
YoutubeService: ys,
ProcessingCount: 0,
})
}
if len(collectors) == 0 {
log.Fatal("not found api key.")
}
}
func pollCurrency() {
collect := func() {
for _, c := range currency.Currencies {
err := c.ScrapeRataToJPY()
if err != nil {
log.Warn(err.Error())
}
log.Info("[%v] %v", c.Code, c.RateToJPY)
}
}
collect()
go func() {
quit := make(chan os.Signal, 1)
defer close(quit)
signal.Notify(quit, os.Interrupt)
t := time.NewTicker(12 * time.Hour)
defer t.Stop()
for {
select {
case <-t.C:
log.Info("pollCurrency timer ticked.")
case <-quit:
return
}
collect()
}
}()
}
func main() {
defer log.Sync()
defer log.SyncSuerChat()
m := sync.Mutex{}
wg := &sync.WaitGroup{}
f := func(vid string) {
m.Lock()
defer m.Unlock()
sort.Slice(collectors, func(i, j int) bool {
return collectors[i].ProcessingCount < collectors[j].ProcessingCount
})
wg.Add(1)
collectors[0].IncrementCount()
log.Info(fmt.Sprintf("watch start ID[%v] ProcessingCount[%v]", collectors[0].ID, collectors[0].ProcessingCount))
go collectors[0].StartWatch(wg, vid, false, 8081)
}
var ns []notifier.Notifier
ns = append(ns, ¬ifier.Gmail{
CollectChat: f,
})
ns = append(ns, ¬ifier.YoutubeHTML{
CollectChat: f,
})
pollCurrency()
// ytproxy.OpenYoutubeLiveChatProxy(8081)
err := chromedp.InitChrome()
if err != nil {
log.Fatal(err.Error())
}
defer chromedp.TerminateChrome()
for _, n := range ns {
wg.Add(1)
go n.PollingStart(wg)
}
wg.Wait()
}