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

View file

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"time"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
@ -32,6 +33,16 @@ type LoginResponse struct {
var Url = os.Getenv("URL") 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 Password = os.Getenv("UNTIS_PASSWORD")
// var USERS = os.Getenv("UNTIS_USER") // var USERS = os.Getenv("UNTIS_USER")
func Main(user, password, userID string) { 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") log.Println("No Discord webhook provided, Discord notifications will be disabled")
} }
locEnv := os.Getenv("LOCATION_ENV") locEnv := os.Getenv("LOCATION_ENV")
var locerr error
var locErr error Untis.Location, locerr = time.LoadLocation(locEnv)
if err != nil {
location, locErr = time.LoadLocation(locEnv) log.Fatalf("Failed to load Timezone: %v", locerr)
if locErr != nil {
log.Fatalf("Failed to load timezone: %v", locErr)
} }
log.Printf("Timezone set to: %s", location)
} }
func getTime() time.Time { func getTime() time.Time {
return time.Now().In(location) return Untis.GetTime()
} }
func main() { func main() {
@ -208,9 +204,15 @@ func scheduleTimetableUpdate() {
prevTimeslotMap := make(map[string]NamedTimetableEntry) prevTimeslotMap := make(map[string]NamedTimetableEntry)
// Get current date to filter out new day changes // 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 // 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 { for _, e := range currentEntries {
key := lessonKey(e) key := lessonKey(e)
@ -229,9 +231,6 @@ func scheduleTimetableUpdate() {
if curr.Date != today { if curr.Date != today {
continue continue
} }
if now.Hour() < 3 {
continue
}
if prev, exists := prevMap[key]; exists { if prev, exists := prevMap[key]; exists {
// Existing lesson — check for changes // Existing lesson — check for changes