-
Notifications
You must be signed in to change notification settings - Fork 479
/
acfun.go
126 lines (114 loc) · 2.97 KB
/
acfun.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package acfun
import (
"net/http"
"net/url"
"strconv"
"strings"
"github.com/hr3lxphr6j/requests"
"github.com/tidwall/gjson"
"github.com/hr3lxphr6j/bililive-go/src/live"
"github.com/hr3lxphr6j/bililive-go/src/live/internal"
"github.com/hr3lxphr6j/bililive-go/src/pkg/utils"
)
const (
domain = "live.acfun.cn"
cnName = "acfun"
roomInfoApi = "https://live.acfun.cn/api/live/info"
loginApi = "https://id.app.acfun.cn/rest/app/visitor/login"
liveInfoApi = "https://api.kuaishouzt.com/rest/zt/live/web/startPlay"
)
func init() {
live.Register(domain, new(builder))
}
type builder struct{}
func (b *builder) Build(url *url.URL, opt ...live.Option) (live.Live, error) {
return &Live{
BaseLive: internal.NewBaseLive(url, opt...),
}, nil
}
type Live struct {
internal.BaseLive
}
func (l *Live) GetInfo() (info *live.Info, err error) {
paths := strings.Split(l.Url.Path, "/")
if len(paths) < 2 {
return nil, live.ErrRoomUrlIncorrect
}
resp, err := requests.Get(roomInfoApi, live.CommonUserAgent, requests.Query("authorId", paths[2]))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
res := gjson.ParseBytes(body)
return &live.Info{
Live: l,
HostName: res.Get("user.name").String(),
RoomName: res.Get("title").String(),
Status: res.Get("liveId").Exists(),
}, nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
did := "web_" + utils.GenRandomName(16)
resp, err := requests.Post(
loginApi,
live.CommonUserAgent,
requests.Form(map[string]string{"sid": "acfun.api.visitor"}),
requests.Cookie("_did", did),
)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrInternalError
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
res := gjson.ParseBytes(body)
userId := res.Get("userId").Int()
visitorSt := res.Get(`acfun\.api\.visitor_st`).String()
resp, err = requests.Post(liveInfoApi,
live.CommonUserAgent,
requests.Queries(map[string]string{
"subBiz": "mainApp",
"kpn": "ACFUN_APP",
"kpf": "PC_WEB",
"userId": strconv.FormatInt(userId, 10),
"did": did,
"acfun.api.visitor_st": visitorSt,
}),
requests.Form(map[string]string{
"authorId": strings.Split(l.Url.Path, "/")[2],
"pullStreamType": "FLV",
}),
requests.Referer(l.GetRawUrl()),
)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrInternalError
}
body, err = resp.Bytes()
if err != nil {
return nil, err
}
representationsJSON := gjson.Get(
gjson.GetBytes(body, "data.videoPlayRes").String(),
"liveAdaptiveManifest.0.adaptationSet.representation").String()
rs, err := newRepresentationsFromJSON(representationsJSON)
if err != nil {
return nil, err
}
return rs.GenUrls()
}
func (l *Live) GetPlatformCNName() string {
return cnName
}