From a720c2c16b442b668db465fbbc70740cfc7ddee4 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 8 Sep 2022 00:31:26 +0800 Subject: Load config from file or cli. --- recording/config.go | 29 ++++++++++++++++++++++++++--- recording/runner.go | 7 +++++-- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'recording') diff --git a/recording/config.go b/recording/config.go index 1a24508..bcd5755 100644 --- a/recording/config.go +++ b/recording/config.go @@ -1,6 +1,9 @@ package recording -import "bilibili-livestream-archiver/common" +import ( + "bilibili-livestream-archiver/common" + "fmt" +) type TaskConfig struct { RoomId common.RoomId `mapstructure:"room_id"` @@ -15,6 +18,26 @@ type TransportConfig struct { } type DownloadConfig struct { - SaveDirectory string `mapstructure:"save_directory"` - FileNameTemplate string `mapstructure:"file_name_template"` + SaveDirectory string `mapstructure:"save_directory"` +} + +func DefaultTransportConfig() TransportConfig { + return TransportConfig{ + SocketTimeoutSeconds: 10, + RetryIntervalSeconds: 2, + MaxRetryTimes: 5, + } +} + +func (t TaskConfig) String() string { + return fmt.Sprintf("room: %v, %v, %v", t.RoomId, t.Transport.String(), t.Download.String()) +} + +func (t TransportConfig) String() string { + return fmt.Sprintf("socket timeout: %vs, retry interval: %vs, max retry times: %v", + t.SocketTimeoutSeconds, t.RetryIntervalSeconds, t.MaxRetryTimes) +} + +func (d DownloadConfig) String() string { + return fmt.Sprintf("save directory: %v", d.SaveDirectory) } diff --git a/recording/runner.go b/recording/runner.go index a4693fb..d4e5a96 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -10,10 +10,12 @@ import ( "bilibili-livestream-archiver/common" "context" "encoding/json" + "errors" "fmt" "log" "os" "path" + "sync" "time" ) @@ -25,7 +27,8 @@ type TaskResult struct { // RunTask start a monitor&download task and // put its execution result into a channel. -func RunTask(ctx context.Context, task *TaskConfig, chTaskResult chan<- TaskResult) { +func RunTask(ctx context.Context, wg *sync.WaitGroup, task *TaskConfig, chTaskResult chan<- TaskResult) { + defer wg.Done() err := doTask(ctx, task) chTaskResult <- TaskResult{ Task: task, @@ -146,7 +149,7 @@ func record( fileName := fmt.Sprintf( "%s.%s", GenerateFileName(profile.Data.Title, time.Now()), - common.Optional[string](common.GetFileExtensionFromUrl(streamSource.URL)).OrElse("flv"), + common.Errorable[string](common.GetFileExtensionFromUrl(streamSource.URL)).OrElse("flv"), ) filePath := path.Join(task.Download.SaveDirectory, fileName) file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) -- cgit v1.2.3