Skip to content

Commit

Permalink
Merge pull request #85 from fzxiao233/refact
Browse files Browse the repository at this point in the history
Rebase and simplify
  • Loading branch information
AlotOfBlahaj committed Aug 23, 2021
2 parents 536ba5f + d903a4c commit 8ef874b
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 207 deletions.
12 changes: 6 additions & 6 deletions config/config.go
Expand Up @@ -14,7 +14,7 @@ import (
)

var Config *MainConfig
var ConfigChanged bool
var Changed bool

type UsersConfig struct {
TargetId string
Expand All @@ -41,7 +41,7 @@ type MainConfig struct {
LogLevel string
RLogLevel string
DownloadQuality string
DownloadDir []string
DownloadDir string
UploadDir string
Module []ModuleConfig
ExpressPort string
Expand Down Expand Up @@ -74,17 +74,17 @@ func initConfig() {
os.Exit(1)
}

ConfigChanged = true
Changed = true
v.OnConfigChange(func(in fsnotify.Event) {
ConfigChanged = true
Changed = true
})
}

func ReloadConfig() (bool, error) {
if !ConfigChanged {
if !Changed {
return false, nil
}
ConfigChanged = false
Changed = false
err := v.ReadInConfig()
if err != nil {
return true, err
Expand Down
4 changes: 2 additions & 2 deletions live/monitor/monitorUtils.go
Expand Up @@ -11,7 +11,7 @@ import (
)

type VideoMonitor = base.VideoMonitor
type LiveTrace func(monitor VideoMonitor) *interfaces.LiveStatus
type LiveTrace func() *interfaces.LiveStatus

// Monitor is responsible for checking if live starts & live's title/link changed
func CreateVideoMonitor(module config.ModuleConfig) VideoMonitor {
Expand All @@ -33,7 +33,7 @@ func CreateVideoMonitor(module config.ModuleConfig) VideoMonitor {
}

// sanitize everything in the videoinfo for downloader & plugins
func CleanVideoInfo(info *interfaces.VideoInfo) *interfaces.VideoInfo {
func GetCleanVideoInfo(info *interfaces.VideoInfo) *interfaces.VideoInfo {
info.Title = utils.RemoveIllegalChar(info.Title)
return info
}
3 changes: 1 addition & 2 deletions live/videoworker/downloader/provbase/downloader.go
Expand Up @@ -18,14 +18,13 @@ type Downloader struct {
}

func (d *Downloader) DownloadVideo(video *interfaces.VideoInfo, proxy string, cookie string, filePath string) string {
//rl.Take()
logger := log.WithField("video", video)
logger.Infof("start to download")
video.FilePath = filePath
err := d.Prov.StartDownload(video, proxy, cookie, filePath)
logger.Infof("finished with status: %s", err)
if !utils.IsFileExist(filePath) {
logger.Infof("%s the video file don't exist", filePath)
logger.Infof("download failed: %s", err)
return ""
}
logger.Infof("%s download successfully", filePath)
Expand Down
77 changes: 0 additions & 77 deletions live/videoworker/downloader/stealth/stealth.go

This file was deleted.

15 changes: 7 additions & 8 deletions live/videoworker/videoProcesser.go
Expand Up @@ -41,16 +41,15 @@ func init() {
limit = rate.NewLimiter(rate.Every(time.Second*5), 1)
}

func StartProcessVideo(LiveTrace monitor.LiveTrace, Monitor monitor.VideoMonitor, Plugins PluginManager) *ProcessVideo {
func StartProcessVideo(LiveTrace monitor.LiveTrace, Monitor monitor.VideoMonitor, Plugins PluginManager) {
p := &ProcessVideo{LiveTrace: LiveTrace, Monitor: Monitor, Plugins: Plugins}
liveStatus := LiveTrace(Monitor)
liveStatus := LiveTrace()
if liveStatus.IsLive {
p.LiveStatus = liveStatus
p.appendTitleHistory(p.LiveStatus.Video.Title)
limit.Wait(context.Background())
p.StartProcessVideo()
}
return p
}

func (p *ProcessVideo) getLogger() *log.Entry {
Expand Down Expand Up @@ -82,10 +81,10 @@ func (p *ProcessVideo) StartProcessVideo() {
func (p *ProcessVideo) prepareDownload() error {
var pathSlice []string
if !config.Config.EnableTS2MP4 {
pathSlice = []string{utils.RandChooseStr(config.Config.DownloadDir), p.LiveStatus.Video.UsersConfig.Name,
pathSlice = []string{config.Config.DownloadDir, p.LiveStatus.Video.UsersConfig.Name,
p.liveStartTime.Format("20060102 150405")}
} else {
pathSlice = []string{utils.RandChooseStr(config.Config.DownloadDir), p.LiveStatus.Video.UsersConfig.Name}
pathSlice = []string{config.Config.DownloadDir, p.LiveStatus.Video.UsersConfig.Name}
}
dirpath := strings.Join(pathSlice, "/")
ret, err := utils.MakeDir(dirpath)
Expand Down Expand Up @@ -203,7 +202,7 @@ func (p *ProcessVideo) keepLiveAlive() {
p.needStop = true
if p.isNeedDownload() {
close(p.triggerChan)
return // 需要下载时不由此控制end
return
}
p.finish <- 1
return
Expand All @@ -219,7 +218,7 @@ func (p *ProcessVideo) appendTitleHistory(title string) {
}

func (p *ProcessVideo) isNewLive() bool {
newLiveStatus := p.LiveTrace(p.Monitor)
newLiveStatus := p.LiveTrace()
logger := p.getLogger()
if newLiveStatus.IsLive == false || p.LiveStatus.IsLive == false {
logger.Infof("[isNewLive] live offline")
Expand Down Expand Up @@ -256,7 +255,7 @@ func (p *ProcessVideo) getFullTitle() string {
}

func (p *ProcessVideo) postProcessing() string {
pathSlice := []string{config.Config.UploadDir, p.LiveStatus.Video.UsersConfig.Name} // , p.getFullTitle()
pathSlice := []string{config.Config.DownloadDir, p.LiveStatus.Video.UsersConfig.Name} // , p.getFullTitle()
dirpath := strings.Join(pathSlice, "/")
_, err := utils.MakeDir(filepath.Dir(dirpath))
if err != nil {
Expand Down
13 changes: 3 additions & 10 deletions live/wrapper.go
Expand Up @@ -9,20 +9,13 @@ import (
)

func StartMonitor(mon base.VideoMonitor, usersConfig config.UsersConfig, pm videoworker.PluginManager) {
//ticker := time.NewTicker(time.Second * time.Duration(utils.Config.CheckSec))
//for {
//pm.AddPlugin(&plugins.PluginTranslationRecorder{})
//pm.AddPlugin(&plugins.PluginUploader{})

var fun = func(mon base.VideoMonitor) *interfaces.LiveStatus {
var liveTrace = func() *interfaces.LiveStatus {
return &interfaces.LiveStatus{
IsLive: mon.CheckLive(usersConfig),
Video: monitor.CleanVideoInfo(mon.CreateVideo(usersConfig)),
Video: monitor.GetCleanVideoInfo(mon.CreateVideo(usersConfig)),
}
}

videoworker.StartProcessVideo(fun, mon, pm)
videoworker.StartProcessVideo(liveTrace, mon, pm)
return
//<-ticker.C
//}
}
81 changes: 10 additions & 71 deletions main.go
Expand Up @@ -7,20 +7,13 @@ import (
"github.com/fzxiao233/Vtb_Record/live/monitor"
"github.com/fzxiao233/Vtb_Record/live/plugins"
"github.com/fzxiao233/Vtb_Record/live/videoworker"
"github.com/fzxiao233/Vtb_Record/utils"
log "github.com/sirupsen/logrus"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"sync"
"syscall"
"time"
)

var SafeStop bool

func initPluginManager() videoworker.PluginManager {
pm := videoworker.PluginManager{}
pm.AddPlugin(&plugins.PluginCQBot{})
Expand All @@ -38,27 +31,21 @@ func arrangeTask() {
go func() {
ticker := time.NewTicker(time.Second * time.Duration(1))
for {
if config.ConfigChanged {
allDone := true
if allDone {
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)
}
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
}

}()
for _, dir := range config.Config.DownloadDir {
utils.MakeDir(dir)
}

var statusMx sync.Mutex
for {
Expand Down Expand Up @@ -94,63 +81,15 @@ func arrangeTask() {
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)
}
time.Sleep(time.Duration(config.Config.NormalCheckSec) * time.Second)

if SafeStop {
break
} else {
time.Sleep(time.Duration(config.Config.NormalCheckSec) * time.Second)
}
}
for {
living := make([]string, 0, 128)
statusMx.Lock()
for _, mod := range status {
for name, val := range mod {
if val {
living = append(living, name)
}
}
}
statusMx.Unlock()
if len(living) == 0 {
break
}
log.Infof("Waiting to finish: current living %s", living)
time.Sleep(time.Second * 5)
}
log.Infof("All tasks finished! Wait an additional time to ensure everything's saved")
time.Sleep(time.Second * 300)
log.Infof("Everything finished, exiting now~~")
}

func handleInterrupt() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Warnf("Ctrl+C pressed in Terminal!")
time.Sleep(5 * time.Second) // wait rclone upload finish..
os.Exit(0)
}()
}

func handleUpdate() {
c := make(chan os.Signal)
SIGUSR1 := syscall.Signal(10)
signal.Notify(c, SIGUSR1)
go func() {
<-c
log.Warnf("Received update signal! Waiting everything done!")
SafeStop = true
}()
}

func main() {
handleInterrupt()
handleUpdate()
rand.Seed(time.Now().UnixNano())

http.DefaultClient.Transport = http.DefaultTransport
config.PrepareConfig()
config.InitLog()
go config.InitProfiling()
Expand Down
23 changes: 0 additions & 23 deletions utils/file.go

This file was deleted.

0 comments on commit 8ef874b

Please sign in to comment.