/
GetGrade.go
164 lines (151 loc) · 4.54 KB
/
GetGrade.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
"time"
)
type GradeStruct struct {
CourseID string
CourseName string
CourseTerm string
CourseCredit string
CourseGrade string
GradePoint string
}
//TODO:Fill Struct Array
func GetGrade(UserName, PassWord string) string {
// 获取用户名和密码
conf := ReadConfig()
USERNAME := UserName
PASSWORD := PassWord
var grades []GradeStruct
var myGrade GradeStruct
var gradeResult GradeResult
// Cookie自动维护
cookieJar, err := cookiejar.New(nil)
gradeResult.Type = "grade"
if err != nil {
fmt.Println("ERROR_0: ", err.Error())
//return
}
var client http.Client
client.Jar = cookieJar
// 第一次请求
req, err := http.NewRequest(http.MethodGet, conf.MangerURL+"eams/login.action", nil)
if err != nil {
fmt.Println("ERROR_1: ", err.Error())
//return
}
resp1, err := client.Do(req)
if err != nil {
fmt.Println("ERROR_2: ", err.Error())
//return
}
defer resp1.Body.Close()
content, err := ioutil.ReadAll(resp1.Body)
if err != nil {
fmt.Println("ERROR_3: ", err.Error())
//return
}
temp := string(content)
if !strings.Contains(temp, "CryptoJS.SHA1(") {
fmt.Println("ERROR_4: GET Failed")
//return
}
// 对密码进行SHA1哈希
temp = temp[strings.Index(temp, "CryptoJS.SHA1(")+15 : strings.Index(temp, "CryptoJS.SHA1(")+52]
PASSWORD = temp + PASSWORD
bytes := sha1.Sum([]byte(PASSWORD))
PASSWORD = hex.EncodeToString(bytes[:])
formValues := make(url.Values)
formValues.Set("username", USERNAME)
formValues.Set("password", PASSWORD)
formValues.Set("session_locale", "zh_CN")
time.Sleep(time.Duration(1000 * time.Millisecond))
req, err = http.NewRequest(http.MethodPost, conf.MangerURL+"eams/login.action", strings.NewReader(formValues.Encode()))
if err != nil {
fmt.Println("ERROR_5: ", err.Error())
//return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0")
resp2, err := client.Do(req)
if err != nil {
fmt.Println("ERROR_6: ", err.Error())
//return
}
defer resp2.Body.Close()
content, err = ioutil.ReadAll(resp2.Body)
if err != nil {
fmt.Println("ERROR_7: ", err.Error())
//return
}
temp = string(content)
if !strings.Contains(temp, "<a href=\"/eams/security/my.action\" target=\"_blank\" title=\"查看详情\" style=\"color:#ffffff\">") {
fmt.Println(temp)
fmt.Println("ERROR_8: LOGIN Failed")
//return
}
time.Sleep(1000 * time.Millisecond)
req, err = http.NewRequest(http.MethodPost, conf.MangerURL+"eams/teach/grade/course/person!historyCourseGrade.action?projectType=MAJOR", strings.NewReader(formValues.Encode()))
if err != nil {
fmt.Println("ERROR_13: ", err.Error())
//return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0")
resp4, err := client.Do(req)
if err != nil {
fmt.Println("ERROR_14: ", err.Error())
//return
}
defer resp4.Body.Close()
content, err = ioutil.ReadAll(resp4.Body)
if err != nil {
fmt.Println("ERROR_15: ", err.Error())
//return
}
temp = string(content)
reg3 := regexp.MustCompile(`(?i)<tr>[\s\S]*?</tr>`)
reg4 := regexp.MustCompile(`(?i)<td.*>([^>]*)</td>`)
reg5 := regexp.MustCompile(`(?i)<sup.*>([^>]*)</sup>`)
gradeStr := reg3.FindAllStringSubmatch(temp, -1)
gradeStr = append(gradeStr[:0], gradeStr[0+1:]...)
gradeStr = append(gradeStr[:0], gradeStr[0+1:]...)
for _, tempStr := range gradeStr {
fuck := reg4.FindAllStringSubmatch(tempStr[0], -1)
myGrade.CourseTerm = strings.Trim(fuck[0][1], "\n")
myGrade.CourseID = strings.Trim(fuck[1][1], "\n")
myGrade.CourseCredit = strings.Trim(fuck[4][1], "\n")
bodyclass := reg5.FindAllStringSubmatch(tempStr[0], -1)
if len(bodyclass) != 0 {
myGrade.CourseName = bodyclass[0][1]
} else {
myGrade.CourseName = strings.Trim(fuck[3][1], "\t\r\n")
}
myGrade.CourseGrade = strings.Trim(fuck[len(fuck)-2][1], "\t\n")
myGrade.GradePoint = strings.Trim(fuck[len(fuck)-1][1], "\t\n")
grades = append(grades, myGrade)
}
req, err = http.NewRequest(http.MethodGet, conf.MangerURL+"eams/logout.action", nil)
if err != nil {
fmt.Println("ERROR_17: ", err.Error())
}
resp5, err := client.Do(req)
if err != nil {
fmt.Println("ERROR_18: ", err.Error())
//return
}
defer resp5.Body.Close()
gradeResult.Data = grades
js, err := json.MarshalIndent(gradeResult, "", "\t")
return B2S(js)
}