-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
107 lines (98 loc) · 2.89 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
package main
import (
"fmt"
"github.com/fzxiao233/Vtb_Record/config"
"github.com/fzxiao233/Vtb_Record/live"
"github.com/fzxiao233/Vtb_Record/live/monitor"
"github.com/fzxiao233/Vtb_Record/live/plugins"
"github.com/fzxiao233/Vtb_Record/live/videoworker"
log "github.com/sirupsen/logrus"
"math/rand"
"os/exec"
"strings"
"sync"
"time"
)
func initPluginManager() videoworker.PluginManager {
pm := videoworker.PluginManager{}
pm.AddPlugin(&plugins.PluginCQBot{})
return pm
}
func checkStreamlink() {
c := exec.Command("streamlink", "--version")
output, _ := c.CombinedOutput()
if !strings.Contains(string(output), "streamlink") {
log.Fatal("Cannot find streamlink")
}
}
func arrangeTask() {
log.Printf("Arrange tasks...")
pm := initPluginManager()
status := make([]map[string]bool, len(config.Config.Module))
for i, module := range config.Config.Module {
status[i] = make(map[string]bool, len(module.Users))
}
go func() {
ticker := time.NewTicker(time.Second * time.Duration(1))
for {
if config.Changed {
time.Sleep(4 * time.Second) // wait to ensure the config is fully written
ret, err := config.ReloadConfig()
if ret {
if err == nil {
log.Infof("\n\n\t\tConfig changed and load successfully!\n\n")
} else {
log.Warnf("Config changed but loading failed: %s", err)
}
}
}
<-ticker.C
}
}()
var statusMx sync.Mutex
for {
var mods []config.ModuleConfig
living := make([]string, 0, 128)
changed := make([]string, 0, 128)
mods = make([]config.ModuleConfig, len(config.Config.Module))
copy(mods, config.Config.Module)
for mod_i, module := range mods {
if module.Enable {
for _, usersConfig := range module.Users {
identifier := fmt.Sprintf("\"%s-%s\"", usersConfig.Name, usersConfig.TargetId)
statusMx.Lock()
if status[mod_i][identifier] != false {
living = append(living, fmt.Sprintf("\"%s-%s\"", usersConfig.Name, usersConfig.TargetId))
statusMx.Unlock()
continue
}
status[mod_i][identifier] = true
statusMx.Unlock()
changed = append(changed, identifier)
go func(i int, j string, mon monitor.VideoMonitor, userCon config.UsersConfig) {
live.StartMonitor(mon, userCon, pm)
statusMx.Lock()
status[i][j] = false
statusMx.Unlock()
}(mod_i, identifier, monitor.CreateVideoMonitor(module), usersConfig)
time.Sleep(time.Millisecond * 20)
}
}
}
log.Infof("current living %s", living)
log.Tracef("checked %s", changed)
if time.Now().Minute() > 55 || time.Now().Minute() < 5 || (time.Now().Minute() > 25 && time.Now().Minute() < 35) {
time.Sleep(time.Duration(config.Config.CriticalCheckSec) * time.Second)
} else {
time.Sleep(time.Duration(config.Config.NormalCheckSec) * time.Second)
}
}
}
func main() {
rand.Seed(time.Now().UnixNano())
config.PrepareConfig()
config.InitLog()
go config.InitProfiling()
checkStreamlink()
arrangeTask()
}