This commit is contained in:
Jannover5000r 2026-08-24 18:41:36 +02:00
commit ae5b1f7b28
4 changed files with 28 additions and 18 deletions

View file

@ -1,5 +1,3 @@
# Well, i guess this is not temporary anymore and will just become the new main
# Discord bot for logging Untis timetable to a Discord Webhook and Multiple accounts which can be added using !addaccount in a server whit the Bot
## create .env with Credentials

View file

@ -8,7 +8,6 @@ import (
"log"
"net/http"
"os"
"time"
)
type TimetableResponse struct {
@ -100,7 +99,7 @@ func Timetable(cookies []*http.Cookie, userID string) {
return
}
today := time.Now().Format("20060102")
today := GetTime().Format("20060102")
g := getTimetable{"2023-05-06 15:44:22.215292", "getTimetable", params{today, today, loginResult.PersonID, loginResult.PersonType}, "2.0"}
timetablesJson, err := json.Marshal(g)
if err != nil {
@ -148,7 +147,7 @@ func Timetable(cookies []*http.Cookie, userID string) {
if userID != "" {
timetableFile = "timetable_" + userID + ".json"
}
if err := os.WriteFile(timetableFile, data, 0644); err != nil {
if err := os.WriteFile(timetableFile, data, 0o644); err != nil {
log.Printf("Error writing timetable file: %v", err)
return
}
@ -172,6 +171,7 @@ func LoadIDMap(path string) (map[int]string, error) {
}
return m, nil
}
func LoadTimetable(path string) ([]TimetableEntry, error) {
data, err := os.ReadFile(path)
if err != nil {
@ -183,11 +183,13 @@ func LoadTimetable(path string) ([]TimetableEntry, error) {
}
return entries, nil
}
func formatTime(t int) string {
h := t / 100
m := t % 100
return fmt.Sprintf("%02d:%02d", h, m)
}
func formatDate(date int) string {
s := fmt.Sprintf("%08d", date) // ensures leading zeros
year := s[:4]
@ -195,6 +197,7 @@ func formatDate(date int) string {
day := s[6:8]
return fmt.Sprintf("%s-%s-%s", day, month, year)
}
func setTimetable(userID string) {
subjects, _ := LoadIDMap("subjects.json")
rooms, _ := LoadIDMap("rooms.json")
@ -242,7 +245,7 @@ func setTimetable(userID string) {
if userID != "" {
timetableFilledFile = "timetableFilled_" + userID + ".json"
}
if err := os.WriteFile(timetableFilledFile, data, 0644); err != nil {
if err := os.WriteFile(timetableFilledFile, data, 0o644); err != nil {
log.Printf("Error writing timetableFilled file: %v", err)
return
}

9
Untis/timeutil.go Normal file
View file

@ -0,0 +1,9 @@
package Untis
import "time"
var Location *time.Location
func GetTime() time.Time {
return time.Now().In(Location)
}

24
main.go
View file

@ -102,13 +102,10 @@ func init() {
log.Println("No Discord webhook provided, Discord notifications will be disabled")
}
locEnv := os.Getenv("LOCATION_ENV")
var locErr error
location, locErr = time.LoadLocation(locEnv)
if locErr != nil {
log.Fatalf("Failed to load timezone: %v", locErr)
var locerr error
Untis.Location, locerr = time.LoadLocation(locEnv)
if locerr != nil {
log.Fatalf("Failed to load Timezone: %v", locerr)
}
log.Printf("Timezone set to: %s", location)
initUpdate()
@ -233,9 +230,15 @@ func scheduleTimetableUpdate() {
prevTimeslotMap := make(map[string]NamedTimetableEntry)
// Get current date to filter out new day changes
today := time.Now().Format("02-01-2006")
today := getTime().Format("02-01-2006")
// Get current time to filter any changes to Timetable before 3am so the new day changes stop
now := time.Now()
//
now := getTime()
// If before 1am stop and update userdata
if now.Hour() < 1 {
prevData[acc.UserID] = data
continue
}
for _, e := range currentEntries {
key := lessonKey(e)
@ -254,9 +257,6 @@ func scheduleTimetableUpdate() {
if curr.Date != today {
continue
}
if now.Hour() < 3 {
continue
}
if prev, exists := prevMap[key]; exists {
// Existing lesson — check for changes