From fb503f6a9e585171e8bf89422234cbe4d6b66aaf Mon Sep 17 00:00:00 2001 From: Jannover5000r <90698896+Jannover5000r@users.noreply.github.com> Date: Mon, 24 Aug 2026 18:37:25 +0200 Subject: [PATCH] added a few things to ticker will now only update timetable not send all requests to avoid rate limiting Untis.go Main() will only be used in initial startup ticker cleanup in botstart since that isnt being used anymore --- Botrun/BotStart.go | 10 ---------- Untis/Untis.go | 12 ++++++++++-- go.mod | 2 +- main.go | 45 +++++++++++++++++++++++++++++++++++---------- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/Botrun/BotStart.go b/Botrun/BotStart.go index 7342ab8..e53f054 100644 --- a/Botrun/BotStart.go +++ b/Botrun/BotStart.go @@ -15,7 +15,6 @@ import ( "path/filepath" "sync" "syscall" - "time" "untislogger/Untis" @@ -225,7 +224,6 @@ func checkAllUsersTimetables(s *discordgo.Session) { accounts := loadAllAccounts() for _, user := range accounts { decPwd, _ := decrypt(user.Password) - // You can use decPwd to fetch the timetable for this user checkTimetableChangesForUser(user, decPwd, s) } } @@ -253,14 +251,6 @@ func Start() { return } - // Schedule timetable checks every minute (or hour as needed) - go func() { - ticker := time.NewTicker(1 * time.Minute) - for range ticker.C { - checkAllUsersTimetables(dg) - } - }() - fmt.Println("Bot is now running. Press CTRL+C to exit.") // Wait for CTRL+C or other term signal to exit. sc := make(chan os.Signal, 1) diff --git a/Untis/Untis.go b/Untis/Untis.go index 01f6324..dc266e6 100644 --- a/Untis/Untis.go +++ b/Untis/Untis.go @@ -32,8 +32,6 @@ type LoginResponse struct { var Url = os.Getenv("URL") -// var Password = os.Getenv("UNTIS_PASSWORD") -// var USERS = os.Getenv("UNTIS_USER") func Main(user, password, userID string) { godotenv.Load("../.env") cookies, err := Auth(user, password, userID) @@ -48,6 +46,16 @@ func Main(user, password, userID string) { Teachers(cookies) } +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) +} + func Auth(user, password, userID string) ([]*http.Cookie, error) { l := Login{"2023-05-06 15:44:22.215292", "authenticate", Params{user, password, "WebUntis Test"}, "2.0"} loginJSON, err := json.Marshal(l) diff --git a/go.mod b/go.mod index 1d720a8..e3883e8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module untislogger -go 1.25.2 +go 1.27.0 require ( github.com/bwmarrin/discordgo v0.29.0 diff --git a/main.go b/main.go index fd60b0c..145c443 100644 --- a/main.go +++ b/main.go @@ -111,19 +111,13 @@ func init() { log.Fatalf("Failed to load timezone: %v", locErr) } log.Printf("Timezone set to: %s", location) -} - -func getTime() time.Time { - return time.Now().In(location) + initUpdate() } func main() { godotenv.Load(".env") - // Untis.Main() //starting API calls function| happens in schedule func - // Run() - // Starts logging the timetable for each new Lesson and logs changes go BotStart.Start() - scheduleTimetableUpdate() + go scheduleTimetableUpdate() sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) @@ -136,6 +130,10 @@ func main() { log.Println("Shutting down...") } +func getTime() time.Time { + return time.Now().In(location) +} + func isScheduledTime(now time.Time) bool { scheduled := []string{"07:45", "08:35", "09:35", "10:25", "11:25", "12:15", "13:40", "14:25", "15:20", "16:00"} current := now.Format("15:04") @@ -147,6 +145,32 @@ func isScheduledTime(now time.Time) bool { return false } +func initUpdate() { + // Load all registered users + accounts, err := BotStart.LoadAndDecryptAccounts() + if err != nil { + log.Printf("Error loading accounts: %v", err) + } + + // Add the default user from .env + defaultUser := os.Getenv("UNTIS_USER") + defaultPassword := os.Getenv("UNTIS_PASSWORD") + if defaultUser != "" && defaultPassword != "" { + accounts = append(accounts, BotStart.DecryptedAccount{ + UserID: "default", + Username: defaultUser, + DecryptedPassword: defaultPassword, + }) + } + + // Process each user + for _, acc := range accounts { + // Fetch the latest timetable and only timetable + Untis.Main(acc.Username, acc.DecryptedPassword, acc.UserID) + time.Sleep(3 * time.Second) + } +} + func scheduleTimetableUpdate() { prevData := make(map[string][]byte) @@ -171,8 +195,9 @@ func scheduleTimetableUpdate() { // Process each user for _, acc := range accounts { - // Fetch the latest timetable - Untis.Main(acc.Username, acc.DecryptedPassword, acc.UserID) + // Fetch the latest timetable and only timetable + Untis.Update(acc.Username, acc.DecryptedPassword, acc.UserID) + time.Sleep(3 * time.Second) // Check for changes timetableFile := fmt.Sprintf("timetableFilled_%s.json", acc.UserID)