Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bvp committed Feb 16, 2021
1 parent 2a681f3 commit 8d7adad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
61 changes: 31 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"regexp"
"strconv"

// "io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
Expand Down Expand Up @@ -76,7 +75,7 @@ func NewClient(login string, password string, schoolID int64, httpClient *http.C
Password: password,
SchoolID: schoolID,
http: httpClient,
currentInfo: ci,
CurrentInfo: ci,
}

return cli
Expand Down Expand Up @@ -122,39 +121,39 @@ func (cli *Client) Login() (err error) {
return
}

err = cli.GetCurrentInfo()
err = cli.getCurrentInfo()

return
}

// GetCurrentInfo for session
func (cli *Client) GetCurrentInfo() (err error) {
// getCurrentInfo for session
func (cli *Client) getCurrentInfo() (err error) {
resp, err := cli.http.Get(urlHomework)
doc, err := goquery.NewDocumentFromResponse(resp)

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

classIDText, _ := doc.Find("body").Attr("onload")
if classIDText != "" {
classIDText = strings.TrimRight(strings.TrimLeft(classIDText, "loadSubjects('/ajax/subj/"), "', true)")
}
classID, err := strconv.ParseInt(classIDText, 10, 32)
cli.currentInfo.ClassID = classID
cli.CurrentInfo.ClassID = classID

var eys, eye int64
eyr := strings.Split(strings.TrimRight(doc.Find("#eduyear > #curedy").Text(), " учебный год"), "-")
eys, _ = strconv.ParseInt(eyr[0], 10, 32)
eye, _ = strconv.ParseInt(eyr[1], 10, 32)
cli.currentInfo.EduYearStart = int(eys)
cli.currentInfo.EduYearEnd = int(eye)
cli.CurrentInfo.EduYearStart = int(eys)
cli.CurrentInfo.EduYearEnd = int(eye)

cli.ToJSON(cli.currentInfo)
cli.ToJSON(cli.CurrentInfo)

return
}
Expand Down Expand Up @@ -186,7 +185,7 @@ func (cli *Client) SetCookie(name, value string) {
var cookies []*http.Cookie
cookies = append(cookies, cookie)
cli.http.Jar.SetCookies(u, cookies)
cli.GetCurrentInfo()
cli.getCurrentInfo()
}

// GetRegions to get client regions
Expand Down Expand Up @@ -237,7 +236,7 @@ func GetSchools(region int64) (schools []School, err error) {

// GetCourses to get subjects
func (cli *Client) GetCourses() (courses []Course, err error) {
resp, err := cli.http.Get(fmt.Sprintf("%s/subj/%d", urlAjax, cli.currentInfo.ClassID))
resp, err := cli.http.Get(fmt.Sprintf("%s/subj/%d", urlAjax, cli.CurrentInfo.ClassID))
if err != nil {
return
}
Expand Down Expand Up @@ -274,9 +273,9 @@ func (cli *Client) GetMarksPeriods() (periods []Lperiod, err error) {
title := strings.TrimSpace(s2.Text())
value, _ := s2.Attr("value")
period := Lperiod{
SchoolID: cli.currentInfo.SchoolID,
SYear: cli.currentInfo.EduYearStart,
EYear: cli.currentInfo.EduYearEnd,
SchoolID: cli.CurrentInfo.SchoolID,
SYear: cli.CurrentInfo.EduYearStart,
EYear: cli.CurrentInfo.EduYearEnd,
Name: title,
Period: value,
}
Expand All @@ -288,16 +287,16 @@ func (cli *Client) GetMarksPeriods() (periods []Lperiod, err error) {

// GetMarksCurrent to get marks for current month
func (cli *Client) GetMarksCurrent() (marks []Mark, err error) {
return cli.GetMarksForMonthWithType("", Note)
return cli.GetMarksForWithType("", Note)
}

// GetMarksForMonth to get marks for specific month
func (cli *Client) GetMarksForMonth(p string) (marks []Mark, err error) {
return cli.GetMarksForMonthWithType(p, Note)
// GetMarksFor to get marks for specific month
func (cli *Client) GetMarksFor(p string) (marks []Mark, err error) {
return cli.GetMarksForWithType(p, Note)
}

// GetMarksForMonthWithType to get user marks
func (cli *Client) GetMarksForMonthWithType(p string, t MarksListType) (marks []Mark, err error) {
// GetMarksForWithType to get user marks
func (cli *Client) GetMarksForWithType(p string, t MarksListType) (marks []Mark, err error) {
var sp string
if p != "" {
sp = fmt.Sprintf("%s/%s/", p, t.String())
Expand All @@ -314,6 +313,8 @@ func (cli *Client) GetMarksForMonthWithType(p string, t MarksListType) (marks []
if err != nil {
return
}
rpt := regexp.MustCompile(`(\r\n)+|\r+|\n+|\t+|\s+`)
log.Printf("page title - %s", rpt.ReplaceAllString(doc.Find("#content > h3").First().Text(), " "))
switch t {
case Note:
doc.Find("#marks > div.week").Each(func(i int, s *goquery.Selection) {
Expand All @@ -322,8 +323,8 @@ func (cli *Client) GetMarksForMonthWithType(p string, t MarksListType) (marks []
table := s2.Find("table")
table.Find("tbody > tr").Each(func(k int, tr *goquery.Selection) {
mark := Mark{}
mark.SYear = cli.currentInfo.EduYearStart
mark.EYear = cli.currentInfo.EduYearEnd
mark.SYear = cli.CurrentInfo.EduYearStart
mark.EYear = cli.CurrentInfo.EduYearEnd
mark.UserID = cli.Username
mark.SchoolID = cli.SchoolID
pd := strings.Split(strings.TrimRight(title, ")"), " (")
Expand Down Expand Up @@ -353,8 +354,8 @@ func (cli *Client) GetMarksForMonthWithType(p string, t MarksListType) (marks []
s.Find("span.mark").Each(func(j int, sj *goquery.Selection) {
if !sj.HasClass("avg") {
mark := Mark{}
mark.SYear = cli.currentInfo.EduYearStart
mark.EYear = cli.currentInfo.EduYearEnd
mark.SYear = cli.CurrentInfo.EduYearStart
mark.EYear = cli.CurrentInfo.EduYearEnd
mark.CourseName = courseName
el := sj.Find("a").First()
onClick, _ := el.Attr("onclick")
Expand Down Expand Up @@ -393,8 +394,8 @@ func (cli *Client) GetMarksFinal() (marks []Mark, err error) {

s.Find(".mark").Each(func(j int, sj *goquery.Selection) {
mark := Mark{}
mark.SYear = cli.currentInfo.EduYearStart
mark.EYear = cli.currentInfo.EduYearEnd
mark.SYear = cli.CurrentInfo.EduYearStart
mark.EYear = cli.CurrentInfo.EduYearEnd
mark.UserID = cli.Username
mark.SchoolID = cli.SchoolID
mark.CourseID, _ = strconv.ParseInt(courseID, 10, 32)
Expand Down Expand Up @@ -550,7 +551,7 @@ func (cli *Client) GetHomework() (hws []Homework, err error) {
return
}

err = cli.GetCurrentInfo()
err = cli.getCurrentInfo()
if err != nil {
return
}
Expand Down
14 changes: 9 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"sort"
"strings"
"testing"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestClient_Login(t *testing.T) {
if err != nil {
t.Error(err.Error())
}
t.Logf("client.GetCurrentInfo() - %#v", client.currentInfo)
t.Logf("client.getCurrentInfo() - %#v", client.CurrentInfo)
}

func TestClient_GetRegions(t *testing.T) {
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestClient_GetMarksPeriods(t *testing.T) {
}

func TestClient_GetMarksNote(t *testing.T) {
marks, _ := client.GetMarksForMonthWithType(Month1.String(), Note)
marks, _ := client.GetMarksForWithType(Month1.String(), Note)
t.Logf(":: size - %d", len(marks))
if DEBUG {
for _, m := range marks {
Expand All @@ -89,9 +90,12 @@ func TestClient_GetMarksNote(t *testing.T) {
}

func TestClient_GetMarksList(t *testing.T) {
marks, _ := client.GetMarksForMonthWithType(Month1.String(), List)
marks, _ := client.GetMarksForWithType(Month2.String(), List)
//marks, _ := client.GetMarksForWithType("edurng7639", List)
t.Logf(":: size - %d", len(marks))
if DEBUG {
//sort.Slice(marks, func(i, j int) bool { return marks[i].Date.After(marks[j].Date) })
sort.Sort(sort.Reverse(MarksByDate(marks)))
for _, m := range marks {
t.Logf("%s: %s - %s", m.Date.Format("2006.01.02"), m.CourseName, arrayToString(m.Grade, ","))
}
Expand Down Expand Up @@ -162,14 +166,14 @@ func TestClient_GetCourses(t *testing.T) {
t.Logf(":: size for 2020 - %d", len(courses))

client.SetCookie("edu_year", "2019")
client.GetCurrentInfo()
client.getCurrentInfo()
client.GetMarksPeriods()
courses2019, _ := client.GetCourses()
courses = append(courses, courses2019...)
t.Logf(":: size for 2019 - %d", len(courses2019))

client.SetCookie("edu_year", "2018")
client.GetCurrentInfo()
client.getCurrentInfo()
client.GetMarksPeriods()
courses2018, _ := client.GetCourses()
courses = append(courses, courses2018...)
Expand Down
12 changes: 9 additions & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Client struct {
SchoolID int64 `json:"schoolId" xorm:"'school_id'"`
Token string `json:"token"`
http *http.Client `xorm:"-"`
currentInfo CurrentInfo `xorm:"-"`
CurrentInfo CurrentInfo `xorm:"-"`
}

// CurrentInfo struct
Expand Down Expand Up @@ -105,8 +105,8 @@ func (p Lperiod) String() string {
type Mark struct {
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
UserID string `json:"userId" xorm:"'user_id'"`
SchoolID int64 `json:"schoolId" xorm:"'school_id'"`
CourseID int64 `json:"courseId" xorm:"'course_id'"`
SchoolID int64 `json:"school_id" xorm:"'school_id'"`
CourseID int64 `json:"course_id" xorm:"'course_id'"`
CourseName string `json:"courseName"`
Subject string `json:"subject"`
HomeWork string `json:"homework"`
Expand All @@ -128,6 +128,12 @@ func (m Mark) String() string {
return string(out)
}

type MarksByDate []Mark

func (a MarksByDate) Len() int { return len(a) }
func (a MarksByDate) Less(i, j int) bool { return a[i].Date.Before(a[j].Date) }
func (a MarksByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// Message struct
type Message struct {
ID int64 `json:"id" xorm:"pk 'id'"`
Expand Down

0 comments on commit 8d7adad

Please sign in to comment.