LSF/noten.go
2019-10-06 19:20:57 +02:00

67 lines
1.8 KiB
Go

package lsf
import (
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
)
// Note is a grade with associated data like the related module
type Note struct {
Nr string
Name string
Semester string
Note string
Status string
CPs string
Versuch string
}
func (n *Note) String() string {
return fmt.Sprintf("%s: %s", n.Name, n.Note)
}
// Noten returns a list of the grades of all graded or signed up modules
func (s *Session) Noten() ([]*Note, error) {
var noten []*Note
client := &http.Client{}
url := "https://lsf.hs-worms.de/qisserver/rds?state=notenspiegelStudent&next=list.vm&nextdir=qispos/notenspiegel/student&createInfos=Y&struct=auswahlBaum&nodeID=auswahlBaum%7Cabschluss%3Aabschl%3D05%2Cstgnr%3D1%7Cstudiengang%3Astg%3D938%2Cpversion%3D2018&expand=0&asi=" + s.ASI
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "could not prepare the request")
}
req.Header.Add("Cookie", fmt.Sprintf("JSESSIONID=%s", s.SID))
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "could not do the request")
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return nil, err
}
// Find all tables. The last one contains the grades. Get all children rows.
rows := doc.Find("table").Last().Find("tr")
// The first three are the header, the average mark and a divider. So lets ignore them.
rows.Slice(3, rows.Length()).Each(func(_ int, row *goquery.Selection) {
vals := row.ChildrenFiltered("td").Map(func(i int, cell *goquery.Selection) string {
return strings.TrimSpace(cell.Text())
})
note := Note{
Nr: vals[0],
Name: vals[1],
Semester: vals[2],
Note: vals[3],
Status: vals[4],
CPs: vals[5],
Versuch: vals[7],
}
noten = append(noten, &note)
})
return noten, nil
}