mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-16 17:48:49 +02:00
142 lines
3.9 KiB
Go
142 lines
3.9 KiB
Go
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
|
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
|
|
|
//This program is free software: you can redistribute it and/or modify
|
|
//it under the terms of the GNU Affero General Public License as published by
|
|
//the Free Software Foundation, either version 3 of the License, or
|
|
//(at your option) any later version.
|
|
|
|
//This program is distributed in the hope that it will be useful,
|
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
//GNU Affero General Public License for more details.
|
|
|
|
//You should have received a copy of the GNU Affero General Public License
|
|
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package v1
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"github.com/pocketbase/pocketbase"
|
|
"github.com/pocketbase/pocketbase/models"
|
|
"htwkalender/data-manager/model"
|
|
"htwkalender/data-manager/service/db"
|
|
"htwkalender/data-manager/service/functions"
|
|
"htwkalender/data-manager/service/functions/time"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
)
|
|
|
|
func getSeminarHTML(semester string) (string, error) {
|
|
url := "https://stundenplan.htwk-leipzig.de/stundenplan/xml/public/semgrp_" + semester + ".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(app *pocketbase.PocketBase) ([]*models.Record, error) {
|
|
var groups []model.SeminarGroup
|
|
|
|
semesterString := functions.CalculateSemesterList(time.RealClock{})
|
|
var results [2]string
|
|
var err error
|
|
|
|
for i, semester := range semesterString {
|
|
results[i], err = getSeminarHTML(semester)
|
|
if err != nil {
|
|
slog.Error("Error while fetching seminar groups for: "+semester, "error", err)
|
|
return nil, err
|
|
}
|
|
groups = append(groups, parseSeminarGroups(results[i], semester)...)
|
|
}
|
|
|
|
// filter duplicates
|
|
groups = removeDuplicates(groups)
|
|
|
|
collection, dbError := db.FindCollection(app, "groups")
|
|
if dbError != nil {
|
|
slog.Error("Error while searching collection groups", "error", dbError)
|
|
return nil, err
|
|
}
|
|
var insertedGroups []*models.Record
|
|
|
|
insertedGroups, dbError = db.SaveGroups(groups, collection, app)
|
|
if dbError != nil {
|
|
slog.Error("Error while saving groups", "error", dbError)
|
|
return nil, err
|
|
}
|
|
|
|
return insertedGroups, nil
|
|
}
|
|
|
|
func removeDuplicates(groups []model.SeminarGroup) []model.SeminarGroup {
|
|
var uniqueGroups []model.SeminarGroup
|
|
for _, group := range groups {
|
|
if !contains(uniqueGroups, group) {
|
|
uniqueGroups = append(uniqueGroups, group)
|
|
}
|
|
}
|
|
return uniqueGroups
|
|
}
|
|
|
|
func contains(groups []model.SeminarGroup, group model.SeminarGroup) bool {
|
|
for _, a := range groups {
|
|
if (a.Course == group.Course) && (a.Semester == group.Semester) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func parseSeminarGroups(result string, semester string) []model.SeminarGroup {
|
|
|
|
var studium model.Studium
|
|
err := xml.Unmarshal([]byte(result), &studium)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var seminarGroups []model.SeminarGroup
|
|
for _, faculty := range studium.Faculty {
|
|
for _, Studiengang := range faculty.Studiengang {
|
|
for _, Studienrichtung := range Studiengang.Semgrp {
|
|
seminarGroup := model.SeminarGroup{
|
|
University: "HTWK-Leipzig",
|
|
GroupShortcut: Studiengang.Name,
|
|
GroupId: Studiengang.ID,
|
|
Course: Studienrichtung.Name,
|
|
Faculty: faculty.Name,
|
|
FacultyId: faculty.ID,
|
|
Semester: semester,
|
|
}
|
|
seminarGroups = append(seminarGroups, seminarGroup)
|
|
}
|
|
}
|
|
}
|
|
return seminarGroups
|
|
}
|