Skip to content

Commit

Permalink
v1.3.0: add alivodkit
Browse files Browse the repository at this point in the history
  • Loading branch information
mizuki1412 committed Mar 27, 2022
1 parent 743e00a commit ef4ef5f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 19 deletions.
2 changes: 1 addition & 1 deletion class/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion library/timekit/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package timekit

import "fmt"

// 毫秒格式化
// FormatMillSecondHMS 毫秒格式化 hh:mm:ss
func FormatMillSecondHMS(mill int64) string {
mill = mill / 1000
hh := mill / 60 / 60
Expand Down
14 changes: 0 additions & 14 deletions library/timekit/time_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions library/timekit/time_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package timekit

import "time"

// 是否存在交集,不含点
// IsOverlap 是否存在交集,不含点
func IsOverlap(base []time.Time, layout []time.Time) bool {
if len(base) != 2 || len(layout) != 2 {
return false
}
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()
Expand All @@ -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()
Expand Down
119 changes: 119 additions & 0 deletions service-third/alivodkit/alivod.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ef4ef5f

Please sign in to comment.