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
This commit is contained in:
Jannover5000r 2026-08-24 18:37:25 +02:00
parent bf0b89e024
commit fb503f6a9e
4 changed files with 46 additions and 23 deletions

View file

@ -15,7 +15,6 @@ import (
"path/filepath" "path/filepath"
"sync" "sync"
"syscall" "syscall"
"time"
"untislogger/Untis" "untislogger/Untis"
@ -225,7 +224,6 @@ func checkAllUsersTimetables(s *discordgo.Session) {
accounts := loadAllAccounts() accounts := loadAllAccounts()
for _, user := range accounts { for _, user := range accounts {
decPwd, _ := decrypt(user.Password) decPwd, _ := decrypt(user.Password)
// You can use decPwd to fetch the timetable for this user
checkTimetableChangesForUser(user, decPwd, s) checkTimetableChangesForUser(user, decPwd, s)
} }
} }
@ -253,14 +251,6 @@ func Start() {
return 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.") fmt.Println("Bot is now running. Press CTRL+C to exit.")
// Wait for CTRL+C or other term signal to exit. // Wait for CTRL+C or other term signal to exit.
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)

View file

@ -32,8 +32,6 @@ type LoginResponse struct {
var Url = os.Getenv("URL") var Url = os.Getenv("URL")
// var Password = os.Getenv("UNTIS_PASSWORD")
// var USERS = os.Getenv("UNTIS_USER")
func Main(user, password, userID string) { func Main(user, password, userID string) {
godotenv.Load("../.env") godotenv.Load("../.env")
cookies, err := Auth(user, password, userID) cookies, err := Auth(user, password, userID)
@ -48,6 +46,16 @@ func Main(user, password, userID string) {
Teachers(cookies) 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) { 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"} l := Login{"2023-05-06 15:44:22.215292", "authenticate", Params{user, password, "WebUntis Test"}, "2.0"}
loginJSON, err := json.Marshal(l) loginJSON, err := json.Marshal(l)

2
go.mod
View file

@ -1,6 +1,6 @@
module untislogger module untislogger
go 1.25.2 go 1.27.0
require ( require (
github.com/bwmarrin/discordgo v0.29.0 github.com/bwmarrin/discordgo v0.29.0

45
main.go
View file

@ -111,19 +111,13 @@ func init() {
log.Fatalf("Failed to load timezone: %v", locErr) log.Fatalf("Failed to load timezone: %v", locErr)
} }
log.Printf("Timezone set to: %s", location) log.Printf("Timezone set to: %s", location)
} initUpdate()
func getTime() time.Time {
return time.Now().In(location)
} }
func main() { func main() {
godotenv.Load(".env") 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() go BotStart.Start()
scheduleTimetableUpdate() go scheduleTimetableUpdate()
sigChan := make(chan os.Signal, 1) sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
@ -136,6 +130,10 @@ func main() {
log.Println("Shutting down...") log.Println("Shutting down...")
} }
func getTime() time.Time {
return time.Now().In(location)
}
func isScheduledTime(now time.Time) bool { 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"} 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") current := now.Format("15:04")
@ -147,6 +145,32 @@ func isScheduledTime(now time.Time) bool {
return false 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() { func scheduleTimetableUpdate() {
prevData := make(map[string][]byte) prevData := make(map[string][]byte)
@ -171,8 +195,9 @@ func scheduleTimetableUpdate() {
// Process each user // Process each user
for _, acc := range accounts { for _, acc := range accounts {
// Fetch the latest timetable // Fetch the latest timetable and only timetable
Untis.Main(acc.Username, acc.DecryptedPassword, acc.UserID) Untis.Update(acc.Username, acc.DecryptedPassword, acc.UserID)
time.Sleep(3 * time.Second)
// Check for changes // Check for changes
timetableFile := fmt.Sprintf("timetableFilled_%s.json", acc.UserID) timetableFile := fmt.Sprintf("timetableFilled_%s.json", acc.UserID)