github.com/antonyho/crhk-recorder@v0.0.0-20220803065738-4962dcafa06f/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/antonyho/crhk-recorder/pkg/dayofweek"
    11  	"github.com/antonyho/crhk-recorder/pkg/stream/recorder"
    12  )
    13  
    14  func main() {
    15  	var (
    16  		channel   string
    17  		startTime string
    18  		endTime   string
    19  		duration  time.Duration
    20  		weekdays  string
    21  		repeat    bool
    22  	)
    23  	flag.StringVar(&channel, "c", "881", "channel name in abbreviation")
    24  	flag.StringVar(&startTime, "s", "", "start time with timezone abbreviation")
    25  	flag.StringVar(&endTime, "e", "", "end time with timezone abbreviation")
    26  	flag.DurationVar(&duration, "d", 0, "record duration [don't do this over 24 hours]")
    27  	flag.StringVar(&weekdays, "w", "", "day of week on scheduled recording [comma seperated] [Sunday=0]")
    28  	flag.BoolVar(&repeat, "r", false, "repeat recording at scheduled time on next day")
    29  	flag.Parse()
    30  
    31  	if duration == 0 {
    32  		if startTime == "" && endTime == "" {
    33  			panic("record time value must be provided")
    34  		}
    35  	}
    36  
    37  	// startTime is set
    38  	// 		duration is not set
    39  	// 			endTime is set - stop at given endTime
    40  	// 			endTime is not set - can't record infinitely
    41  	// 		duration is set
    42  	// 			endTime is not set - go with given duration
    43  	// 			endTime is set - stop at given endTime, ignore duration
    44  	// startTime is not set
    45  	// 		duration is not set
    46  	// 			endTime is set - start now and stop at endTime
    47  	// 			endTime is not set - this condition is unreachable
    48  	// 		duration is set
    49  	// 			endTime is set - start now and stop at endTime
    50  	// 			endTime is not set - start now and go with given duration
    51  
    52  	rcdr := recorder.NewRecorder(channel)
    53  
    54  	if startTime == "" {
    55  		// Add a second delay to avoid skipping
    56  		startTime = time.Now().Add(time.Second).Format("15:04:05 -0700")
    57  	}
    58  
    59  	if duration > time.Duration(0) {
    60  		if endTime == "" {
    61  			// Change Schedule() accepts parameter types and create endTime
    62  			// With duration parameter, it will override the endTime
    63  			start, err := time.Parse("15:04:05 -0700", startTime)
    64  			if err != nil {
    65  				panic(err)
    66  			}
    67  			endTime = start.Add(duration).Format("15:04:05 -0700")
    68  		}
    69  	}
    70  
    71  	if endTime == "" {
    72  		panic("record time cannot be infinite")
    73  	}
    74  
    75  	dowMask := dayofweek.New()
    76  	if weekdays != "" {
    77  		days := strings.Split(weekdays, ",")
    78  		for _, day := range days {
    79  			day = strings.TrimSpace(day)
    80  			d, err := strconv.ParseUint(day, 10, 8)
    81  			if err != nil {
    82  				panic(fmt.Errorf("incorrect day of week parameter [%s]", weekdays))
    83  			} else if d >= 0 && d <= 6 {
    84  				dowMask.Enable(time.Weekday(d))
    85  			} else {
    86  				panic(fmt.Errorf("incorrect day of week parameter [%s]", weekdays))
    87  			}
    88  		}
    89  	} else {
    90  		if !repeat {
    91  			// Just now, just once.
    92  			dowMask.Enable(time.Now().Weekday()) // Hope you aren't starting at 23:59:59
    93  		} // Otherwise, all weeekdays.
    94  	}
    95  
    96  	if err := rcdr.Schedule(startTime, endTime, *dowMask, repeat); err != nil {
    97  		panic(err)
    98  	}
    99  }