Commit 40923f7d authored by Junhiee's avatar Junhiee

gdhsxy

parents
Pipeline #4272 failed with stages
module gdhsxy
go 1.21.2
package hsxy
type Publish struct {
}
\ No newline at end of file
package hsxy
import (
"encoding/json"
"fmt"
"gdhsxy/utils"
"net/http"
"net/url"
"regexp"
"strings"
)
var client *http.Client
func init() {
proxyURL, err := url.Parse("http://192.168.31.40:7890")
if err != nil {
fmt.Println(err)
}
transport := &http.Transport{
// 设置其他参数
Proxy: http.ProxyURL(proxyURL),
}
client = &http.Client{
Transport: transport,
}
}
// 厚德楼时间表
const (
HDL_1 string = "8:20-9:05"
HDL_2 string = "9:10-9:55"
HDL_3 string = "10:25-11:10"
HDL_4 string = "11:15-12:00"
HDL_5 string = "14:00-14:45"
HDL_6 string = "14:50-15:35"
HDL_7 string = "16:00-16:45"
HDL_8 string = "16:50-17:35"
HDL_9 string = "18:45-19:30"
HDL_10 string = "19:40-20:25"
HDL_11 string = "20:35-21:20"
)
type HSXY interface {
Parse(string) *Schedule
Json() ([]byte, error)
Html(string)
Updata() (bool, error)
}
type Schedule struct {
name string
teacher string
time string
date string
addr string
}
func (s *Schedule) Parse(schedule map[string]string) *Schedule {
return &Schedule{
name: schedule["课程名称"],
teacher: schedule["教师"],
time: schedule["时间"],
date: schedule["周次小节"],
addr: schedule["地点"],
}
}
func (s *Schedule) Json() ([]byte, error) {
return json.Marshal(&s)
}
func (s *Schedule) Html(data string) (html string) {
return html
}
func (s *Schedule) Updata() (ok bool, err error) {
return ok, err
}
func parseURL(base_url string, params map[string]string) string {
param := url.Values{}
for key, value := range params {
param.Add(key, value)
}
uri := base_url + "?" + param.Encode()
return uri
}
func addHeaders(req *http.Request, headers map[string]string) {
for key, value := range headers {
req.Header.Add(key, value)
}
}
func BuildRequest(base_url string) *http.Request {
// base_url := "http://jwxt.gdhsc.edu.cn/jsxsd/framework/mainV_index_loadkb.htmlx"
params := map[string]string{
"xnxqid": "2023-2024-1", // 学年
"rq": "2023-10-01", // 周次
"xswk": "false", // 选课网址
"sjmsValue": "58B5870A5A8E431C95DA33D78BFAF1A8", // 非必要
}
headers := map[string]string{
"Accept": "text/html, */*; q=0.01",
"Accept-Language": "en,zh;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7",
"Cookie": "bzb_jsxsd=2D4BFEF24DD19538DA4FB5BD356181B9; SERVERID=123;",
"Proxy-Connection": "keep-alive",
"Referer": "http://jwxt.gdhsc.edu.cn/jsxsd/framework/xsMainV_new.htmlx?t1=1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
uri := parseURL(base_url, params)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
fmt.Println(err)
}
addHeaders(req, headers)
return req
}
func Fetch(req *http.Request) (*http.Response, error) {
resp, err := client.Do(req)
return resp, err
}
func RawParse(html string) []string {
re := regexp.MustCompile(`(?s)<tr align="center">(.*?)</tr>`)
matches := re.FindAllString(html, -1)
// 匹配字段
nameCompile := regexp.MustCompile(`<p>([^<]+)</p><div class='tch-name'>`)
teacherCompile := regexp.MustCompile(`</p><p>(.*?)</p><span class='text'>`)
dateCompile := regexp.MustCompile(`<span class='text'>(.*?)</span></span>`)
addrCompile := regexp.MustCompile(`<img src='/jsxsd/assets_v1/images/item1.png'>(.*?)</span><span>`)
weekCompile := regexp.MustCompile(`<img src='/jsxsd/assets_v1/images/item3.png'>(.*?)</span></div><span style='display:none;' name='wkxx'>`)
var result []string
for _, matche := range matches {
nameTemp := nameCompile.FindAllStringSubmatch(matche, -1)
teacherTemp := teacherCompile.FindAllStringSubmatch(matche, -1)
dateTemp := dateCompile.FindAllStringSubmatch(matche, -1)
addrTemp := addrCompile.FindAllStringSubmatch(matche, -1)
weekTemp := weekCompile.FindAllStringSubmatch(matche, -1)
if len(nameTemp) > 1 {
for i := 0; i < len(nameTemp); i++ {
result = append(
result,
nameTemp[i][1],
teacherTemp[i][1],
dateTemp[i][1],
addrTemp[i][1],
weekTemp[i][1],
)
}
}
}
return result
}
func ToMap() (result map[string][]map[string]string) {
var sTemp []string
var resTemp []map[string]string
var mTemp map[string]string
data, err := utils.ReadFile("./resource/schedule.html")
if err != nil {
fmt.Println(err)
}
sechdule := RawParse(string(data))
result = make(map[string][]map[string]string)
var time string
const key = "sch"
for i := 0; i <= len(sechdule); i++ {
if i%5 == 0 && i != 0 {
name := sTemp[0]
teacher := strings.Split(sTemp[1], ":")[1]
date := strings.Split(sTemp[4], " ")[1] + " " + sTemp[2]
addr := sTemp[3]
strs := strings.Split(sTemp[2], " ")[0]
switch strs {
case "01~02小节":
time = strings.Split(HDL_1, "-")[0] + "-" + strings.Split(HDL_2, "-")[1]
case "03~04小节":
time = strings.Split(HDL_3, "-")[0] + "-" + strings.Split(HDL_4, "-")[1]
case "05~06小节":
time = strings.Split(HDL_5, "-")[0] + "-" + strings.Split(HDL_6, "-")[1]
case "07~08小节":
time = strings.Split(HDL_7, "-")[0] + "-" + strings.Split(HDL_8, "-")[1]
case "09~10~11小节":
time = strings.Split(HDL_9, "-")[0] + "-" + strings.Split(HDL_11, "-")[1]
}
mTemp = map[string]string{
"课程名称": name,
"教师": teacher,
"时间": time,
"周次小节": date,
"地点": addr,
}
resTemp = append(resTemp, mTemp)
sTemp = []string{}
}
if i != 65 {
sTemp = append(sTemp, sechdule[i])
}
}
result[key] = resTemp
return result
}
package main
import (
"fmt"
"gdhsxy/hsxy"
)
func main() {
// TODO 初始化课程表登录信息 - 持久化
s := new(hsxy.Schedule)
schMap := hsxy.ToMap()["sch"]
fmt.Println(len(schMap))
for _, sch := range schMap {
result := s.Parse(sch)
fmt.Println(result)
}
}
This diff is collapsed.
This diff is collapsed.
package test
import (
"fmt"
"gdhsxy/utils"
"testing"
)
func TestReadFile(t *testing.T) {
data, err := utils.ReadFile("../resource/schedule.html")
fmt.Println(string(data), err)
}
\ No newline at end of file
package test
import (
// "encoding/json"
"fmt"
"gdhsxy/hsxy"
"gdhsxy/utils"
"io"
"regexp"
"strings"
"testing"
)
func TestFetch(t *testing.T) {
base_url := "http://jwxt.gdhsc.edu.cn/jsxsd/framework/mainV_index_loadkb.htmlx"
req := hsxy.BuildRequest(base_url)
// fmt.Println(req)
resp, err := hsxy.Fetch(req)
if err != nil {
fmt.Println(err)
}
body, err := io.ReadAll(resp.Body)
fmt.Println(string(body))
if err != nil {
fmt.Println(err)
}
}
func TestParse(t *testing.T) {
s := new(hsxy.Schedule)
sch := map[string]string{
"周次小节": "星期一 01~02小节 第15周",
"地点": "厚德楼C-厚德楼C303大金融实验室",
"教师": "钟广玲",
"课程名称": "Python程序设计",
}
fmt.Println(s.Parse(sch))
}
func TestParser(t *testing.T) {
data, err := utils.ReadFile("../resource/schedule.html")
if err != nil {
fmt.Println(err)
}
re := regexp.MustCompile(`(?s)<tr align="center">(.*?)</tr>`)
matches := re.FindAllString(string(data), -1)
// 匹配字段
nameCompile := regexp.MustCompile(`<p>([^<]+)</p><div class='tch-name'>`)
teacherCompile := regexp.MustCompile(`</p><p>(.*?)</p><span class='text'>`)
dateCompile := regexp.MustCompile(`<span class='text'>(.*?)</span></span>`)
addrCompile := regexp.MustCompile(`<img src='/jsxsd/assets_v1/images/item1.png'>(.*?)</span><span>`)
weekCompile := regexp.MustCompile(`<img src='/jsxsd/assets_v1/images/item3.png'>(.*?)</span></div><span style='display:none;' name='wkxx'>`)
var result []string
for _, matche := range matches {
nameTemp := nameCompile.FindAllStringSubmatch(matche, -1)
teacherTemp := teacherCompile.FindAllStringSubmatch(matche, -1)
dateTemp := dateCompile.FindAllStringSubmatch(matche, -1)
addrTemp := addrCompile.FindAllStringSubmatch(matche, -1)
weekTemp := weekCompile.FindAllStringSubmatch(matche, -1)
if len(nameTemp) > 1 {
for i := 0; i < len(nameTemp); i++ {
result = append(
result,
nameTemp[i][1],
teacherTemp[i][1],
dateTemp[i][1],
addrTemp[i][1],
weekTemp[i][1],
)
}
}
}
fmt.Println(result)
}
func TestMap(t *testing.T) {
var sTemp []string
var resTemp []map[string]string
var mTemp map[string]string
var result map[string][]map[string]string
data, err := utils.ReadFile("../resource/schedule.html")
if err != nil {
fmt.Println(err)
}
sechdule := hsxy.RawParse(string(data))
result = make(map[string][]map[string]string)
var time string
const key = "sch"
for i := 0; i <= len(sechdule); i++ {
if i%5 == 0 && i != 0 {
name := sTemp[0]
teacher := strings.Split(sTemp[1], ":")[1]
date := strings.Split(sTemp[4], " ")[1] + " " + sTemp[2]
addr := sTemp[3]
strs := strings.Split(sTemp[2], " ")[0]
switch strs {
case "01~02小节":
time = strings.Split(hsxy.HDL_1, "-")[0] + "-" + strings.Split(hsxy.HDL_2, "-")[1]
case "03~04小节":
time = strings.Split(hsxy.HDL_3, "-")[0] + "-" + strings.Split(hsxy.HDL_4, "-")[1]
case "05~06小节":
time = strings.Split(hsxy.HDL_5, "-")[0] + "-" + strings.Split(hsxy.HDL_6, "-")[1]
case "07~08小节":
time = strings.Split(hsxy.HDL_7, "-")[0] + "-" + strings.Split(hsxy.HDL_8, "-")[1]
case "09~10~11小节":
time = strings.Split(hsxy.HDL_9, "-")[0] + "-" + strings.Split(hsxy.HDL_11, "-")[1]
}
mTemp = map[string]string{
"课程名称": name,
"教师": teacher,
"时间": time,
"周次小节": date,
"地点": addr,
}
resTemp = append(resTemp, mTemp)
sTemp = []string{}
}
if i != 65 {
sTemp = append(sTemp, sechdule[i])
}
}
result[key] = resTemp
fmt.Println(len(resTemp))
// fmt.Println(result)
}
package utils
import (
"bufio"
_ "fmt"
"io"
"log"
"os"
)
func init() {
}
func ReadFile(path string) (data []byte, err error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var rd io.Reader = file
// var lines []string
reader := bufio.NewReader(rd)
// for {
// // ReadLine is a low-level line-reading primitive.
// // Most callers should use ReadBytes('\n') or ReadString('\n') instead or use a Scanner.
// bytes, _, err := reader.ReadLine()
// if err == io.EOF {
// break
// }
// if err != nil {
// return lines, err
// }
// lines = append(lines, string(bytes))
// // fmt.Println(string(bytes))
// }
// fmt.Println(lines)
data, err = io.ReadAll(reader)
return data, err
}
func WriteFile(data []byte) {
if _, err := io.WriteString(os.Stdout, "Hello World"); err != nil {
log.Fatal(err)
}
os.WriteFile("../resource/schedule.html", data, os.ModePerm)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment