-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduler.go
79 lines (62 loc) · 1.54 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"strings"
"time"
"fmt"
"flag"
)
var recordAll = flag.Bool("recordAll", false, "records everything")
func Scheduler(recordC chan<- Showing, refreshC <-chan struct{}) (err error) {
var recordTitles []string
for {
fmt.Println("Schedule: ")
recordTitles, err = readRecordTitles()
if err != nil {
return
}
abortC := make(chan struct{})
if err = runSchedule(recordTitles, recordC, abortC); err != nil {
fmt.Println(err.Error())
time.Sleep(5 * time.Second)
}
<- refreshC
close(abortC)
fmt.Println()
}
return
}
func runSchedule(recordTitles []string, recordC chan<- Showing, abortC <-chan struct{}) (error) {
recordLookup := genRecordTitlesMap(recordTitles)
showings, err := FetchSchedule()
if err != nil {
return err
}
for _, showing := range showings {
if (!recordLookup[strings.ToLower(showing.Title)] && !*recordAll) || timeNow().After(showing.End) {
continue
}
go func(showing Showing) {
fmt.Printf("%s will record at %s\n", showing.Title, showing.Start.Local())
select {
case <-time.After(showing.Start.Sub(timeNow())):
// Note this will also run if we are in the middle of the recording
recordC <- showing
case <-abortC:
}
}(showing)
}
return nil
}
func genRecordTitlesMap(recordTitles []string) map[string]bool {
m := make(map[string]bool)
for _, title := range recordTitles {
m[strings.ToLower(title)] = true
}
return m
}
func readRecordTitles() ([]string, error) {
if err := ReadConfig(); err != nil {
return nil, err
}
return config.Titles, nil
}