mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-03 18:29:14 +02:00
fix:#41 added error if html request isn't 200
This commit is contained in:
@@ -122,10 +122,12 @@ func DeleteAllEvents(app *pocketbase.PocketBase) error {
|
||||
// If the update was not successful, an error is returned
|
||||
func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) (model.Events, error) {
|
||||
|
||||
seminarGroup := v1.GetSeminarGroupEventsFromHTML(course)
|
||||
seminarGroup, err := v1.GetSeminarGroupEventsFromHTML(course)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
seminarGroup = v1.ClearEmptySeminarGroups(seminarGroup)
|
||||
|
||||
seminarGroup = v1.ReplaceEmptyEventNames(seminarGroup)
|
||||
|
||||
//check if events in the seminarGroups Events are already in the database
|
||||
|
@@ -17,8 +17,10 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
@@ -26,18 +28,27 @@ import (
|
||||
// getPlanHTML Get the HTML document from the specified URL
|
||||
|
||||
func GetHTML(url string) (string, error) {
|
||||
|
||||
// Create HTTP client with timeout of 5 seconds
|
||||
client := http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
return GetHTMLWithClient(url, &client)
|
||||
}
|
||||
|
||||
func GetHTMLWithClient(url string, client *http.Client) (string, error) {
|
||||
|
||||
// Send GET request
|
||||
response, err := client.Get(url)
|
||||
if err != nil {
|
||||
fmt.Printf("Error occurred while making the request: %s\n", err.Error())
|
||||
slog.Error("Error occurred while fetching the HTML document:", "error", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if response.StatusCode != 200 {
|
||||
slog.Warn("While fetching the HTML document, the server responded with status code: ", "status", response.StatusCode)
|
||||
return "", errors.New(fmt.Sprintf("Server responded with status code: %d", response.StatusCode))
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
63
services/data-manager/service/fetch/htmlDownloader_test.go
Normal file
63
services/data-manager/service/fetch/htmlDownloader_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"github.com/jarcoal/httpmock"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetHTMLWithClient(t *testing.T) {
|
||||
|
||||
client := &http.Client{}
|
||||
httpmock.ActivateNonDefault(client)
|
||||
|
||||
type args struct {
|
||||
url string
|
||||
statusCode int
|
||||
method string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test GetHTML with status code 200",
|
||||
args: args{
|
||||
url: "https://stundenplan.htwk-leipzig.de/ss/Berichte/Text-Listen;Studenten-Sets;name;1-1?template=sws_semgrp&weeks=1-65",
|
||||
method: "GET",
|
||||
statusCode: 200,
|
||||
},
|
||||
want: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test GetHTML with status code 404",
|
||||
args: args{
|
||||
url: "https://stundenplan.htwk-leipzig.de/ss/Berichte/Text-Lists;Studenten-Sets;name;1-1?template=sws_semgrp&weeks=1-65",
|
||||
method: "GET",
|
||||
statusCode: 404,
|
||||
},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
httpmock.RegisterResponder(tt.args.method, tt.args.url,
|
||||
httpmock.NewStringResponder(tt.args.statusCode, tt.want))
|
||||
got, err := GetHTMLWithClient(tt.args.url, client)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetHTML() error = %v, wantNoErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("GetHTML() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
httpmock.DeactivateAndReset()
|
||||
}
|
@@ -50,25 +50,51 @@ func ClearEmptySeminarGroups(seminarGroup model.SeminarGroup) model.SeminarGroup
|
||||
return newSeminarGroup
|
||||
}
|
||||
|
||||
func GetSeminarGroupEventsFromHTML(seminarGroupLabel string) model.SeminarGroup {
|
||||
var seminarGroup model.SeminarGroup
|
||||
func fetchHTMLFromURL(semester, seminarGroupLabel string) (string, error) {
|
||||
url := "https://stundenplan.htwk-leipzig.de/" + semester + "/Berichte/Text-Listen;Studenten-Sets;name;" + seminarGroupLabel + "?template=sws_semgrp&weeks=1-65"
|
||||
result, err := fetch.GetHTML(url)
|
||||
if err != nil {
|
||||
slog.Error("Error occurred while fetching the HTML document:", "error", err)
|
||||
return "", err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
if (time.Now().Month() >= 3) && (time.Now().Month() <= 10) {
|
||||
ssUrl := "https://stundenplan.htwk-leipzig.de/" + string("ss") + "/Berichte/Text-Listen;Studenten-Sets;name;" + seminarGroupLabel + "?template=sws_semgrp&weeks=1-65"
|
||||
result, getError := fetch.GetHTML(ssUrl)
|
||||
if getError == nil {
|
||||
seminarGroup = parseSeminarGroup(result)
|
||||
func GetSeminarGroupEventsFromHTML(seminarGroupLabel string) (model.SeminarGroup, error) {
|
||||
var seminarGroup [2]model.SeminarGroup
|
||||
var result string
|
||||
var errSS error
|
||||
var errWS error
|
||||
|
||||
currentMonth := time.Now().Month()
|
||||
if currentMonth >= 3 && currentMonth <= 10 {
|
||||
result, errSS = fetchHTMLFromURL("ss", seminarGroupLabel)
|
||||
if errSS == nil {
|
||||
seminarGroup[0] = parseSeminarGroup(result)
|
||||
}
|
||||
|
||||
}
|
||||
if currentMonth >= 9 || currentMonth <= 4 {
|
||||
result, errWS = fetchHTMLFromURL("ws", seminarGroupLabel)
|
||||
if errWS == nil {
|
||||
seminarGroup[1] = parseSeminarGroup(result)
|
||||
}
|
||||
}
|
||||
|
||||
if (time.Now().Month() >= 9) || (time.Now().Month() <= 4) {
|
||||
wsUrl := "https://stundenplan.htwk-leipzig.de/" + string("ws") + "/Berichte/Text-Listen;Studenten-Sets;name;" + seminarGroupLabel + "?template=sws_semgrp&weeks=1-65"
|
||||
result, getError := fetch.GetHTML(wsUrl)
|
||||
if getError == nil {
|
||||
seminarGroup = parseSeminarGroup(result)
|
||||
if errSS != nil {
|
||||
if errWS != nil {
|
||||
return model.SeminarGroup{}, errWS
|
||||
} else {
|
||||
return seminarGroup[1], nil
|
||||
}
|
||||
} else {
|
||||
if errWS != nil {
|
||||
return seminarGroup[0], nil
|
||||
} else {
|
||||
seminarGroup[0].Events = append(seminarGroup[0].Events, seminarGroup[1].Events...)
|
||||
return seminarGroup[0], nil
|
||||
}
|
||||
}
|
||||
return seminarGroup
|
||||
}
|
||||
|
||||
func SplitEventType(events []model.Event) ([]model.Event, error) {
|
||||
|
Reference in New Issue
Block a user