2019-10-05 20:32:39 +02:00
|
|
|
package lsf
|
|
|
|
|
|
|
|
import (
|
2019-10-06 19:20:57 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2019-10-05 20:32:39 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-10-11 16:07:56 +02:00
|
|
|
// 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
|
2019-10-06 19:20:57 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:07:56 +02:00
|
|
|
// Grades is an Array of Grade pointers
|
|
|
|
type Grades []*Grade
|
2019-10-11 16:04:40 +02:00
|
|
|
|
2019-10-11 16:07:56 +02:00
|
|
|
func (g *Grade) String() string {
|
2019-10-06 19:59:58 +02:00
|
|
|
var status string
|
2019-10-11 16:07:56 +02:00
|
|
|
switch g.Status {
|
2019-10-06 19:59:58 +02:00
|
|
|
case "angemeldet":
|
|
|
|
status = "⏳ - "
|
|
|
|
case "bestanden":
|
|
|
|
status = "✔ "
|
|
|
|
case "nicht bestanden":
|
|
|
|
status = "✖ "
|
|
|
|
default:
|
|
|
|
status = "❓"
|
|
|
|
}
|
2019-10-11 16:07:56 +02:00
|
|
|
return fmt.Sprintf("%s%s %s", status, g.Grade, g.Name)
|
2019-10-06 19:20:57 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:07:56 +02:00
|
|
|
// Print prints a clear overview of the grades in the given list
|
|
|
|
func (gg Grades) Print() {
|
|
|
|
for _, g := range gg {
|
|
|
|
fmt.Println(g)
|
2019-10-11 16:04:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 16:07:56 +02:00
|
|
|
// Grades returns a list of the grades of all graded or signed up modules
|
|
|
|
func (s *Session) Grades() (Grades, error) {
|
|
|
|
var grades Grades
|
2023-06-28 01:11:22 +02:00
|
|
|
|
2019-10-06 19:20:57 +02:00
|
|
|
client := &http.Client{}
|
2023-06-28 01:11:22 +02:00
|
|
|
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
|
2019-10-06 19:20:57 +02:00
|
|
|
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())
|
|
|
|
})
|
2019-10-11 16:07:56 +02:00
|
|
|
grade := &Grade{
|
|
|
|
Nr: vals[0],
|
|
|
|
Name: vals[1],
|
|
|
|
Term: vals[2],
|
|
|
|
Grade: vals[3],
|
|
|
|
Status: vals[4],
|
|
|
|
Credits: vals[5],
|
|
|
|
Try: vals[7],
|
2019-10-06 19:20:57 +02:00
|
|
|
}
|
2019-10-11 16:07:56 +02:00
|
|
|
grades = append(grades, grade)
|
2019-10-06 19:20:57 +02:00
|
|
|
})
|
|
|
|
|
2019-10-11 16:07:56 +02:00
|
|
|
return grades, nil
|
2019-10-05 20:32:39 +02:00
|
|
|
}
|