diff --git a/Dockerfile b/Dockerfile index 66e2c6d..ff99892 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,7 @@ RUN apt-get -qq update && \ mkdir /opt/config/ COPY conf/config_default.yml /opt/config/config.yml +COPY conf/QuestionBank.db /opt/config/QuestionBank.db COPY ./dist/docker_linux_$TARGETARCH*/study_xxqg /opt/study_xxqg diff --git a/conf/QuestionBank.db b/conf/QuestionBank.db new file mode 100644 index 0000000..362aea9 Binary files /dev/null and b/conf/QuestionBank.db differ diff --git a/go.mod b/go.mod index 717cd9f..9feda1f 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ replace github.com/willf/bitset v1.2.1 => github.com/bits-and-blooms/bitset v1.2 require ( github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f - github.com/Luoxin/go-pushdeer-sdk v0.0.0-20220308071956-e50d1ec38c98 github.com/dustin/go-humanize v1.0.0 github.com/gin-gonic/gin v1.7.1 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0 @@ -36,7 +35,6 @@ require ( github.com/go-playground/locales v0.13.0 // indirect github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/validator/v10 v10.5.0 // indirect - github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/go-stack/stack v1.8.1 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect diff --git a/go.sum b/go.sum index 435c8c2..325e678 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f h1:2dk3eOnYllh+wUOuDhOoC2vUVoJF/5z478ryJ+wzEII= github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/go.mod h1:4a58ifQTEe2uwwsaqbh3i2un5/CBPg+At/qHpt18Tmk= -github.com/Luoxin/go-pushdeer-sdk v0.0.0-20220308071956-e50d1ec38c98 h1:88VEoycHG8Fecaf8h/Tq5FSP5Jjl1+yodHiPAJawDe0= -github.com/Luoxin/go-pushdeer-sdk v0.0.0-20220308071956-e50d1ec38c98/go.mod h1:ZO6lLdHMY7Hj7VDqvy+k+s5AQdkxK0KV08GU9CUuqR0= github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0= github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= @@ -24,8 +22,6 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-playground/validator/v10 v10.5.0 h1:X9rflw/KmpACwT8zdrm1upefpvdy6ur8d1kWyq6sg3E= github.com/go-playground/validator/v10 v10.5.0/go.mod h1:xm76BBt941f7yWdGnI2DVPFFg1UK3YY04qifoXU3lOk= -github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= -github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0 h1:mbEDV1g6RBzKd4sFjOWuyZdxItw4CWu5Kq4KaBAJbHM= @@ -113,7 +109,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qq github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= @@ -155,7 +150,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220111093109-d55c255bac03 h1:0FB83qp0AzVJm+0wcIlauAjJ+tNdh7jLuacRYCIVv7s= golang.org/x/net v0.0.0-20220111093109-d55c255bac03/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= @@ -164,7 +158,6 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/lib/respond.go b/lib/respond.go index 7b49686..bdeab7a 100644 --- a/lib/respond.go +++ b/lib/respond.go @@ -38,6 +38,11 @@ div > div.my-points-section > div.my-points-content > div:nth-child(6) > div.my- * @param model */ func (c *Core) RespondDaily(user *model.User, model string) { + + var title string + retryTimes := 0 + var id int + // 捕获所有异常,防止程序崩溃 defer func() { err := recover() @@ -135,7 +140,7 @@ func (c *Core) RespondDaily(user *model.User, model string) { //} // 获取每周答题的ID - id, err := getweekID(user.ToCookies()) + id, err = getweekID(user.ToCookies()) if err != nil { return } @@ -167,10 +172,11 @@ func (c *Core) RespondDaily(user *model.User, model string) { //} // 获取专项答题ID - id, err := getSpecialID(user.ToCookies()) - if err != nil { - return - } + //id, err = getSpecialID(user.ToCookies()) + //if err != nil { + // return + //} + id = 81 // 跳转到专项答题界面 _, err = page.Goto(fmt.Sprintf("https://pc.xuexi.cn/points/exam-paper-detail.html?id=%d", id), playwright.PageGotoOptions{ Referer: playwright.String(MyPointsUri), @@ -312,7 +318,15 @@ func (c *Core) RespondDaily(user *model.User, model string) { log.Errorln("获取题目问题失败" + err.Error()) return } + log.Infoln("## 题目:" + questionText) + if title == questionText { + log.Warningln("可能已经卡住,正在重试,重试次数+1") + retryTimes++ + } else { + retryTimes = 0 + } + title = questionText // 获取答题帮助 openTips, err := page.QuerySelector( @@ -349,6 +363,15 @@ func (c *Core) RespondDaily(user *model.User, model string) { // 从整个页面内容获取提示信息 tips := getTips(content) log.Infoln("[提示信息]:", tips) + + if retryTimes > 4 { + log.Warningln("重试次数太多,即将退出答题") + options, _ := getOptions(page) + c.Push(user.PushId, "flush", fmt.Sprintf( + "答题过程出现异常!!
答题渠道:%v
题目ID:%v
题目类型:%v
题目:%v
题目选项:%v
提示信息:%v
", model, id, categoryText, questionText, strings.Join(options, " "), strings.Join(tips, " "))) + return + } + // 填空题 switch { case strings.Contains(categoryText, "填空题"): @@ -579,7 +602,19 @@ func FillBlank(page playwright.Page, tips []string) error { video = true } if video { - answer = append(answer, "不知道") + data1, err := page.QuerySelector("#app > div > div.layout-body > div > div.detail-body > div.question > div.q-body > div > span:nth-child(1)") + if err != nil { + log.Errorln("获取题目前半段失败" + err.Error()) + return err + } + data1Text, _ := data1.TextContent() + log.Infoln("题目前半段:=》" + data1Text) + searchAnswer := model.SearchAnswer(data1Text) + if searchAnswer != "" { + answer = append(answer, searchAnswer) + } else { + answer = append(answer, "不知道") + } } else { answer = tips } @@ -599,11 +634,12 @@ func FillBlank(page playwright.Page, tips []string) error { } var ans string for i := 0; i < len(inouts); i++ { - if video { + if len(answer) < i+1 { ans = "不知道" } else { ans = answer[i] } + err := inouts[i].Fill(ans) if err != nil { log.Errorln("填充答案失败" + err.Error()) diff --git a/model/model.go b/model/model.go index 230cda5..761838a 100644 --- a/model/model.go +++ b/model/model.go @@ -2,6 +2,7 @@ package model import ( "database/sql" + "sync" _ "modernc.org/sqlite" @@ -10,6 +11,8 @@ import ( var ( db *sql.DB + + db1 *sql.DB ) func init() { @@ -36,6 +39,18 @@ func init() { `) } +func initQuestionDb() { + once := sync.Once{} + once.Do(func() { + var err error + db1, err = sql.Open("sqlite", "./config/QuestionBank.db") + if err != nil { + log.Errorln("题目数据库打开失败,请检查config目录权限") + log.Panicln(err.Error()) + } + }) +} + func ping() { err := db.Ping() if err != nil { diff --git a/model/question.go b/model/question.go new file mode 100644 index 0000000..c99bf6b --- /dev/null +++ b/model/question.go @@ -0,0 +1,27 @@ +package model + +import log "github.com/sirupsen/logrus" + +func SearchAnswer(title string) string { + initQuestionDb() + if db1 == nil { + return "" + } + var answer string + row := db1.QueryRow("select answer from tiku where question like ?", title+"%") + err := row.Scan(&answer) + if err != nil { + log.Errorln(err.Error()) + return "" + } + if answer == "" { + row := db1.QueryRow("select answer from tikuNet where question like ?", title+"%") + err := row.Scan(&answer) + if err != nil { + log.Errorln(err.Error()) + return "" + } + } + log.Infoln("从数据库查询到答案:" + answer) + return answer +} diff --git a/push/tg.go b/push/tg.go index 44076f4..008112c 100644 --- a/push/tg.go +++ b/push/tg.go @@ -424,11 +424,23 @@ func study(bot *Telegram, from int64, args []string) { defer datas.Delete(u) core.Init() defer core.Quit() - core.LearnArticle(user) - core.LearnVideo(user) - core.RespondDaily(user, "daily") - core.RespondDaily(user, "weekly") - core.RespondDaily(user, "special") + if config.Model == 1 { + core.LearnArticle(user) + core.LearnVideo(user) + } else if config.Model == 2 { + core.LearnArticle(user) + core.LearnVideo(user) + core.RespondDaily(user, "daily") + } else if config.Model == 3 { + core.LearnArticle(user) + core.LearnVideo(user) + core.RespondDaily(user, "daily") + core.RespondDaily(user, "weekly") + core.RespondDaily(user, "special") + } else if config.Model == 4 { + core.RespondDaily(user, "special") + } + c <- 1 }()