mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-07-16 17:48:51 +02:00
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"htwk-planner/model"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/pocketbase/pocketbase"
|
|
)
|
|
|
|
func getSeminarHTML() (string, error) {
|
|
url := "https://stundenplan.htwk-leipzig.de/stundenplan/xml/public/semgrp_ss.xml"
|
|
|
|
// Send GET request
|
|
response, err := http.Get(url)
|
|
if err != nil {
|
|
fmt.Printf("Error occurred while making the request: %s\n", err.Error())
|
|
return "", err
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}(response.Body)
|
|
|
|
// Read the response body
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
fmt.Printf("Error occurred while reading the response: %s\n", err.Error())
|
|
return "", err
|
|
}
|
|
|
|
return string(body), err
|
|
|
|
}
|
|
|
|
func FetchSeminarGroups(c echo.Context, app *pocketbase.PocketBase) error {
|
|
|
|
result, _ := getSeminarHTML()
|
|
|
|
println(result)
|
|
|
|
var studium []model.SeminarGroup
|
|
|
|
studium = parseSeminarGroups(result)
|
|
|
|
return c.JSON(http.StatusOK, studium)
|
|
}
|
|
|
|
func parseSeminarGroups(result string) []model.SeminarGroup {
|
|
|
|
var studium model.Studium
|
|
|
|
err := xml.Unmarshal([]byte(result), &studium)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var seminarGroups []model.SeminarGroup
|
|
for _, Fakultaet := range studium.Fakultaet {
|
|
for _, Studiengang := range Fakultaet.Studiengang {
|
|
for _, Studienrichtung := range Studiengang.Semgrp {
|
|
seminarGroup := model.SeminarGroup{
|
|
University: "HTWK-Leipzig",
|
|
GroupShortcut: Studiengang.Name,
|
|
GroupId: Studiengang.ID,
|
|
Course: Studienrichtung.Name,
|
|
Faculty: Fakultaet.Name,
|
|
FacultyId: Fakultaet.ID,
|
|
}
|
|
seminarGroups = append(seminarGroups, seminarGroup)
|
|
}
|
|
}
|
|
}
|
|
|
|
return seminarGroups
|
|
}
|