Skip to content

Commit

Permalink
add interval run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Jacob committed Nov 9, 2023
1 parent d122b68 commit 6613328
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
parser := argparse.NewParser("asw-stundenplan", "scrapes all timetable events from ASW gGmbH ans saves them as ics")
outputdir := parser.String("o", "out", &argparse.Options{Required: false, Help: "output directory", Default: "out"})
timezone := parser.String("t", "timezone", &argparse.Options{Required: false, Help: "timezone", Default: "Europe/Berlin"})
interval := parser.Int("i", "interval", &argparse.Options{Required: false, Help: "interval", Default: nil})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
Expand All @@ -32,6 +33,34 @@ func main() {
if err != nil {
log.Fatalln(err)
}
if interval == nil || *interval == 0 {
run(tz, *outputdir)
} else {
log.Println(fmt.Sprintf("running in interval mode. Interval %d seconds", *interval))
ticker := time.NewTicker(time.Duration(*interval) * time.Second)
runing := true
runOnce := func() {
if runing {
return
}
runing = true
run(tz, *outputdir)
runing = false
}
runOnce()
go func() {
for {
select {
case <-ticker.C:
runOnce()
}
}
}()
<-make(chan struct{})
}
}

func run(tz *time.Location, outputdir string) {
log.Println("scraping timetable urls")
ttm, err := timetablelist.GetTimeTableListDefault()
if err != nil {
Expand Down Expand Up @@ -126,7 +155,7 @@ func main() {
for id, e := range ce {
cal.AddVEvent(ical.ConvertEvent(e, id, tz))
}
af, err := os.Create(path.Join(*outputdir, cn+".ics"))
af, err := os.Create(path.Join(outputdir, cn+".ics"))
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 6613328

Please sign in to comment.