Skip to content

Commit

Permalink
fix huya lol fragment bug (hr3lxphr6j#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
kira1928 authored Jul 19, 2024
1 parent 4f23b9b commit 210b9d9
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 134 deletions.
15 changes: 10 additions & 5 deletions src/live/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (l *Live) GetInfo() (info *live.Info, err error) {
return info, nil
}

func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
func (l *Live) GetStreamInfos() (infos []*live.StreamUrlInfo, err error) {
if l.realID == "" {
if err := l.parseRealId(); err != nil {
return nil, err
Expand Down Expand Up @@ -177,7 +177,7 @@ func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
if err != nil {
return nil, err
}
urls := make([]string, 0, 4)
urlStrings := make([]string, 0, 4)
addr := ""

if l.Options.Quality == 0 && gjson.GetBytes(body, "data.playurl_info.playurl.stream.1.format.1.codec.#").Int() > 1 {
Expand All @@ -190,18 +190,23 @@ func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
gjson.GetBytes(body, addr+".url_info").ForEach(func(_, value gjson.Result) bool {
hosts := gjson.Get(value.String(), "host").String()
queries := gjson.Get(value.String(), "extra").String()
urls = append(urls, hosts+baseURL+queries)
urlStrings = append(urlStrings, hosts+baseURL+queries)
return true
})

return utils.GenUrls(urls...)
urls, err := utils.GenUrls(urlStrings...)
if err != nil {
return nil, err
}
infos = utils.GenUrlInfos(urls, l.getHeadersForDownloader())
return
}

func (l *Live) GetPlatformCNName() string {
return cnName
}

func (l *Live) GetHeadersForDownloader() map[string]string {
func (l *Live) getHeadersForDownloader() map[string]string {
agent := biliWebAgent
referer := l.GetRawUrl()
if l.Options.AudioOnly {
Expand Down
117 changes: 34 additions & 83 deletions src/live/huya/huya.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package huya

import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/http"
"net/url"
Expand All @@ -12,13 +10,11 @@ import (
"github.com/hr3lxphr6j/bililive-go/src/live/internal"
"github.com/hr3lxphr6j/bililive-go/src/pkg/utils"
"github.com/hr3lxphr6j/requests"
"github.com/tidwall/gjson"
)

const (
domain = "www.huya.com"
cnName = "虎牙"
uaApp = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003137) NetType/WIFI Language/zh_CN WeChat/8.0.49.33 CFNetwork/1474 Darwin/23.0.0"
)

func init() {
Expand All @@ -35,6 +31,22 @@ func (b *builder) Build(url *url.URL, opt ...live.Option) (live.Live, error) {

type Live struct {
internal.BaseLive
getInfoMethodIndex int
getStreamInfosMethodIndex int
LastCdnIndex int
}

type GetInfoMethod func(l *Live, body string) (*live.Info, error)
type GetStreamInfosMethod func(l *Live) ([]*live.StreamUrlInfo, error)

var GetInfoMethodList = []GetInfoMethod{
GetInfo_ForXingXiu,
GetInfo_ForLol,
}

var GetStreamInfosMethodList = []GetStreamInfosMethod{
GetStreamInfos_ForXingXiu,
GetStreamInfos_ForLol,
}

func (l *Live) GetHtmlBody() (htmlBody string, err error) {
Expand All @@ -50,38 +62,6 @@ func (l *Live) GetHtmlBody() (htmlBody string, err error) {
return
}

func (l *Live) getDate(htmlBody string) (result *gjson.Result, err error) {
strFilter := utils.NewStringFilterChain(utils.ParseUnicode, utils.UnescapeHTMLEntity)
rjson := strFilter.Do(utils.Match1(`stream: (\{"data".*?),"iWebDefaultBitRate"`, htmlBody)) + "}"
gj := gjson.Parse(rjson)

roomId := gj.Get("data.0.gameLiveInfo.profileRoom").String()
params := make(map[string]string)
params["m"] = "Live"
params["do"] = "profileRoom"
params["roomid"] = roomId
params["showSecret"] = "1"

headers := make(map[string]interface{})
headers["User-Agent"] = uaApp
headers["xweb_xhr"] = "1"
headers["referer"] = "https://servicewechat.com/wx74767bf0b684f7d3/301/page-frame.html"
headers["accept-language"] = "zh-CN,zh;q=0.9"
resp, err := requests.Get("https://mp.huya.com/cache.php", requests.Headers(headers), requests.Queries(params), requests.UserAgent(uaApp))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
body, err := resp.Text()
if err != nil {
return nil, err
}
res := gjson.Parse(body)
return &res, nil
}

func (l *Live) GetInfo() (info *live.Info, err error) {
body, err := l.GetHtmlBody()
if err != nil {
Expand All @@ -101,70 +81,41 @@ func (l *Live) GetInfo() (info *live.Info, err error) {
}, nil
}

res, err := l.getDate(body)
if err != nil {
return nil, err
getInfoMethodCount := len(GetInfoMethodList)
if getInfoMethodCount == 0 {
return nil, fmt.Errorf("no GetInfoMethod")
}

if res := utils.Match1("该主播不存在!", res.String()); res != "" {
return nil, live.ErrRoomNotExist
if l.getInfoMethodIndex >= getInfoMethodCount {
l.getInfoMethodIndex = 0
}

var (
hostName = res.Get("data.liveData.nick").String()
roomName = res.Get("data.liveData.introduction").String()
status = res.Get("data.realLiveStatus").String()
)

if hostName == "" || roomName == "" || status == "" {
return nil, live.ErrInternalError
}

info = &live.Info{
Live: l,
HostName: hostName,
RoomName: roomName,
Status: status == "ON",
}
return info, nil
}

func GetMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
info, err = GetInfoMethodList[l.getInfoMethodIndex](l, body)
l.getInfoMethodIndex++
return
}

func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
body, err := l.GetHtmlBody()
if err != nil {
return nil, err
}

data, err := l.getDate(body)
if err != nil {
return nil, err
func (l *Live) GetStreamInfos() (infos []*live.StreamUrlInfo, err error) {
getStreamUrlsMethodCount := len(GetStreamInfosMethodList)
if getStreamUrlsMethodCount == 0 {
return nil, fmt.Errorf("no GetStreamUrlsMethod")
}
sFlvUrl := data.Get("data.stream.baseSteamInfoList.0.sFlvUrl").String()
sStreamName := data.Get("data.stream.baseSteamInfoList.0.sStreamName").String()
sFlvUrlSuffix := data.Get("data.stream.baseSteamInfoList.0.sFlvUrlSuffix").String()
sFlvAntiCode := data.Get("data.stream.baseSteamInfoList.0.sFlvAntiCode").String()
streamUrl := fmt.Sprintf("%s/%s.%s?%s", sFlvUrl, sStreamName, sFlvUrlSuffix, sFlvAntiCode)

res, err := utils.GenUrls(streamUrl)
if err != nil {
return nil, err
if l.getStreamInfosMethodIndex >= getStreamUrlsMethodCount {
l.getStreamInfosMethodIndex = 0
}
return res, nil

infos, err = GetStreamInfosMethodList[l.getStreamInfosMethodIndex](l)
l.getStreamInfosMethodIndex++
return
}

func (l *Live) GetPlatformCNName() string {
return cnName
}

func (l *Live) GetHeadersForDownloader() map[string]string {
func getGeneralHeadersForDownloader() map[string]string {
return map[string]string{
"User-Agent": uaApp,
"Accept": `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`,
"Accept-Encoding": `gzip, deflate`,
"Accept-Language": `zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3`,
Expand Down
Loading

0 comments on commit 210b9d9

Please sign in to comment.