Implement watch mode

This commit is contained in:
2024-05-16 10:04:36 +02:00
parent b18fdd5922
commit 9d164a43d5
2 changed files with 25 additions and 1 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module minie4/itslearningdl
go 1.21.5
go 1.22
require (
github.com/anaskhan96/soup v1.2.5

24
main.go
View File

@ -1,11 +1,13 @@
package main
import (
"flag"
"fmt"
"minie4/itslearningdl/itslearning"
"os"
"regexp"
"sync"
"time"
"github.com/charmbracelet/log"
"github.com/gosuri/uiprogress"
@ -22,7 +24,25 @@ type FSResource struct {
fsPath string
}
func watchForChanges(orig []itslearning.Course, itsl itslearning.Itslearning) itslearning.Course {
for {
time.Sleep(2 * time.Second)
logger.Info("Checking for changes")
newCourses := itslearning.QueryCourseList(itsl)
for i, course := range orig {
if course.LastUpdatedUtc != newCourses[i].LastUpdatedUtc {
logger.Info("Found change", "course", course.Title, "id", course.CourseId)
return course
}
}
logger.Debug("No changes")
}
}
func main() {
var doWatch = flag.Bool("w", false, "Watch for changes")
flag.Parse()
bars := uiprogress.New()
bars.Start()
@ -46,6 +66,10 @@ func main() {
itsl := itslearning.New(username, password, instance, user_agent)
courses := itslearning.QueryCourseList(itsl)
if *doWatch {
logger.Info("Entering watch mode. Checking for changes every :2 seconds...")
courses = []itslearning.Course{watchForChanges(courses, itsl)}
}
bar_course := bars.AddBar(len(courses)).AppendFunc(appendProgress)
bar_course.PrependFunc(func(b *uiprogress.Bar) string {