Feature update at2 am #13

Merged
Jannover5000r merged 3 commits from feature-UpdateAt2Am into main 2026-08-24 02:44:27 +02:00
4 changed files with 40 additions and 18 deletions

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
}

View file

@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
)
@ -32,6 +33,16 @@ type LoginResponse struct {
var Url = os.Getenv("URL")
var location *time.Location
func init() {
locEnv := os.Getenv("LOCATION_ENV")
if locEnv == "" {
locEnv = "UTC"
}
location, _ = time.LoadLocation(locEnv)
}
// var Password = os.Getenv("UNTIS_PASSWORD")
// var USERS = os.Getenv("UNTIS_USER")
func Main(user, password, userID string) {

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)
}

27
main.go
View file

@ -102,19 +102,15 @@ 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 err != nil {
log.Fatalf("Failed to load Timezone: %v", locerr)
}
log.Printf("Timezone set to: %s", location)
}
func getTime() time.Time {
return time.Now().In(location)
return Untis.GetTime()
}
func main() {
@ -208,9 +204,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)
@ -229,9 +231,6 @@ func scheduleTimetableUpdate() {
if curr.Date != today {
continue
}
if now.Hour() < 3 {
continue
}
if prev, exists := prevMap[key]; exists {
// Existing lesson — check for changes