2026-03-13 20:43:44 +08:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-03-16 13:40:53 +08:00
|
|
|
"schedule_converter/client"
|
|
|
|
|
"schedule_converter/config"
|
2026-03-13 20:43:44 +08:00
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
|
|
|
|
|
fmt.Println("[2] 获取验证码...")
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", config.BaseURL+"/captchaImage", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
req.Header = client.GetHeaders()
|
|
|
|
|
|
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := os.WriteFile("captcha.png", body, 0644); err != nil {
|
|
|
|
|
fmt.Printf(" 警告: 无法保存验证码图片: %v\n", err)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(" 验证码已保存到: captcha.png")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Recognize(imagePath string) (string, error) {
|
|
|
|
|
fmt.Println("[3] 使用固定验证码...")
|
|
|
|
|
return "....", nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 13:40:53 +08:00
|
|
|
func GetAndRecognize(httpClient *http.Client) (string, error) {
|
|
|
|
|
_, err := SaveCaptchaImage(httpClient)
|
2026-03-13 20:43:44 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
captchaCode, err := Recognize("captcha.png")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf(" 识别结果: %s\n", captchaCode)
|
|
|
|
|
return captchaCode, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseHTML(html string) (*goquery.Document, error) {
|
|
|
|
|
return goquery.NewDocumentFromReader(strings.NewReader(html))
|
|
|
|
|
}
|