feat(internal/reminder): Signal提醒
This commit is contained in:
@@ -2,11 +2,10 @@ module github.com/ssdomei232/goodBaby
|
|||||||
|
|
||||||
go 1.24.0
|
go 1.24.0
|
||||||
|
|
||||||
toolchain go1.24.7
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/CuteReimu/bilibili/v2 v2.3.5
|
github.com/CuteReimu/bilibili/v2 v2.3.5
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/wneessen/go-mail v0.7.2
|
github.com/wneessen/go-mail v0.7.2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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/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 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
|
||||||
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
|
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 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
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=
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||||
|
|||||||
@@ -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登录")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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提醒")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
// internal/timer_manager.go
|
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"html/template"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -11,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/CuteReimu/bilibili/v2"
|
"github.com/CuteReimu/bilibili/v2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/ssdomei232/goodBaby/configs"
|
"github.com/ssdomei232/goodBaby/configs"
|
||||||
"github.com/ssdomei232/goodBaby/internal"
|
"github.com/ssdomei232/goodBaby/internal"
|
||||||
)
|
)
|
||||||
@@ -46,8 +46,8 @@ func init() {
|
|||||||
|
|
||||||
// Init Bilibili Clinet
|
// Init Bilibili Clinet
|
||||||
biliClient = bilibili.New()
|
biliClient = bilibili.New()
|
||||||
initBilibili()
|
internal.InitBilibili(biliClient)
|
||||||
startCookieChecker() // Check Bilibili cookie
|
internal.StartCookieChecker(cookieCheckTimer, biliClient) // Check Bilibili cookie
|
||||||
|
|
||||||
internal.InitTimerManager(duration)
|
internal.InitTimerManager(duration)
|
||||||
timer = time.NewTimer(duration)
|
timer = time.NewTimer(duration)
|
||||||
@@ -57,6 +57,7 @@ func init() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// trigger
|
||||||
func trigger(config configs.Config) {
|
func trigger(config configs.Config) {
|
||||||
if config.EnableQQ {
|
if config.EnableQQ {
|
||||||
go internal.SendQQ()
|
go internal.SendQQ()
|
||||||
@@ -69,44 +70,26 @@ func trigger(config configs.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// cron job
|
||||||
|
internal.Reminder()
|
||||||
|
c := cron.New()
|
||||||
|
c.AddFunc("@every 1h", internal.Reminder)
|
||||||
|
c.Start()
|
||||||
|
|
||||||
|
// Web service
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
templFS, _ := fs.Sub(templateFiles, "templates")
|
templFS, _ := fs.Sub(templateFiles, "templates")
|
||||||
r.SetHTMLTemplate(loadTemplates(templFS))
|
r.SetHTMLTemplate(internal.LoadTemplates(templFS))
|
||||||
staticFS, _ := fs.Sub(staticFiles, "static")
|
staticFS, _ := fs.Sub(staticFiles, "static")
|
||||||
r.StaticFS("/static", http.FS(staticFS))
|
r.StaticFS("/static", http.FS(staticFS))
|
||||||
|
|
||||||
r.GET("/", indexPage)
|
r.GET("/", internal.IndexPage)
|
||||||
r.GET("/signal", handleSignal)
|
r.GET("/signal", handleSignal)
|
||||||
r.GET("/timer/status", internal.HandleTimerStatus)
|
r.GET("/timer/status", internal.HandleTimerStatus)
|
||||||
r.Run(":8088")
|
r.Run(":8088")
|
||||||
}
|
}
|
||||||
|
|
||||||
func indexPage(c *gin.Context) {
|
// Handle Signal
|
||||||
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleSignal(c *gin.Context) {
|
func handleSignal(c *gin.Context) {
|
||||||
config, err := configs.GetConfig()
|
config, err := configs.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -129,9 +112,3 @@ func handleSignal(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
log.Println("触发信号")
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user