diff --git a/global/config.go b/global/config.go index a4b14ce..c775f41 100644 --- a/global/config.go +++ b/global/config.go @@ -49,6 +49,8 @@ func createDefault() { viper.SetDefault("basic.user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15") // 调试模式 viper.SetDefault("basic.debug", "false") + // 调试模式 + viper.SetDefault("basic.engine", "webkit") // 慕课服务地址 viper.SetDefault("mooc.path", "http://cce.org.uooconline.com") // 持久化存储位置 diff --git a/global/pkg.go b/global/pkg.go new file mode 100644 index 0000000..626a230 --- /dev/null +++ b/global/pkg.go @@ -0,0 +1,9 @@ +package global + +import "github.com/playwright-community/playwright-go" + +type RunPKG struct { + Engine *playwright.Playwright + Running bool + Error string +} diff --git a/main.go b/main.go index 1df0264..08d9977 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( "github.com/skye-z/auto-mooc/global" "github.com/skye-z/auto-mooc/service" - "github.com/skye-z/auto-mooc/webkit" + "github.com/skye-z/auto-mooc/work" "github.com/gin-gonic/gin" ) @@ -21,17 +21,17 @@ func main() { // 初始化配置 global.InitConfig() // 初始化WebKit - WebKit := webkit.InitWebKit() + work := work.InitWork() if len(os.Args) == 1 { // 启动Http服务 - RunHttp(WebKit) + RunHttp(work) } else { os.Exit(0) } } // 启动Http服务 -func RunHttp(obj *webkit.WebKit) { +func RunHttp(obj *global.RunPKG) { // 关闭调试 gin.SetMode(gin.ReleaseMode) // 禁用路由日志 @@ -54,11 +54,11 @@ func RunHttp(obj *webkit.WebKit) { }) // 创建状态服务 statusService := &service.StatusService{ - WebKitObj: obj, + PKG: obj, } // 创建慕课服务 moocService := &service.MoocService{ - WebKitObj: obj, + PKG: obj, } // 接口 查询状态 route.GET("/status", statusService.GetStatus) diff --git a/service/mooc.go b/service/mooc.go index 651d0c3..df1d461 100644 --- a/service/mooc.go +++ b/service/mooc.go @@ -7,14 +7,14 @@ import ( "time" "github.com/skye-z/auto-mooc/global" - "github.com/skye-z/auto-mooc/webkit" + "github.com/skye-z/auto-mooc/work" "github.com/gin-gonic/gin" "github.com/playwright-community/playwright-go" ) type MoocService struct { - WebKitObj *webkit.WebKit + PKG *global.RunPKG } type Class struct { @@ -26,7 +26,7 @@ type Class struct { func (ms MoocService) Login(ctx *gin.Context) { host := global.GetString("mooc.path") // 打开页面 - session, err := webkit.OpenPage(ms.WebKitObj.Engine, host) + session, err := work.OpenPage(ms.PKG.Engine, host) if err != nil { log.Fatalf("无法打开页面: %v", err) } @@ -80,7 +80,7 @@ func (ms MoocService) checkLogin(ctx *gin.Context, host string, page playwright. func (ms MoocService) ClassList(ctx *gin.Context) { host := global.GetString("mooc.path") // 打开页面 - session, err := webkit.OpenPage(ms.WebKitObj.Engine, host+"/home") + session, err := work.OpenPage(ms.PKG.Engine, host+"/home") if err != nil { log.Fatalf("无法打开页面: %v", err) } @@ -108,6 +108,7 @@ func (ms MoocService) ClassList(ctx *gin.Context) { list = append(list, *classInfo) } global.ReturnData(ctx, true, "请选择课程", list) + session.Page.Close() } // 选课 @@ -137,11 +138,11 @@ func (ms MoocService) StartClass(ctx *gin.Context) { } host := global.GetString("mooc.path") // 打开页面 - session, err := webkit.OpenPage(ms.WebKitObj.Engine, host+"/home/learn/index#/"+classId+"/go") + session, err := work.OpenPage(ms.PKG.Engine, host+"/home/learn/index#/"+classId+"/go") if err != nil { log.Fatalf("无法打开页面: %v", err) } - state := webkit.CreateWork(session, ms.WebKitObj) + state := work.CreateWork(session, ms.PKG) if !state { global.ReturnMessage(ctx, false, "正在上课中") return @@ -152,7 +153,7 @@ func (ms MoocService) StartClass(ctx *gin.Context) { // 结束上课 func (ms MoocService) StopClass(ctx *gin.Context) { - state := webkit.CloseWork(ms.WebKitObj) + state := work.CloseWork(ms.PKG) if !state { global.ReturnMessage(ctx, false, "未在上课中") return @@ -168,7 +169,7 @@ func (ms MoocService) getLoginStatus(page playwright.Page) bool { } // 关闭会话 -func (ms MoocService) Close(session *webkit.Session) { +func (ms MoocService) Close(session *work.Session) { time.Sleep(1 * time.Second) session.Page.Close() session.Context.Close() diff --git a/service/status.go b/service/status.go index 012722a..45286ea 100644 --- a/service/status.go +++ b/service/status.go @@ -2,13 +2,13 @@ package service import ( "github.com/skye-z/auto-mooc/global" - "github.com/skye-z/auto-mooc/webkit" + "github.com/skye-z/auto-mooc/work" "github.com/gin-gonic/gin" ) type StatusService struct { - WebKitObj *webkit.WebKit + PKG *global.RunPKG } func (ss StatusService) GetStatus(ctx *gin.Context) { @@ -21,7 +21,7 @@ func (ss StatusService) GetStatus(ctx *gin.Context) { if len(classId) != 0 { loginTips += ", 已选课(" + classId + ")" } - if ss.WebKitObj.Running { + if ss.PKG.Running { global.ReturnMessage(ctx, true, loginTips+", 任务执行中") } else { global.ReturnMessage(ctx, true, loginTips+", 任务未启动") @@ -29,11 +29,11 @@ func (ss StatusService) GetStatus(ctx *gin.Context) { } func (ss StatusService) GetScreenshot(ctx *gin.Context) { - if !ss.WebKitObj.Running { + if !ss.PKG.Running { global.ReturnMessage(ctx, false, "服务尚未启动") return } - data, err := webkit.WorkScreenshot() + data, err := work.WorkScreenshot() if err != nil { global.ReturnMessage(ctx, false, "获取任务截图失败") } diff --git a/webkit/basic.go b/work/basic.go similarity index 70% rename from webkit/basic.go rename to work/basic.go index 83c757c..6aa1f9e 100644 --- a/webkit/basic.go +++ b/work/basic.go @@ -1,4 +1,4 @@ -package webkit +package work import ( "github.com/skye-z/auto-mooc/global" @@ -14,10 +14,20 @@ type Session struct { func OpenPage(engine *playwright.Playwright, url string) (*Session, error) { storagePath := global.GetString("mooc.storage") + var ( + browser playwright.Browser + err error + ) // 启动浏览器 - browser, err := engine.WebKit.Launch(playwright.BrowserTypeLaunchOptions{ - Headless: playwright.Bool(!global.GetBool("basic.debug")), - }) + if global.GetString("basic.engine") == "webkit" { + browser, err = engine.WebKit.Launch(playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(!global.GetBool("basic.debug")), + }) + } else { + browser, err = engine.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(!global.GetBool("basic.debug")), + }) + } if err != nil { return nil, err } diff --git a/webkit/init.go b/work/init.go similarity index 55% rename from webkit/init.go rename to work/init.go index 87e8921..2ac02e7 100644 --- a/webkit/init.go +++ b/work/init.go @@ -1,4 +1,4 @@ -package webkit +package work import ( "io" @@ -17,39 +17,41 @@ var RunOptions = &playwright.RunOptions{ Verbose: true, } -type WebKit struct { - Engine *playwright.Playwright - Running bool - Error string -} - -func InitWebKit() *WebKit { +func InitWork() *global.RunPKG { + engine := global.GetString("basic.engine") + if global.GetString("basic.engine") != "webkit" { + RunOptions.Browsers = []string{"chromium"} + } // 检查是否已安装环境 installed := global.GetBool("basic.install") // 未安装 if !installed { - log.Println("[WebKit] No webkit detected, ready to install...") - // 开始安装WebKit + log.Println("[Work] No " + engine + " detected, ready to install...") + // 开始安装浏览器 err := playwright.Install(RunOptions) // 安装出错 if err != nil { - log.Fatalf("[WebKit] Error installing: %v", err) + log.Fatalf("[Work] Error installing: %v", err) return nil } // 创建持久化文件 CreateStorage() global.Set("basic.install", true) } - // 启动WebKit + // 启动浏览器 pw, err := playwright.Run(RunOptions) // 启动出错 if err != nil { - log.Fatalf("[WebKit] Error launching: %v", err) + log.Fatalf("[Work] Error launching: %v", err) return nil } // 输出启动脚步位置 - log.Printf("[WebKit] Launches from: %s", pw.WebKit.ExecutablePath()) - return &WebKit{ + if global.GetString("basic.engine") == "webkit" { + log.Printf("[Work] Launches from: %s", pw.WebKit.ExecutablePath()) + } else { + log.Printf("[Work] Launches from: %s", pw.Chromium.ExecutablePath()) + } + return &global.RunPKG{ Engine: pw, Running: false, } @@ -59,12 +61,12 @@ func CreateStorage() { path := global.GetString("basic.workspace") + "/storage.db" file, err := os.Create(path) if err != nil { - log.Fatalf("[WebKit] Create storage errir: %v", err) + log.Fatalf("[Work] Create storage errir: %v", err) } data := "{\"cookies\":[],\"origins\":[]}" _, err = io.Writer.Write(file, []byte(data)) if err != nil { - log.Fatalf("[WebKit] Write storage errir: %v", err) + log.Fatalf("[Work] Write storage errir: %v", err) } - log.Printf("[WebKit] Create storage from: %s", path) + log.Printf("[Work] Create storage from: %s", path) } diff --git a/webkit/script.js b/work/script.js similarity index 100% rename from webkit/script.js rename to work/script.js diff --git a/webkit/work.go b/work/work.go similarity index 93% rename from webkit/work.go rename to work/work.go index 04c93ae..9f95448 100644 --- a/webkit/work.go +++ b/work/work.go @@ -1,4 +1,4 @@ -package webkit +package work import ( _ "embed" @@ -27,7 +27,7 @@ var ( ) // 创建任务 -func CreateWork(session *Session, status *WebKit) bool { +func CreateWork(session *Session, status *global.RunPKG) bool { // 加锁防并发 workLock.Lock() defer workLock.Unlock() @@ -72,7 +72,7 @@ func CreateWork(session *Session, status *WebKit) bool { } // 关闭任务 -func CloseWork(status *WebKit) bool { +func CloseWork(status *global.RunPKG) bool { // 加锁防并发 workLock.Lock() defer workLock.Unlock() @@ -95,7 +95,7 @@ func CloseWork(status *WebKit) bool { } // 任务内容 -func WorkContent(session *Session, status *WebKit) { +func WorkContent(session *Session, status *global.RunPKG) { page := session.Page // 查找是否存在视频播放器 visible, _ := page.Locator("#player").IsVisible()