-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (88 loc) · 2.76 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"fmt"
"strconv"
"strings"
"time"
"github.com/antonyho/crhk-recorder/pkg/dayofweek"
"github.com/antonyho/crhk-recorder/pkg/stream/recorder"
)
func main() {
var (
channel string
startTime string
endTime string
duration time.Duration
weekdays string
repeat bool
)
flag.StringVar(&channel, "c", "881", "channel name in abbreviation")
flag.StringVar(&startTime, "s", "", "start time with timezone abbreviation")
flag.StringVar(&endTime, "e", "", "end time with timezone abbreviation")
flag.DurationVar(&duration, "d", 0, "record duration [don't do this over 24 hours]")
flag.StringVar(&weekdays, "w", "", "day of week on scheduled recording [comma seperated] [Sunday=0]")
flag.BoolVar(&repeat, "r", false, "repeat recording at scheduled time on next day")
flag.Parse()
if duration == 0 {
if startTime == "" && endTime == "" {
panic("record time value must be provided")
}
}
// startTime is set
// duration is not set
// endTime is set - stop at given endTime
// endTime is not set - can't record infinitely
// duration is set
// endTime is not set - go with given duration
// endTime is set - stop at given endTime, ignore duration
// startTime is not set
// duration is not set
// endTime is set - start now and stop at endTime
// endTime is not set - this condition is unreachable
// duration is set
// endTime is set - start now and stop at endTime
// endTime is not set - start now and go with given duration
rcdr := recorder.NewRecorder(channel)
if startTime == "" {
// Add a second delay to avoid skipping
startTime = time.Now().Add(time.Second).Format("15:04:05 -0700")
}
if duration > time.Duration(0) {
if endTime == "" {
// Change Schedule() accepts parameter types and create endTime
// With duration parameter, it will override the endTime
start, err := time.Parse("15:04:05 -0700", startTime)
if err != nil {
panic(err)
}
endTime = start.Add(duration).Format("15:04:05 -0700")
}
}
if endTime == "" {
panic("record time cannot be infinite")
}
dowMask := dayofweek.New()
if weekdays != "" {
days := strings.Split(weekdays, ",")
for _, day := range days {
day = strings.TrimSpace(day)
d, err := strconv.ParseUint(day, 10, 8)
if err != nil {
panic(fmt.Errorf("incorrect day of week parameter [%s]", weekdays))
} else if d >= 0 && d <= 6 {
dowMask.Enable(time.Weekday(d))
} else {
panic(fmt.Errorf("incorrect day of week parameter [%s]", weekdays))
}
}
} else {
if !repeat {
// Just now, just once.
dowMask.Enable(time.Now().Weekday()) // Hope you aren't starting at 23:59:59
} // Otherwise, all weeekdays.
}
if err := rcdr.Schedule(startTime, endTime, *dowMask, repeat); err != nil {
panic(err)
}
}