-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.go
78 lines (64 loc) · 1.64 KB
/
login.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
package parser
import (
"html"
"regexp"
"strconv"
"github.com/pkg/errors"
)
type LoginForm struct {
Logged bool
Action string
Question string
}
func ParseLoginHTML(data []byte) (*LoginForm, error) {
var logged bool
var action string
logged, _ = regexp.Match(`Logout|登出`, data)
{
regex := regexp.MustCompile(`(?i)<form action="([^"]+)"`)
match := regex.FindSubmatch(data)
if len(match) == 0 {
return nil, errors.New("no form action has been found")
}
action = html.UnescapeString(string(match[1]))
}
question, err := ParseQuestion(data)
if err != nil {
return nil, errors.WithStack(err)
}
return &LoginForm{
Logged: logged,
Action: action,
Question: question,
}, nil
}
func ParseQuestion(data []byte) (string, error) {
regex := regexp.MustCompile(`\d+(?:\+|+| 加 )\d+(?:=|=| 等於)`)
match := regex.Find(data)
if len(match) == 0 {
return "", errors.New("no question has been found")
}
return string(match), nil
}
func AnswerQuestion(question string) (string, error) {
regex := regexp.MustCompile(`(\d+)(\+|+| 加 )(\d+)(?:=|=| 等於)`)
match := regex.FindStringSubmatch(question)
if len(match) == 0 {
return "", errors.Errorf("failed to parse question: %s", question)
}
left, err := strconv.Atoi(match[1])
if err != nil {
return "", errors.Wrap(err, "failed to parse question")
}
right, err := strconv.Atoi(match[3])
if err != nil {
return "", errors.Wrap(err, "failed to parse question")
}
operator := match[2]
switch operator {
case "+", "+", " 加 ":
return strconv.Itoa(left + right), nil
default:
return "", errors.Errorf("unreconized operator: %s", operator)
}
}