-
Notifications
You must be signed in to change notification settings - Fork 442
/
weibolive.go
108 lines (92 loc) · 2.45 KB
/
weibolive.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
package weibolive
import (
"net/http"
"net/url"
"strings"
"regexp"
"fmt"
"github.com/hr3lxphr6j/bililive-go/src/pkg/utils"
"github.com/hr3lxphr6j/requests"
"github.com/tidwall/gjson"
"github.com/hr3lxphr6j/bililive-go/src/live"
"github.com/hr3lxphr6j/bililive-go/src/live/internal"
)
const (
domain = "weibo.com"
cnName = "微博直播"
liveurl = "https://weibo.com/l/!/2/wblive/room/show_pc_live.json?live_id="
)
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
roomID string
}
func (l *Live) getRoomInfo() ([]byte, error) {
paths := strings.Split(l.Url.Path, "/")
if len(paths) < 5 {
return nil, live.ErrRoomUrlIncorrect
}
roomid := paths[5]
l.roomID = roomid
resp, err := requests.Get(liveurl+roomid,
live.CommonUserAgent,
requests.Headers(map[string]interface{}{
"Referer": l.Url,
}))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil || gjson.GetBytes(body, "error_code").Int() != 0 {
return nil, live.ErrRoomNotExist
}
return body, nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
body, err := l.getRoomInfo()
if err != nil {
return nil, live.ErrRoomNotExist
}
info = &live.Info{
Live: l,
HostName: gjson.GetBytes(body, "data.user.screenName").String(),
RoomName: gjson.GetBytes(body, "data.title").String(),
Status: gjson.GetBytes(body, "data.status").String() == "1",
CustomLiveId: "weibolive/" + l.roomID,
}
return info, nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
body, err := l.getRoomInfo()
if err != nil {
return nil, live.ErrRoomNotExist
}
streamurl := gjson.GetBytes(body, "data.live_origin_flv_url").String()
queryParams := l.Url.Query()
quality := queryParams.Get("q")
if quality != "" {
targetQuality := "_wb" + quality + "avc.flv"
reg, err := regexp.Compile(`_wb[\d]+avc\.flv`)
if err == nil && reg.MatchString(streamurl) {
streamurl = reg.ReplaceAllString(streamurl, targetQuality)
} else {
streamurl = strings.Replace(streamurl, ".flv", targetQuality, -1)
}
fmt.Println("weibo stream quality fixed: " + streamurl)
}
return utils.GenUrls(streamurl)
}
func (l *Live) GetPlatformCNName() string {
return cnName
}