diff --git a/go.mod b/go.mod index a7f306e..2e231ec 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,10 @@ module github.com/ssdomei232/goodBaby go 1.24.0 -toolchain go1.24.7 - require ( github.com/CuteReimu/bilibili/v2 v2.3.5 github.com/gin-gonic/gin v1.11.0 + github.com/robfig/cron/v3 v3.0.1 github.com/wneessen/go-mail v0.7.2 ) diff --git a/go.sum b/go.sum index 99d2387..48f2f27 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,8 @@ github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI= github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg= github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg= github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= diff --git a/internal/bilibili.go b/internal/bilibili.go index 9ff96a8..de6c989 100644 --- a/internal/bilibili.go +++ b/internal/bilibili.go @@ -262,3 +262,25 @@ func triggerLoginRequest(biliClient *bilibili.Client) { } } } + +// Enable periodic cookie checks +func StartCookieChecker(cookieCheckTimer *time.Ticker, biliClient *bilibili.Client) { + // check cookie per hour + cookieCheckTimer = time.NewTicker(1 * time.Hour) + go func() { + for { + <-cookieCheckTimer.C + CheckCookieValidity(biliClient) + } + }() +} + +func InitBilibili(biliClient *bilibili.Client) { + // try cached cookie + if !LoadCookies(biliClient) { + // if cookie check failed, request qrcode login + LoginWithQRCode(biliClient) + } else { + log.Println("使用已存储的有效cookie登录") + } +} diff --git a/internal/remind.go b/internal/remind.go new file mode 100644 index 0000000..fa3a6ea --- /dev/null +++ b/internal/remind.go @@ -0,0 +1,22 @@ +package internal + +import ( + "fmt" + "log" + "time" + + "github.com/ssdomei232/goodBaby/configs" +) + +func Reminder() { + config, err := configs.GetConfig() + if err != nil { + log.Printf("获取配置文件失败: %v", err) + } + + remainingTime := GlobalTimerManager.GetRemainingTime() + if remainingTime <= 48*time.Hour { + sendMsg := fmt.Sprintf("距离摇篮系统触发还有%d小时,请尽快发送Signal", int(remainingTime.Hours())) + sendMailMsg(config.BiliWarnAddress, sendMsg, "GoodBaby提醒") + } +} diff --git a/internal/timer.go b/internal/timer.go index 7fd06f1..0962def 100644 --- a/internal/timer.go +++ b/internal/timer.go @@ -1,4 +1,3 @@ -// internal/timer_manager.go package internal import ( diff --git a/internal/web.go b/internal/web.go new file mode 100644 index 0000000..fcc01b1 --- /dev/null +++ b/internal/web.go @@ -0,0 +1,19 @@ +package internal + +import ( + "html/template" + "io/fs" + "net/http" + + "github.com/gin-gonic/gin" +) + +// loadTemplates loads templates from the embedded file system +func LoadTemplates(filesystem fs.FS) *template.Template { + templ := template.Must(template.New("").ParseFS(filesystem, "*.html")) + return templ +} + +func IndexPage(c *gin.Context) { + c.HTML(http.StatusOK, "index.html", nil) +} diff --git a/main.go b/main.go index a5cd2c2..da3ddc4 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "embed" - "html/template" "io/fs" "log" "net/http" @@ -11,6 +10,7 @@ import ( "github.com/CuteReimu/bilibili/v2" "github.com/gin-gonic/gin" + "github.com/robfig/cron/v3" "github.com/ssdomei232/goodBaby/configs" "github.com/ssdomei232/goodBaby/internal" ) @@ -46,8 +46,8 @@ func init() { // Init Bilibili Clinet biliClient = bilibili.New() - initBilibili() - startCookieChecker() // Check Bilibili cookie + internal.InitBilibili(biliClient) + internal.StartCookieChecker(cookieCheckTimer, biliClient) // Check Bilibili cookie internal.InitTimerManager(duration) timer = time.NewTimer(duration) @@ -57,6 +57,7 @@ func init() { }() } +// trigger func trigger(config configs.Config) { if config.EnableQQ { go internal.SendQQ() @@ -69,44 +70,26 @@ func trigger(config configs.Config) { } func main() { + // cron job + internal.Reminder() + c := cron.New() + c.AddFunc("@every 1h", internal.Reminder) + c.Start() + + // Web service r := gin.Default() templFS, _ := fs.Sub(templateFiles, "templates") - r.SetHTMLTemplate(loadTemplates(templFS)) + r.SetHTMLTemplate(internal.LoadTemplates(templFS)) staticFS, _ := fs.Sub(staticFiles, "static") r.StaticFS("/static", http.FS(staticFS)) - r.GET("/", indexPage) + r.GET("/", internal.IndexPage) r.GET("/signal", handleSignal) r.GET("/timer/status", internal.HandleTimerStatus) r.Run(":8088") } -func indexPage(c *gin.Context) { - c.HTML(http.StatusOK, "index.html", nil) -} - -func initBilibili() { - // try cached cookie - if !internal.LoadCookies(biliClient) { - // if cookie check failed, request qrcode login - internal.LoginWithQRCode(biliClient) - } else { - log.Println("使用已存储的有效cookie登录") - } -} - -// Enable periodic cookie checks -func startCookieChecker() { - // check cookie per hour - cookieCheckTimer = time.NewTicker(1 * time.Hour) - go func() { - for { - <-cookieCheckTimer.C - internal.CheckCookieValidity(biliClient) - } - }() -} - +// Handle Signal func handleSignal(c *gin.Context) { config, err := configs.GetConfig() if err != nil { @@ -129,9 +112,3 @@ func handleSignal(c *gin.Context) { }) log.Println("触发信号") } - -// loadTemplates loads templates from the embedded file system -func loadTemplates(filesystem fs.FS) *template.Template { - templ := template.Must(template.New("").ParseFS(filesystem, "*.html")) - return templ -}