Files
htwkalender-pwa/backend/service/fetch/v1/fetchSeminarGroupService.go
2024-05-26 22:40:35 +02:00

144 lines
3.8 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/model"
"htwkalender/service/db"
"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
resultSummer, err := getSeminarHTML("ss")
if err != nil {
slog.Error("Error while fetching seminar groups for winter semester", "error", err)
return nil, err
}
resultWinter, _ := getSeminarHTML("ws")
if err != nil {
slog.Error("Error while fetching seminar groups for summer semester", "error", err)
return nil, err
}
groups = parseSeminarGroups(resultSummer, "ss")
groups = append(groups, parseSeminarGroups(resultWinter, "ws")...)
// filter duplicates
groups = removeDuplicates(groups)
collection, dbError := db.FindCollection(app, "groups")
if dbError != nil {
slog.Error("Error while searching collection groups", dbError)
return nil, err
}
var insertedGroups []*models.Record
insertedGroups, dbError = db.SaveGroups(groups, collection, app)
if dbError != nil {
slog.Error("Error while saving groups", 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
}