-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
huya.go
160 lines (143 loc) · 3.82 KB
/
huya.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
package tv
import (
"encoding/base64"
"fmt"
"html"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/go-olive/tv/util"
)
func init() {
registerSite("huya", &huya{})
}
type huya struct {
base
}
func (this *huya) Snap(tv *Tv) error {
tv.Info = &Info{
Timestamp: time.Now().Unix(),
}
options := []Option{
this.setRoomOn(),
this.setStreamURL(),
}
for _, option := range options {
if err := option(tv); err != nil {
return err
}
}
return nil
}
func (this *huya) Name() string {
return "虎牙"
}
func (this *huya) streamURL(roomID string) (string, error) {
roomURL := fmt.Sprintf("https://m.huya.com/%s", roomID)
userAgent := "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko); Chrome/75.0.3770.100 Mobile Safari/537.36 "
req := &util.HttpRequest{
URL: roomURL,
Method: "GET",
ResponseData: *new(string),
ContentType: "application/x-www-form-urlencoded",
Header: map[string]string{
"User-Agent": userAgent,
},
}
if err := req.Send(); err != nil {
return "", err
}
respBody := fmt.Sprint(req.ResponseData)
re := regexp.MustCompile(`liveLineUrl":"([^"]+)",`)
res := re.FindStringSubmatch(respBody)
if len(res) > 0 { //有直播链接
u := res[1]
if len(u) > 0 {
decodedRet, _ := base64.StdEncoding.DecodeString(u)
decodedUrl := string(decodedRet)
if strings.Contains(decodedUrl, "replay") { //重播
return "https:" + u, nil
} else {
liveLineUrl := this.proc(decodedUrl)
liveLineUrl = strings.Replace(liveLineUrl, "hls", "flv", -1)
liveLineUrl = strings.Replace(liveLineUrl, "m3u8", "flv", -1)
return "https:" + liveLineUrl, nil
}
}
}
return "", nil
}
func (this *huya) proc(e string) string {
i := strings.Split(e, "?")[0]
b := strings.Split(e, "?")[1]
r := strings.Split(i, "/")
re := regexp.MustCompile(".(flv|m3u8)")
s := re.ReplaceAllString(r[len(r)-1], "")
srcAntiCode := html.UnescapeString(b)
c := strings.Split(srcAntiCode, "&")
cc := c[:0]
n := make(map[string]string)
for _, x := range c {
if len(x) > 0 {
cc = append(cc, x)
ss := strings.Split(x, "=")
n[ss[0]] = ss[1]
}
}
c = cc
fm, _ := url.QueryUnescape(n["fm"])
uu, _ := base64.StdEncoding.DecodeString(fm)
u := string(uu)
p := strings.Split(u, "_")[0]
f := strconv.FormatInt(time.Now().UnixNano()/100, 10)
l := n["wsTime"]
t := "0"
h := p + "_" + t + "_" + s + "_" + f + "_" + l
m := util.GetMd5Hash(h)
url := fmt.Sprintf("%s?wsSecret=%s&wsTime=%s&u=%s&seqid=%s&txyp=%s&fs=%s&sphdcdn=%s&sphdDC=%s&sphd=%s&u=0&t=100&sv=", i, m, l, t, f, n["txyp"], n["fs"], n["sphdcdn"], n["sphdDC"], n["sphd"])
return url
}
func (this *huya) setRoomOn() Option {
return func(tv *Tv) error {
webUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
roomURL := fmt.Sprintf("https://www.huya.com/%s", tv.RoomID)
req := &util.HttpRequest{
URL: roomURL,
Method: "GET",
ResponseData: *new(string),
ContentType: "application/x-www-form-urlencoded",
Header: map[string]string{
"User-Agent": webUserAgent,
},
}
if err := req.Send(); err != nil {
return err
}
resp := fmt.Sprint(req.ResponseData)
tv.roomOn = strings.Contains(resp, `"isOn":true`)
titleRe := regexp.MustCompile(`host-title" title="([^"]+)">`)
titleSubmatch := titleRe.FindAllStringSubmatch(resp, -1)
titleRes := make([]string, 0)
for _, v := range titleSubmatch {
titleRes = append(titleRes, string(v[1]))
}
if len(titleRes) > 0 {
tv.roomName = titleRes[0]
}
return nil
}
}
func (this *huya) setStreamURL() Option {
return func(tv *Tv) (err error) {
if !tv.roomOn {
return nil
}
tv.streamUrl, err = this.streamURL(tv.RoomID)
if !strings.Contains(tv.streamUrl, "https") {
tv.roomOn = false
}
return
}
}