From ef4ef5f54aaa2dcb4587d31e47ee8ffa2e685335 Mon Sep 17 00:00:00 2001 From: yingchaojie Date: Mon, 28 Mar 2022 01:23:12 +0800 Subject: [PATCH] v1.3.0: add alivodkit --- class/time.go | 2 +- library/timekit/format.go | 2 +- library/timekit/time_transfer.go | 14 ---- library/timekit/time_utils.go | 6 +- service-third/alivodkit/alivod.go | 119 ++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 service-third/alivodkit/alivod.go diff --git a/class/time.go b/class/time.go index 26cc574..d9932f6 100644 --- a/class/time.go +++ b/class/time.go @@ -111,5 +111,5 @@ func (th *Time) Set(val any) *Time { } func (th *Time) UnixMill() int64 { - return timekit.GetUnixMill(th.Time) + return th.Time.UnixMilli() } diff --git a/library/timekit/format.go b/library/timekit/format.go index bcec53c..14a35b9 100644 --- a/library/timekit/format.go +++ b/library/timekit/format.go @@ -2,7 +2,7 @@ package timekit import "fmt" -// 毫秒格式化 +// FormatMillSecondHMS 毫秒格式化 hh:mm:ss func FormatMillSecondHMS(mill int64) string { mill = mill / 1000 hh := mill / 60 / 60 diff --git a/library/timekit/time_transfer.go b/library/timekit/time_transfer.go index e6c18c7..f22bfa4 100644 --- a/library/timekit/time_transfer.go +++ b/library/timekit/time_transfer.go @@ -7,20 +7,6 @@ import ( "time" ) -// UnixMill 毫秒时间戳解析为Time -// Deprecated -func UnixMill(t int64) time.Time { - return time.UnixMilli(t) - //return time.Unix(t/1000, t%1000*1000000) -} - -// GetUnixMill -// Deprecated -func GetUnixMill(t time.Time) int64 { - //return t.UnixNano() / 1e6 - return t.UnixMilli() -} - // ParseString cast.StringToDate 不能设置时区 func ParseString(dtString string, layout string) (time.Time, error) { return time.ParseInLocation(layout, dtString, time.Local) diff --git a/library/timekit/time_utils.go b/library/timekit/time_utils.go index 3d7beac..b1ad210 100644 --- a/library/timekit/time_utils.go +++ b/library/timekit/time_utils.go @@ -2,7 +2,7 @@ package timekit import "time" -// 是否存在交集,不含点 +// IsOverlap 是否存在交集,不含点 func IsOverlap(base []time.Time, layout []time.Time) bool { if len(base) != 2 || len(layout) != 2 { return false @@ -10,7 +10,7 @@ func IsOverlap(base []time.Time, layout []time.Time) bool { return base[1].Unix() > layout[0].Unix() && base[0].Unix() < layout[1].Unix() } -// 取base中不含layout的部分 +// GetNotOverlapArray 取base中不含layout的部分 func GetNotOverlapArray(base []time.Time, layout []time.Time) ([]time.Time, []time.Time) { base1 := base[0].Unix() base2 := base[1].Unix() @@ -30,7 +30,7 @@ func GetNotOverlapArray(base []time.Time, layout []time.Time) ([]time.Time, []ti } } -// 合并两组时间,返回nil则未合并 +// MergeArray 合并两组时间,返回nil则未合并 func MergeArray(base []time.Time, layout []time.Time) []time.Time { base1 := base[0].Unix() base2 := base[1].Unix() diff --git a/service-third/alivodkit/alivod.go b/service-third/alivodkit/alivod.go new file mode 100644 index 0000000..26b0470 --- /dev/null +++ b/service-third/alivodkit/alivod.go @@ -0,0 +1,119 @@ +package alivodkit + +import ( + "fmt" + "github.com/aliyun/alibaba-cloud-sdk-go/sdk" + "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" + "github.com/aliyun/alibaba-cloud-sdk-go/services/vod" + "github.com/mizuki1412/go-core-kit/class" + "github.com/mizuki1412/go-core-kit/class/exception" + "github.com/mizuki1412/go-core-kit/init/configkey" + "github.com/mizuki1412/go-core-kit/library/timekit" + "github.com/mizuki1412/go-core-kit/service/configkit" + "github.com/spf13/cast" + "sync" +) + +var client *vod.Client +var once sync.Once + +// InitVodClient accessKeyId string, accessKeySecret string +func InitVodClient(keys ...string) { + once.Do(func() { + var accessKeyId string + var accessKeySecret string + if len(keys) == 2 { + accessKeyId = keys[0] + accessKeySecret = keys[1] + } else { + accessKeyId = configkit.GetStringD(configkey.AliAccessKey) + accessKeySecret = configkit.GetStringD(configkey.AliAccessKeySecret) + } + // 点播服务接入地域 + regionId := configkit.GetString(configkey.AliRegionId, "cn-shanghai") + // 创建授权对象 + credential := &credentials.AccessKeyCredential{ + AccessKeyId: accessKeyId, + AccessKeySecret: accessKeySecret, + } + // 自定义config + config := sdk.NewConfig() + config.AutoRetry = true // 失败是否自动重试 + config.MaxRetryTime = 3 // 最大重试次数 + config.Timeout = 3000000000 // 连接超时,单位:纳秒;默认为3秒 + var err error + client, err = vod.NewClientWithOptions(regionId, config, credential) + if err != nil { + panic(exception.New(err.Error())) + } + }) +} + +type SearchMediaInfoListParam struct { + Title string +} + +// SearchMediaInfoList return {id, duration/hh:mm:ss, size/MB,cover,status, dt} +// 注意ram账号授权相关api的操作 +// https://help.aliyun.com/document_detail/86044.htm?spm=a2c4g.11186623.0.0.3cd418971FU74d#doc-api-vod-SearchMedia +func SearchMediaInfoList(p SearchMediaInfoListParam) []map[string]any { + InitVodClient() + request := vod.CreateSearchMediaRequest() + request.Fields = "Duration,Size,CoverURL,Status" + request.Match = fmt.Sprintf("Title='%s'", p.Title) + res, err := client.SearchMedia(request) + if err != nil { + panic(exception.New(err.Error())) + } + ret := make([]map[string]any, 0, len(res.MediaList)) + for _, e := range res.MediaList { + ret = append(ret, map[string]any{ + "id": e.MediaId, + "dt": timekit.ParseD(e.CreationTime).Format(timekit.TimeLayout), + "cover": e.Video.CoverURL, + "status": e.Video.Status, + "size": class.NewDecimal(e.Video.Size).Div(class.NewDecimal(1024)).DivRound(class.NewDecimal(1024), 2).Float64(), + "duration": timekit.FormatSecondHMS(cast.ToInt64(e.Video.Duration), false), + }) + } + return ret +} + +// GetPlayInfo https://help.aliyun.com/document_detail/56124.html +func GetPlayInfo(videoId string) []map[string]any { + request := vod.CreateGetPlayInfoRequest() + request.VideoId = videoId + // Definition Url + res, err := client.GetPlayInfo(request) + if err != nil { + panic(exception.New(err.Error())) + } + ret := make([]map[string]any, 0, len(res.PlayInfoList.PlayInfo)) + for _, e := range res.PlayInfoList.PlayInfo { + ret = append(ret, map[string]any{ + "definition": getDefinitionName(e.Definition), + "url": e.PlayURL, + }) + } + return ret +} + +func getDefinitionName(val string) string { + switch val { + case "FD": + return "流畅" + case "LD": + return "标清" + case "SD": + return "高清" + case "HD": + return "超清" + case "OD": + return "原画" + case "SQ": + return "普通音质" + case "HQ": + return "高音质" + } + return val +}