Skip to content

Commit

Permalink
add engine config
Browse files Browse the repository at this point in the history
  • Loading branch information
skye-z committed Oct 11, 2023
1 parent b7605f8 commit f77977a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 45 deletions.
2 changes: 2 additions & 0 deletions global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
// 持久化存储位置
Expand Down
9 changes: 9 additions & 0 deletions global/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package global

import "github.com/playwright-community/playwright-go"

type RunPKG struct {
Engine *playwright.Playwright
Running bool
Error string
}
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
// 禁用路由日志
Expand All @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions service/mooc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -108,6 +108,7 @@ func (ms MoocService) ClassList(ctx *gin.Context) {
list = append(list, *classInfo)
}
global.ReturnData(ctx, true, "请选择课程", list)
session.Page.Close()
}

// 选课
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions service/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -21,19 +21,19 @@ 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+", 任务未启动")
}
}

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, "获取任务截图失败")
}
Expand Down
18 changes: 14 additions & 4 deletions webkit/basic.go → work/basic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webkit
package work

import (
"github.com/skye-z/auto-mooc/global"
Expand All @@ -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
}
Expand Down
38 changes: 20 additions & 18 deletions webkit/init.go → work/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webkit
package work

import (
"io"
Expand All @@ -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,
}
Expand All @@ -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)
}
File renamed without changes.
8 changes: 4 additions & 4 deletions webkit/work.go → work/work.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webkit
package work

import (
_ "embed"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit f77977a

Please sign in to comment.