✨ 学习通获取课程列表

This commit is contained in:
sugarBecomer
2025-11-10 14:40:38 +08:00
parent dde5825d30
commit 7262023382
6 changed files with 344 additions and 2 deletions
@@ -0,0 +1,160 @@
package cookiejar
import (
"bytes"
"encoding/gob"
"github.com/bytedance/sonic"
"io"
"likedream-goframe/internal/utils"
"net/http"
"net/url"
"strings"
"sync"
)
type domainMap map[string]cookieMap
// AllSavePersistCookieJar 支持泛域名的 Cookie Jar
// 不同的二级域名会共享相同主域名的 cookies
// 例如: passport2.chaoxing.com 和 mooc1-api.chaoxing.com 共享 .chaoxing.com 的 cookies
type AllSavePersistCookieJar struct {
domainMap domainMap
*sync.RWMutex
}
func NewAllSavePersistCookieJar() *AllSavePersistCookieJar {
return &AllSavePersistCookieJar{
domainMap: make(domainMap),
RWMutex: &sync.RWMutex{},
}
}
// extractMainDomain 提取主域名
// 例如: passport2.chaoxing.com -> chaoxing.com
//
// mooc1-api.chaoxing.com -> chaoxing.com
// www.example.co.uk -> example.co.uk
func extractMainDomain(host string) string {
// 移除端口号
if colonIndex := strings.Index(host, ":"); colonIndex != -1 {
host = host[:colonIndex]
}
parts := strings.Split(host, ".")
if len(parts) <= 2 {
// 已经是主域名或单级域名
return host
}
// 处理特殊的二级顶级域名,如 .co.uk, .com.cn 等
// 简化处理:取最后两部分
// 更复杂的情况可以使用 publicsuffix 包
return strings.Join(parts[len(parts)-2:], ".")
}
func (a *AllSavePersistCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
a.Lock()
defer a.Unlock()
// 提取主域名
mainDomain := extractMainDomain(u.Host)
if _, exists := a.domainMap[mainDomain]; !exists {
a.domainMap[mainDomain] = make(cookieMap)
}
for _, cookie := range cookies {
if a.domainMap[mainDomain] == nil {
a.domainMap[mainDomain] = make(cookieMap)
}
a.domainMap[mainDomain][cookie.Name] = cookie
}
}
func (a *AllSavePersistCookieJar) Cookies(u *url.URL) []*http.Cookie {
a.RLock()
defer a.RUnlock()
// 提取主域名
mainDomain := extractMainDomain(u.Host)
if cookies, exists := a.domainMap[mainDomain]; exists {
result := make([]*http.Cookie, 0, len(cookies))
for _, cookie := range cookies {
result = append(result, cookie)
}
return result
}
return nil
}
func (a *AllSavePersistCookieJar) Serialize() string {
a.RLock()
defer a.RUnlock()
json, err := sonic.Marshal(a.domainMap)
utils.OkOrPanic(err)
return string(json)
}
func (a *AllSavePersistCookieJar) Deserialize(content string) {
a.Lock()
defer a.Unlock()
if content == "" {
a.domainMap = make(domainMap)
return
}
var domainMap domainMap
err := sonic.Unmarshal([]byte(content), &domainMap)
utils.OkOrPanic(err)
a.domainMap = domainMap
}
func (a *AllSavePersistCookieJar) SerializeBytes() []byte {
a.RLock()
defer a.RUnlock()
ret := make([]byte, 0)
retWriter := bytes.NewBuffer(ret)
err := gob.NewEncoder(retWriter).
Encode(a.domainMap)
utils.OkOrPanic(err)
return retWriter.Bytes()
}
func (a *AllSavePersistCookieJar) DeserializeBytes(content []byte) {
a.Lock()
defer a.Unlock()
if len(content) == 0 {
a.domainMap = make(domainMap)
return
}
retReader := bytes.NewBuffer(content)
var domainMap domainMap
err := gob.NewDecoder(retReader).
Decode(&domainMap)
utils.OkOrPanic(err)
a.domainMap = domainMap
}
func (a *AllSavePersistCookieJar) DeserializeReader(reader io.Reader) {
a.Lock()
defer a.Unlock()
var domainMap domainMap
err := gob.NewDecoder(reader).
Decode(&domainMap)
utils.OkOrPanic(err)
a.domainMap = domainMap
}
+2 -1
View File
@@ -10,7 +10,8 @@ import (
)
var Xuexitong = resty.New().
SetCookieJar(cookiejar.NewPersistCookieJar()).
SetCookieJar(cookiejar.NewAllSavePersistCookieJar()).
SetRedirectPolicy(resty.FlexibleRedirectPolicy(20)).
SetTimeout(30 * time.Second).
SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
@@ -0,0 +1,86 @@
package xuexitong
import (
"fmt"
"io"
"likedream-goframe/internal/utils"
"log"
"math/rand"
"os"
"strings"
"time"
"resty.dev/v3"
)
var randChar []string = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", "E", "F"}
func ApiVerifyCode(client *resty.Client, retry int) (string, error) {
if retry < 0 {
return "", fmt.Errorf("retry limit exceeded")
}
urlStr := "https://mooc1-api.chaoxing.com/processVerifyPng.ac?t=" + fmt.Sprintf("%d", time.Now().UnixMilli())
req := client.R()
req.Header.Add("User-Agent", GetUA("mobile"))
req.Header.Add("Accept", "*/*")
req.Header.Add("Host", "mooc1-api.chaoxing.com")
req.Header.Add("Connection", "keep-alive")
res, err := req.Get(urlStr)
if err != nil {
if retry > 0 {
return ApiVerifyCode(client, retry-1)
} else {
return res.String(), err
}
}
codeFileName := "code" + randChar[rand.Intn(len(randChar))] //生成验证码文件名称
for i := 0; i < 10; i++ {
codeFileName += randChar[rand.Intn(len(randChar))]
}
codeFileName += ".png"
utils.PathExistForCreate("./assets/code/") //检测是否存在路径,如果不存在则创建
filepath := fmt.Sprintf("./assets/code/%s", codeFileName)
file, err := os.Create(filepath)
if err != nil {
res.Body.Close() //立即释放
log.Println(err)
return "", err
}
_, err = io.Copy(file, res.Body)
if err != nil {
res.Body.Close() //立即释放
log.Println(err)
return "", err
}
file.Close()
if utils.IsBadImg(filepath) {
res.Body.Close() //立即释放
utils.DeleteFile(filepath) //删除坏的文件
return ApiVerifyCode(client, retry-1)
}
return filepath, nil
}
func ApiPassVerifyCode(client *resty.Client, code string, retry int) bool {
if retry < 0 {
return false
}
urlStr := "https://mooc1-api.chaoxing.com/html/processVerify.ac"
client.SetRedirectPolicy(resty.NoRedirectPolicy())
req := client.R()
req.SetMultipartField("ucode", "", "", strings.NewReader(code))
req.SetMultipartField("app", "", "", strings.NewReader("0"))
req.Header.Add("User-Agent", GetUA("mobile"))
req.Header.Add("Accept", "*/*")
req.Header.Add("Host", "mooc1-api.chaoxing.com")
req.Header.Add("Connection", "keep-alive")
res, err := req.Post(urlStr)
if err != nil {
return ApiPassVerifyCode(client, code, retry-1)
}
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20))
return res.StatusCode() == 302
}
@@ -2,11 +2,15 @@ package xuexitong
import (
"context"
"errors"
"likedream-goframe/internal/application/recognizer/ddddocr"
"likedream-goframe/internal/application/xuexitong/entity"
"likedream-goframe/internal/framework/logger"
"likedream-goframe/internal/utils"
"strings"
"github.com/bytedance/sonic"
ort "github.com/yalue/onnxruntime_go"
"resty.dev/v3"
)
@@ -39,6 +43,26 @@ func (c *XuexitongCourse) PullCourseList(ctx context.Context, retry int) (course
if strings.Contains(body, "请输入验证码") {
logger.Logger.Warn("触发验证码,正在进行AI智能识别绕过.....")
//todo ddddocr识别
succ := false
for i := 0; i < 5; i++ {
codePath, err := ApiVerifyCode(c.client, 5)
if err != nil {
return nil, err
}
if codePath == "" { //如果path为空,那么可能是账号问题
return nil, errors.New("无法正常获取学习通验证码,请检查对应url是否正常")
}
img, _ := utils.ReadImg(codePath) //读取验证码图片
codeResult := ddddocr.SemiOCRVerification(img, ort.NewShape(1, 23))
utils.DeleteFile(codePath) //删除验证码文件、
if ApiPassVerifyCode(c.client, codeResult, 5) {
succ = true
break
}
}
if !succ {
return nil, errors.New("AI验证码识别失败,请检查对应url是否正常")
}
}
var courseResult entity.XueXiTCourseJson
err = sonic.Unmarshal([]byte(body), &courseResult)
@@ -10,5 +10,9 @@ func TestXuexitongLogin(t *testing.T) {
xuexitongLogin := NewXuexitongLogin(context.Background())
err := xuexitongLogin.Login(context.Background(), "13551503012", "xiaozhi520", 5)
utils.OkOrPanic(err)
t.Log(xuexitongLogin.GetCookieString())
client := xuexitongLogin.GetClient()
courseAction := NewXuexitongCourse(client)
list, err := courseAction.PullCourseList(context.Background(), 5)
utils.OkOrPanic(err)
t.Log(list)
}
+67
View File
@@ -0,0 +1,67 @@
package utils
import (
"fmt"
"image"
"image/png"
"os"
)
// ReadImg 从文件读取imgage
func ReadImg(imgFile string) (image.Image, error) {
f, err := os.Open(imgFile)
if err != nil {
return nil, err
}
img, err := png.Decode(f)
if err != nil {
return nil, err
}
f.Close()
return img, nil
}
// IsBadImg 检测图片是否损坏,损坏为true,没损坏为false
func IsBadImg(imgFile string) bool {
f, err := os.Open(imgFile)
defer f.Close()
if err != nil {
return true
}
_, err1 := png.Decode(f)
if err1 != nil {
return true
}
return false
}
// PathExistForCreate 检测目录是否存在,不存在就创建
func PathExistForCreate(path string) {
exists, _ := PathExists(path)
if !exists {
os.MkdirAll(path, os.ModePerm)
}
}
// PathExists 检测文件夹或文件是否存在
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// DeleteFile 删除文件
func DeleteFile(path string) {
// 删除文件
err := os.Remove(path)
if err != nil {
fmt.Println("Error:", err)
return
}
}