/
doupai.go
executable file
·53 lines (43 loc) · 1.2 KB
/
doupai.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
package parser
import (
"errors"
"fmt"
"net/url"
"github.com/tidwall/gjson"
"github.com/go-resty/resty/v2"
)
type douPai struct {
}
func (d douPai) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
urlInfo, err := url.Parse(shareUrl)
if err != nil {
return nil, errors.New("parse share url fail")
}
if len(urlInfo.Query()["id"]) <= 0 {
return nil, errors.New("can not parse video id from share url")
}
return d.parseVideoID(urlInfo.Query()["id"][0])
}
func (d douPai) parseVideoID(videoId string) (*VideoParseInfo, error) {
reqUrl := fmt.Sprintf("https://v2.doupai.cc/topic/%s.json", videoId)
headers := map[string]string{
HttpHeaderUserAgent: DefaultUserAgent,
}
client := resty.New()
res, err := client.R().
SetHeaders(headers).
Get(reqUrl)
if err != nil {
return nil, err
}
data := gjson.GetBytes(res.Body(), "data")
parseInfo := &VideoParseInfo{
Title: data.Get("name").String(),
VideoUrl: data.Get("videoUrl").String(),
CoverUrl: data.Get("imageUrl").String(),
}
parseInfo.Author.Uid = data.Get("userId.id").String()
parseInfo.Author.Name = data.Get("userId.name").String()
parseInfo.Author.Avatar = data.Get("userId.avatar").String()
return parseInfo, nil
}