package lsf import ( "fmt" "net/http" "strings" "github.com/PuerkitoBio/goquery" "github.com/pkg/errors" ) // Grade is a grade with associated data like the related module type Grade struct { Nr string Name string Term string Grade string Status string Credits string Try string } // Grades is an Array of Grade pointers type Grades []*Grade func (g *Grade) String() string { var status string switch g.Status { case "angemeldet": status = "⏳ - " case "bestanden": status = "✔ " case "nicht bestanden": status = "✖ " default: status = "❓" } return fmt.Sprintf("%s%s %s", status, g.Grade, g.Name) } // Print prints a clear overview of the grades in the given list func (gg Grades) Print() { for _, g := range gg { fmt.Println(g) } } // Grades returns a list of the grades of all graded or signed up modules func (s *Session) Grades() (Grades, error) { var grades Grades 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%3DC37%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()) }) grade := &Grade{ Nr: vals[0], Name: vals[1], Term: vals[2], Grade: vals[3], Status: vals[4], Credits: vals[5], Try: vals[7], } grades = append(grades, grade) }) return grades, nil }