Skip to content

Commit

Permalink
v0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
bvp committed Oct 5, 2023
1 parent 6cc17d8 commit 213b0c9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.idea
.vscode

config_test.json
config_test*.json
*~
*.bak
*.dll
Expand Down
35 changes: 14 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ package dnevnik76
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"regexp"
"strconv"
Expand Down Expand Up @@ -47,7 +46,7 @@ var (
func NewClient(login string, password string, schoolID int64, httpClient *http.Client) *Client {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
log.Fatalf("NewClient error - %s", err)
}

ci := CurrentInfo{}
Expand Down Expand Up @@ -142,12 +141,11 @@ func (cli *Client) getCurrentInfo() (err error) {
}

cli.CurrentInfo.SchoolID = cli.SchoolID
classNumber, classChar, err := getClassName(doc.Find("#auth_info > #role").Text())
class, err := getClassName(doc.Find("#auth_info > #role").Text())
if err != nil {
return
}
cli.CurrentInfo.ClassNumber = classNumber
cli.CurrentInfo.ClassChar = classChar
cli.CurrentInfo.Class = class

classIDText, _ := doc.Find("body").Attr("onload")
if classIDText != "" {
Expand Down Expand Up @@ -260,6 +258,11 @@ func (cli *Client) GetCurrentQuarter() (result string) {
result = p.Period
break
}
} else {
if strings.Contains(p.Name, "полугодие") {
result = p.Period
break
}
}
}
return
Expand Down Expand Up @@ -496,7 +499,7 @@ func (cli *Client) GetMessagesCount() (unread int, total int, err error) {
return
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
Expand Down Expand Up @@ -575,20 +578,10 @@ func (cli *Client) GetMessage(msgID int64) (m Message, err error) {
return
}

func getClassName(s string) (classNumber int, classChar string, err error) {
re, err := regexp.Compile(`\s?\n\s+Учащийся\n\s+\((\d+) "(.)"\)\n\s+`)
if err != nil {
return
}
matches := re.FindStringSubmatch(s)
if len(matches) > 1 {
clsNum, _ := strconv.ParseInt(matches[1], 10, 32)
classNumber = int(clsNum)
classChar = re.FindStringSubmatch(s)[2]
} else {
return 0, "", errors.New("match size less or equal then 1")
}

func getClassName(s string) (class string, err error) {
re_inside_whtsp := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
final := re_inside_whtsp.ReplaceAllString(strings.TrimSpace(s), " ")
class = strings.TrimLeft(strings.TrimRight(final, ")"), "Учащийся (")
return
}

Expand Down
5 changes: 2 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dnevnik76
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
Expand Down Expand Up @@ -173,7 +172,7 @@ func TestClient_GetCourses(t *testing.T) {
courses, _ := client.GetCourses()
t.Logf(":: size - %d", len(courses))

years := []string{"2022", "2021", "2020", "2019", "2018"}
years := []string{"2023", "2022", "2021", "2020", "2019", "2018"}
for _, y := range years {
client.SetCookie("edu_year", y)
client.getCurrentInfo()
Expand Down Expand Up @@ -218,7 +217,7 @@ func TestClient_GetTeachers(t *testing.T) {

func setup() {
DEBUG = true
file, _ := ioutil.ReadFile("config_test.json")
file, _ := os.ReadFile("config_test.json")
cfg = config{}
_ = json.Unmarshal([]byte(file), &cfg)

Expand Down
3 changes: 1 addition & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type CurrentInfo struct {
//PersonID int64 `json:"personId" xorm:"'person_id'"`
SchoolID int64 `json:"schoolId" xorm:"'school_id'"`
ClassID int64 `json:"clsId" xorm:"'class_id'"`
ClassNumber int `json:"clsNum"`
ClassChar string `json:"clsChr"`
Class string `json:"cls"`
EduYearStart int `json:"eduYearStart"`
EduYearEnd int `json:"eduYearEnd"`
}
Expand Down

0 comments on commit 213b0c9

Please sign in to comment.