forked from iawia002/lux
/
yinyuetai.go
85 lines (77 loc) · 2.1 KB
/
yinyuetai.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
package yinyuetai
import (
"encoding/json"
"errors"
"fmt"
"github.com/iawia002/annie/extractors/types"
"github.com/iawia002/annie/request"
"github.com/iawia002/annie/utils"
)
const yinyuetaiAPI = "https://ext.yinyuetai.com/main/"
const (
actionGetMvInfo = "get-h-mv-info"
)
func genAPI(action string, param string) string {
return fmt.Sprintf("%s%s?json=true&%s", yinyuetaiAPI, action, param)
}
type extractor struct{}
// New returns a youtube extractor.
func New() types.Extractor {
return &extractor{}
}
// Extract is the main function to extract the data.
func (e *extractor) Extract(url string, option types.Options) ([]*types.Data, error) {
vid := utils.MatchOneOf(
url,
`https?://v.yinyuetai.com/video/(\d+)(?:\?vid=\d+)?`,
`https?://v.yinyuetai.com/video/h5/(\d+)(?:\?vid=\d+)?`,
`https?://m2.yinyuetai.com/video.html\?id=(\d+)`,
)
if vid == nil || len(vid) < 2 {
return nil, errors.New("invalid url for yinyuetai")
}
params := fmt.Sprintf("videoId=%s", vid[1])
// generate api url
apiURL := genAPI(actionGetMvInfo, params)
var err error
html, err := request.Get(apiURL, url, nil)
if err != nil {
return nil, err
}
// parse yinyuetai data
data := yinyuetaiMvData{}
if err = json.Unmarshal([]byte(html), &data); err != nil {
return nil, types.ErrURLParseFailed
}
// handle api error
if data.Error {
return nil, errors.New(data.Message)
}
if data.VideoInfo.CoreVideoInfo.Error {
return nil, errors.New(data.VideoInfo.CoreVideoInfo.ErrorMsg)
}
title := data.VideoInfo.CoreVideoInfo.VideoName
streams := make(map[string]*types.Stream, len(data.VideoInfo.CoreVideoInfo.VideoURLModels))
// set streams
for _, model := range data.VideoInfo.CoreVideoInfo.VideoURLModels {
urlData := &types.Part{
URL: model.VideoURL,
Size: model.FileSize,
Ext: "mp4",
}
streams[model.QualityLevel] = &types.Stream{
Parts: []*types.Part{urlData},
Size: model.FileSize,
Quality: model.QualityLevelName,
}
}
return []*types.Data{
{
Site: "音悦台 yinyuetai.com",
Title: title,
Type: types.DataTypeVideo,
Streams: streams,
URL: url,
},
}, nil
}