Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bvp committed Jan 26, 2021
1 parent b02a02f commit 2a681f3
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 110 deletions.
6 changes: 1 addition & 5 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// dnevnik76-api project doc.go

/*
dnevnik76-api document
*/
// Package dnevnik76 doc
package dnevnik76

/* Сообщения
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/bvp/dnevnik76-api
go 1.12

require (
github.com/PuerkitoBio/goquery v1.6.0
github.com/PuerkitoBio/goquery v1.6.1
github.com/andybalholm/cascadia v1.2.0 // indirect
github.com/bvp/russiantime v0.1.0
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb
golang.org/x/net v0.0.0-20210119194325-5f4716e94777
)
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
github.com/PuerkitoBio/goquery v1.6.0 h1:j7taAbelrdcsOlGeMenZxc2AWXD5fieT1/znArdnx94=
github.com/PuerkitoBio/goquery v1.6.0/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/PuerkitoBio/goquery v1.6.1 h1:FgjbQZKl5HTmcn4sKBgvx8vv63nhyhIpv7lJpFGCWpk=
github.com/PuerkitoBio/goquery v1.6.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
github.com/bvp/russiantime v0.1.0 h1:oXpuB2ooSlexQGJ+Vp0ydjQ2IPgiuqpVwxz3vBVclm0=
github.com/bvp/russiantime v0.1.0/go.mod h1:jALOcp8csKwzExdl0Yn4OagrU4PZ1YqJlJIayioeyCE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb h1:mUVeFHoDKis5nxCAzoAi7E8Ghb86EXh/RK6wtvJIqRY=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
63 changes: 7 additions & 56 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ func (cli *Client) Login() (err error) {
return
}
cli.Token, _ = doc.Find(".login__form > input[name='csrfmiddlewaretoken']").First().Attr("value")
if DEBUG {
log.Printf("csrfmiddlewaretoken - %s\n", cli.Token)
}

payload := url.Values{
"next": {""}, // /marks/current/
Expand Down Expand Up @@ -125,26 +122,13 @@ func (cli *Client) Login() (err error) {
return
}

if DEBUG {
h1 := doc.Find("#logo > h1").First()
log.Printf("%s", h1.Text())
}

// redirected := resp.Request.URL.String()
// log.Printf("redirected: %s", redirected)
// if redirected != urlLoginSuccess {
// return errors.New("redirected to not success url")
// }

err = cli.GetCurrentInfo()

return
}

// GetCurrentInfo for session
func (cli *Client) GetCurrentInfo() (err error) {
// ci := CurrentInfo{}

resp, err := cli.http.Get(urlHomework)
doc, err := goquery.NewDocumentFromResponse(resp)

Expand All @@ -153,18 +137,12 @@ func (cli *Client) GetCurrentInfo() (err error) {
if err != nil {
return
}
if DEBUG {
log.Printf("className - %d%s\n", classNumber, 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)")
if DEBUG {
log.Printf("class_id - %s\n", classIDText)
}
}
classID, err := strconv.ParseInt(classIDText, 10, 32)
cli.currentInfo.ClassID = classID
Expand All @@ -176,17 +154,7 @@ func (cli *Client) GetCurrentInfo() (err error) {
cli.currentInfo.EduYearStart = int(eys)
cli.currentInfo.EduYearEnd = int(eye)

// log.Printf("eduyear - %s\n", eyr)

// for _, cookie := range cli.http.Jar.Cookies(u) {
// if cookie.Name == "edu_year" {
// cli.currentInfo.EduYearStart = cookie.Value
// }
// }

cli.ToJSON(cli.currentInfo)
// cli.currentInfo = ci
// cli.ToJSON(cli.currentInfo)

return
}
Expand All @@ -195,9 +163,6 @@ func (cli *Client) GetCurrentInfo() (err error) {
func (cli *Client) ToJSON(o interface{}) (result string) {
oj, _ := json.Marshal(o)
result = string(oj)
if DEBUG {
log.Printf(":: %s\n", result)
}
return
}

Expand All @@ -219,16 +184,13 @@ func (cli *Client) SetCookie(name, value string) {

u, _ := url.Parse(urlLogin)
var cookies []*http.Cookie
// cookies := cli.http.Jar.Cookies(u)
cookies = append(cookies, cookie)
cli.http.Jar.SetCookies(u, cookies)
cli.GetCurrentInfo()
}

// GetRegions to get client regions
func GetRegions() (regions []Region, err error) {
// Get kladr - https://my.dnevnik76.ru/ajax/kladr/?login=true
// 1. get region - select > option
resp, err := http.Get(fmt.Sprintf("%s/kladr/?login=true", urlAjax))
if err != nil {
return
Expand Down Expand Up @@ -312,7 +274,6 @@ func (cli *Client) GetMarksPeriods() (periods []Lperiod, err error) {
title := strings.TrimSpace(s2.Text())
value, _ := s2.Attr("value")
period := Lperiod{
// RegionID: cli.currentInfo.RegionID,
SchoolID: cli.currentInfo.SchoolID,
SYear: cli.currentInfo.EduYearStart,
EYear: cli.currentInfo.EduYearEnd,
Expand Down Expand Up @@ -403,9 +364,6 @@ func (cli *Client) GetMarksForMonthWithType(p string, t MarksListType) (marks []
mark.Date = russiantime.ParseDateString(d)
mark.Grade = append(mark.Grade, int8(pm))
marks = append(marks, mark)
if DEBUG {
log.Printf("course - %s, d - %s, mark - %d", mark.CourseName, mark.Date, pm)
}
}
})
})
Expand All @@ -429,19 +387,9 @@ func (cli *Client) GetMarksFinal() (marks []Mark, err error) {
if err != nil {
return
}
title := doc.Find("#content > h3").Text()

ret := regexp.MustCompile(`([^"]*)(\d{4}\s\-\s\d{4})([^"]*)`)
lrange := ret.ReplaceAllString(title, "${2}")
if DEBUG {
log.Printf("%s", lrange)
}

courses, _ := cli.GetCourses()
doc.Find("#marks > #wrap-col > #wrap-marks > div > #mark-row").Each(func(i int, s *goquery.Selection) {
courseID, _ := s.Attr("name")
if DEBUG {
log.Printf("course id - %s", courseID)
}

s.Find(".mark").Each(func(j int, sj *goquery.Selection) {
mark := Mark{}
Expand All @@ -450,6 +398,12 @@ func (cli *Client) GetMarksFinal() (marks []Mark, err error) {
mark.UserID = cli.Username
mark.SchoolID = cli.SchoolID
mark.CourseID, _ = strconv.ParseInt(courseID, 10, 32)
for _, c := range courses {
if c.ID == mark.CourseID {
mark.CourseName = c.Name
break
}
}
data := func() (period string, fmark string) {
el := sj.Find("a").First()
onClick, _ := el.Attr("onclick")
Expand Down Expand Up @@ -604,9 +558,6 @@ func (cli *Client) GetHomework() (hws []Homework, err error) {
classIDText, _ := doc.Find("body").Attr("onload")
if classIDText != "" {
classIDText = strings.TrimRight(strings.TrimLeft(classIDText, "loadSubjects('/ajax/subj/"), "', true)")
if DEBUG {
log.Printf("class_id - %s\n", classIDText)
}
}
classID, err := strconv.ParseInt(classIDText, 10, 64)
hwPagesFlag := doc.Find("#homework_list > div.pager > span.page_remark").Text()
Expand Down
Loading

0 comments on commit 2a681f3

Please sign in to comment.