Add logging and new flags for attendance program.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -106,24 +107,37 @@ func decrypt(encryptedHex, passphrase string) (string, error) {
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
||||
func parseFlags() (int, bool, error) {
|
||||
func parseFlags() (int, bool, bool, string, int, bool, error) {
|
||||
//* Set the flags
|
||||
wait := flag.Int("w", 10, "Time to wait in between refreshes")
|
||||
flag.IntVar(wait, "wait", 10, "Time to wait in between refreshes")
|
||||
wait := flag.Int("w", 10, "\tTime to wait in between refreshes")
|
||||
flag.IntVar(wait, "wait", 10, "\tTime to wait in between refreshes")
|
||||
|
||||
// Verbose mode
|
||||
isVerbose := flag.Bool("v", false, "\tEnable verbose mode")
|
||||
flag.BoolVar(isVerbose, "verbose", false, "\tEnable verbose mode")
|
||||
|
||||
// By default, run in headless mode
|
||||
headless := flag.Bool("h", true, "Run in headless mode")
|
||||
flag.BoolVar(headless, "headless", true, "Run in headless mode")
|
||||
headless := flag.Bool("h", true, "\tRun in headless mode")
|
||||
flag.BoolVar(headless, "headless", true, "\tRun in headless mode")
|
||||
|
||||
// Log to file
|
||||
logFile := flag.String("l", "", "\tLog output to file")
|
||||
flag.StringVar(logFile, "log", "", "\tLog output to file")
|
||||
|
||||
// Maximum time to run the program (minutes)
|
||||
maxTime := flag.Int("m", -1, "\tMaximum time to run the program (minutes)")
|
||||
flag.IntVar(maxTime, "maxtime", -1, "\tMaximum time to run the program (minutes)")
|
||||
|
||||
replacePass := flag.Bool("r", false, "\tReplace saved credentials")
|
||||
flag.BoolVar(replacePass, "replace", false, "\tReplace saved credentials")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *wait < 0 {
|
||||
return 0, false, fmt.Errorf("invalid wait time: %d", *wait)
|
||||
return 0, false, false, "", -1, false, fmt.Errorf("invalid wait time: %d", *wait)
|
||||
}
|
||||
|
||||
fmt.Println(*headless)
|
||||
|
||||
return *wait, *headless, nil
|
||||
return *wait, *isVerbose, *headless, *logFile, *maxTime, *replacePass, nil
|
||||
}
|
||||
|
||||
func welcomeMessage() {
|
||||
@@ -253,7 +267,7 @@ func saveCreds(filePath, username, password string) (Credentials, error) {
|
||||
return Credentials{}, fmt.Errorf("could not write credentials to file: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Credentials saved successfully!")
|
||||
log.Println("Credentials saved successfully!")
|
||||
return creds, nil
|
||||
}
|
||||
|
||||
@@ -279,6 +293,12 @@ func login(ctx context.Context, creds Credentials) error {
|
||||
func marcarPresenca(ctx context.Context) error {
|
||||
var attempt int
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
if isVerbose || maxTime != -1 {
|
||||
log.Printf("Starting attendance registration at %s\n", startTime.Format(time.RFC1123))
|
||||
}
|
||||
|
||||
for attempt = 1; attempt <= maxRetries; attempt++ {
|
||||
if err := chromedp.Run(ctx,
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
@@ -286,23 +306,23 @@ func marcarPresenca(ctx context.Context) error {
|
||||
waitCtx, cancel := context.WithTimeout(ctx, time.Duration(waitTime)*time.Second)
|
||||
defer cancel()
|
||||
|
||||
//Try to wait for the element
|
||||
err := chromedp.Run(waitCtx,
|
||||
chromedp.WaitVisible(menuPresencas))
|
||||
|
||||
if err == nil {
|
||||
fmt.Println("Navigation menu is visible!")
|
||||
if isVerbose {
|
||||
log.Println("Navigation menu is visible!")
|
||||
}
|
||||
}
|
||||
|
||||
chromedp.Run(waitCtx, chromedp.Click(menuPresencas))
|
||||
fmt.Println("Navigation menu clicked!")
|
||||
var currentURL string
|
||||
if err := chromedp.Location(¤tURL).Do(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.Contains(currentURL, "obterAulasMarcarPresenca") {
|
||||
fmt.Println("Attendance page loaded!")
|
||||
fmt.Println("Registering attendance...")
|
||||
log.Println("Attendance page loaded :", time.Now().Format(time.RFC1123))
|
||||
log.Println("Registering attendance... : ", time.Now().Format(time.RFC1123))
|
||||
break
|
||||
}
|
||||
|
||||
@@ -310,6 +330,14 @@ func marcarPresenca(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
time.Sleep(time.Duration(waitTime) * time.Second)
|
||||
|
||||
if maxTime != -1 {
|
||||
elapsedMinutes := time.Since(startTime).Minutes()
|
||||
if int(elapsedMinutes) >= maxTime {
|
||||
log.Println("Maximum time reached, attendance not registered", time.Now().Format(time.RFC1123))
|
||||
return errors.New("maximum time reached")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
@@ -327,11 +355,11 @@ func marcarPresenca(ctx context.Context) error {
|
||||
select {
|
||||
case alertMessage := <-ch:
|
||||
if strings.Contains(alertMessage, "sucesso") {
|
||||
fmt.Println("Attendance registered successfully!")
|
||||
log.Println("Attendance registered successfully! : ", time.Now().Format(time.RFC1123))
|
||||
} else if strings.Contains(alertMessage, "anteriormente") {
|
||||
fmt.Println("Attendance previously registered!")
|
||||
log.Println("Attendance previously registered! : ", time.Now().Format(time.RFC1123))
|
||||
} else {
|
||||
fmt.Println("Error registering attendance: ", alertMessage)
|
||||
log.Println("Error registering attendance: ", alertMessage)
|
||||
}
|
||||
case <-time.After(time.Duration(waitTime) * time.Second):
|
||||
return fmt.Errorf("timeout waiting for alert")
|
||||
@@ -339,6 +367,10 @@ func marcarPresenca(ctx context.Context) error {
|
||||
return nil
|
||||
}),
|
||||
); err != nil {
|
||||
// Check if the error was due to maximum time reached
|
||||
if err.Error() == "maximum time reached" {
|
||||
return err
|
||||
}
|
||||
log.Printf("Attempt %d: Failed to register attendance: %v", attempt, err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user