-
Notifications
You must be signed in to change notification settings - Fork 498
/
huajiao.go
175 lines (154 loc) · 3.84 KB
/
huajiao.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package huajiao
import (
"net/http"
"net/url"
"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 = "www.huajiao.com"
cnName = "花椒"
apiUserInfo = "https://webh.huajiao.com/User/getUserInfo"
apiUserFeeds = "https://webh.huajiao.com/User/getUserFeeds"
apiStream = "https://live.huajiao.com/live/substream"
)
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 {
uid string
internal.BaseLive
}
func (l *Live) getUid() (string, error) {
if l.uid != "" {
return l.uid, nil
}
var uid string
if uid = utils.Match1(`https?:\/\/www.huajiao.com\/user\/(\d+)`, l.GetRawUrl()); uid != "" {
// nothing to do
} else if liveId := utils.Match1(`https?:\/\/www.huajiao.com\/l\/(\d+)`, l.GetRawUrl()); liveId != "" {
resp, err := requests.Get(l.GetRawUrl(), live.CommonUserAgent)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", live.ErrRoomNotExist
}
body, err := resp.Text()
if err != nil {
return "", err
}
uid = utils.Match1(`<span class="js-author-id">(\d+)</span>`, body)
// if uid == "" {
// TODO: error log
// }
}
if uid != "" && uid != "0" {
l.uid = uid
return l.uid, nil
} else {
return "", live.ErrRoomUrlIncorrect
}
}
func (l *Live) getNickname(uid string) (string, error) {
resp, err := requests.Get(apiUserInfo, live.CommonUserAgent, requests.Query("fmt", "json"), requests.Query("uid", uid))
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil {
return "", err
}
if errno := gjson.GetBytes(body, "errno").Int(); errno != 0 {
return "", live.ErrRoomNotExist
}
return gjson.GetBytes(body, "data.nickname").String(), nil
}
func (l *Live) getLiveFeeds(uid string) ([]gjson.Result, error) {
resp, err := requests.Get(apiUserFeeds, live.CommonUserAgent, requests.Query("fmt", "json"), requests.Query("uid", uid))
if err != nil {
return nil, err
}
feedsData, err := resp.Bytes()
if err != nil {
return nil, err
}
return gjson.GetBytes(feedsData, "data.feeds.#(type==1)#").Array(), nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
uid, err := l.getUid()
if err != nil {
return nil, err
}
info = &live.Info{
Live: l,
HostName: "",
RoomName: "",
Status: false,
CustomLiveId: "huajiao/" + uid,
}
nickname, err := l.getNickname(uid)
if err != nil {
return nil, err
}
info.HostName = nickname
feeds, err := l.getLiveFeeds(uid)
if err != nil {
return nil, err
}
if len(feeds) == 0 {
return info, nil
}
info.RoomName = feeds[0].Get("feed.title").String()
info.Status = true
return info, nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
uid, err := l.getUid()
if err != nil {
return nil, err
}
feeds, err := l.getLiveFeeds(uid)
if err != nil {
return nil, err
}
if len(feeds) == 0 {
return nil, live.ErrInternalError
}
var (
sn = feeds[0].Get("feed.sn").String()
liveID = feeds[0].Get("feed.relateid").String()
)
resp, err := requests.Get(apiStream, live.CommonUserAgent, requests.Queries(map[string]string{
"sn": sn,
"uid": uid,
"liveid": liveID,
"encode": "h264",
}))
if err != nil {
return nil, err
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
if errno := gjson.GetBytes(body, "errno").Int(); errno != 0 {
return nil, live.ErrInternalError
}
return utils.GenUrls(gjson.GetBytes(body, "data.main").String())
}
func (l *Live) GetPlatformCNName() string {
return cnName
}