/
wanneng.go
98 lines (89 loc) · 2.53 KB
/
wanneng.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package search
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/itihey/tikuAdapter/pkg/errors"
"github.com/itihey/tikuAdapter/pkg/logger"
"github.com/itihey/tikuAdapter/pkg/model"
"strconv"
"strings"
"time"
)
type result struct {
Group string `json:"group"`
Num int `json:"num"`
Answers []interface{} `json:"answers"`
Success bool `json:"success"`
}
type wapiResponse struct {
Timestamp int64 `json:"timestamp"`
Code int `json:"code"`
Message string `json:"message"`
Result result `json:"result"`
}
// WannengClient icodef题库
type WannengClient struct {
Enable bool
Token string
}
// getHTTPClient 获取HTTP客户端
func (in *WannengClient) getHTTPClient() *resty.Client {
return resty.New().
SetTimeout(5*time.Second).
SetRetryCount(1).
SetRetryWaitTime(1*time.Second).
AddRetryCondition(func(r *resty.Response, err error) bool {
return err != nil || strings.Contains(r.String(), "已限流,正在重新请求...")
}).
SetRetryMaxWaitTime(10*time.Second).
AddRetryHook(func(r *resty.Response, err error) {
logger.SysError(fmt.Sprintf("万能免费题库触发流控限制,正在重试...%s", r.String()))
}).
SetHeader("Content-Type", "application/json")
}
// SearchAnswer 搜索答案
func (in *WannengClient) SearchAnswer(req model.SearchRequest) (answer [][]string, err error) {
answer = make([][]string, 0)
if !in.Enable {
return answer, nil
}
url := "http://lyck6.cn/scriptService/api/autoFreeAnswer"
if in.Token != "" && len(in.Token) == 10 {
url = "http://lyck6.cn/scriptService/api/autoAnswer/" + in.Token
}
resp, err := in.getHTTPClient().R().
SetBody(req).
SetHeader("plat", strconv.Itoa(int(req.Plat))).
Post(url)
if err != nil || resp.StatusCode() != 200 {
return nil, errors.ErrTargetServerError
}
var res wapiResponse
err = json.Unmarshal(resp.Body(), &res)
if err != nil {
return nil, errors.ErrTargetServerError
}
// 不等于0就是请求失败
if res.Code != 0 {
if res.Code == 429 {
return nil, errors.ErrTargetAPIFlow
}
return nil, errors.ErrTargetServerError
}
if res.Result.Success {
var as []string
for _, v := range res.Result.Answers {
as = append(as, req.Options[(int)(v.(float64))])
}
return [][]string{as, as, as, as, as, as, as, as, as, as}, nil
}
for _, ans := range res.Result.Answers {
var innerArray []string
for _, val := range ans.([]interface{}) {
innerArray = append(innerArray, val.(string))
}
answer = append(answer, innerArray)
}
return answer, nil
}