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 { var status string switch n.Status { case "angemeldet": status = "⏳ - " case "bestanden": status = "✔ " case "nicht bestanden": // Not sure if thats the correct value. // Can't test it. Never failed an exam :P status = "✖ " default: status = "❓" } return fmt.Sprintf("%s%s %s", status, n.Note, n.Name) } // 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, ¬e) }) return noten, nil }