in order to prevent a huge amount of restarts i removed Fatalf and made it return normally I also made GetTime load the timezone once and just return the correct value as well as default to utc made the Auth function retry 3 times per account and send an error after that put time.Sleep into the correct loop in main.go initUpdate is in main now
113 lines
2.4 KiB
Go
113 lines
2.4 KiB
Go
package Untis
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Params struct {
|
|
Users string `json:"user"`
|
|
Password string `json:"password"`
|
|
Client string `json:"client"`
|
|
}
|
|
|
|
type Login struct {
|
|
Id string `json:"id"`
|
|
Method string `json:"method"`
|
|
Params Params `json:"params"`
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
}
|
|
type LoginResponse struct {
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
ID string `json:"id"`
|
|
Result Loginresult `json:"result"`
|
|
}
|
|
|
|
var Url = os.Getenv("URL")
|
|
|
|
func Main(user, password, userID string) {
|
|
godotenv.Load("../.env")
|
|
cookies, err := Auth(user, password, userID)
|
|
if err != nil {
|
|
log.Printf("Authentication failed for user %s: %v", user, err)
|
|
return
|
|
}
|
|
Rooms(cookies)
|
|
Classes(cookies)
|
|
Subjects(cookies)
|
|
Timetable(cookies, userID)
|
|
Teachers(cookies)
|
|
log.Printf("updated everything for user: %s ", user)
|
|
}
|
|
|
|
func Update(user, password, userID string) {
|
|
godotenv.Load("../.env")
|
|
cookies, err := Auth(user, password, userID)
|
|
if err != nil {
|
|
log.Printf("Authentication failed for user %s: %v", user, err)
|
|
return
|
|
}
|
|
Timetable(cookies, userID)
|
|
log.Printf("Updated info for user: %s ", user)
|
|
}
|
|
|
|
func Auth(user, password, userID string) ([]*http.Cookie, error) {
|
|
loginJSON, _ := json.Marshal(Login{
|
|
"2023-05-06 15:44:22.215292", "authenticate",
|
|
Params{user, password, "WebUntis Test"},
|
|
"2.0",
|
|
})
|
|
if Url == "" {
|
|
Url = os.Getenv("URL")
|
|
}
|
|
|
|
var LoginOut *http.Response
|
|
var err error
|
|
for attempt := 1; attempt <= 3; attempt++ {
|
|
LoginOut, err = http.Post(Url, "application/json", bytes.NewReader(loginJSON))
|
|
if err == nil {
|
|
break
|
|
}
|
|
log.Printf("Auth attempt %d/3 failed for %s: %v", attempt, user, err)
|
|
time.Sleep(time.Duration(attempt*2) * time.Second)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer LoginOut.Body.Close()
|
|
cookies := LoginOut.Cookies()
|
|
// log.Printf("Login successful for user: %s", user)//log not needed
|
|
|
|
response, err := io.ReadAll(LoginOut.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var Response LoginResponse
|
|
if err := json.Unmarshal(response, &Response); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data, err := json.MarshalIndent(Response.Result, "", " ")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
loginFile := "login.json"
|
|
if userID != "" {
|
|
loginFile = "login_" + userID + ".json"
|
|
}
|
|
if err := os.WriteFile(loginFile, data, 0o644); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cookies, nil
|
|
}
|