Skip to content

Commit

Permalink
delete: github.com/djimenez/iconv-go
Browse files Browse the repository at this point in the history
  • Loading branch information
SchwarzSail committed Sep 26, 2024
1 parent 59c09fa commit 5d834c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions calendar.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package jwch

import (
"github.com/west2-online/jwch/utils"
"regexp"
"strings"

"github.com/antchfx/htmlquery"
iconv "github.com/djimenez/iconv-go"
"github.com/west2-online/jwch/constants"
)

Expand All @@ -17,7 +17,7 @@ func (s *Student) GetSchoolCalendar() (*SchoolCalendar, error) {
}

rawCurTerm := htmlquery.InnerText(htmlquery.FindOne(resp, `//html/body/center/div`))
rawCurTerm, _ = iconv.ConvertString(rawCurTerm, "gb2312", "utf-8")
rawCurTerm, err = utils.ConvertGB2312ToUTF8([]byte(rawCurTerm))
curTermRegex := regexp.MustCompile(`当前学期:(\d{6})`)
curTerm := curTermRegex.FindStringSubmatch(rawCurTerm)[1]

Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *Student) GetTermEvents(termId string) (*CalTermEvents, error) {

rawTermDetail := htmlquery.InnerText(htmlquery.FindOne(resp, `/html/body/table[2]/tbody/tr`))
rawTermDetail = strings.ReplaceAll(rawTermDetail, " ", " ")
rawTermDetail, _ = iconv.ConvertString(rawTermDetail, "gb2312", "utf-8")
rawTermDetail, _ = utils.ConvertGB2312ToUTF8([]byte(rawTermDetail))

res := &CalTermEvents{
TermId: termId,
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/west2-online/jwch

go 1.20
go 1.22

require (
github.com/antchfx/htmlquery v1.3.0
Expand All @@ -10,7 +10,6 @@ require (

require (
github.com/antchfx/xpath v1.2.4 // indirect
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
golang.org/x/text v0.14.0 // indirect
)
16 changes: 16 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"encoding/json"
"fmt"
"golang.org/x/net/html"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -138,3 +141,16 @@ func SafeAtoi(s string) int {

return n
}

func ConvertGB2312ToUTF8(input []byte) (string, error) {
// 使用 transform.NewReader 进行编码转换
reader := transform.NewReader(bytes.NewReader(input), simplifiedchinese.GB18030.NewDecoder())

// 将转换后的结果读取为 UTF-8 字符串
utf8Bytes, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}

return string(utf8Bytes), nil
}

0 comments on commit 5d834c9

Please sign in to comment.