Skip to content

Commit

Permalink
修复获取抖音信息的正则表达式;抖音同等分辨率下选择码率最高的流 (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
kira1928 authored Nov 4, 2023
1 parent 24edf57 commit 1d70c55
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ cookies:
3. make build-web
4. make
三、linux编译其他环境(以windows 为例)
1. GOOS=windows GOARCH=amd64 CGO_ENABLED=0 UPX_ENABLE=0 TAGS=dev ./src/hack/build.sh bililive
1. GOOS=windows GOARCH=amd64 CGO_ENABLED=0 UPX_ENABLE=0 TAGS=dev GCFLAGS="all=-N -l" ./src/hack/build.sh bililive
2.如果不需要调试,可以改成
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 UPX_ENABLE=0 TAGS=release ./src/hack/build.sh bililive
```
Expand Down
73 changes: 52 additions & 21 deletions src/live/douyin/douyin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

randomCookieChars = "1234567890abcdef"
roomIdCatcherRegex = `{\\"webrid\\":\\"([^"]+)\\"}`
mainInfoLineCatcherRegex = `self.__pace_f.push\(\[1,\s*"a:(\[.*\])\\n"\]\)`
mainInfoLineCatcherRegex = `self.__pace_f.push\(\[1,\s*"[^:]*:([^<]*,null,\{\\"state\\"[^<]*\])\\n"\]\)`
commonInfoLineCatcherRegex = `self.__pace_f.push\(\[1,\s*\"(\{.*\})\"\]\)`
)

Expand Down Expand Up @@ -95,26 +95,51 @@ func (l *Live) getLiveRoomWebPageResponse() (body string, err error) {
return
}

func (l *Live) getRoomInfoFromBody(body string) (info *live.Info, streamUrlInfos []live.StreamUrlInfo, err error) {
const errorMessageForErrorf = "getRoomInfoFromBody() failed on step %d"
stepNumberForLog := 1
mainInfoLine := utils.Match1(mainInfoLineCatcherRegex, body)
if mainInfoLine == "" {
err = fmt.Errorf(errorMessageForErrorf, stepNumberForLog)
func getMainInfoLine(body string) (json *gjson.Result, err error) {
reg, err := regexp.Compile(mainInfoLineCatcherRegex)
if err != nil {
return
}
stepNumberForLog++

// 获取房间信息
mainJson := gjson.Parse(fmt.Sprintf(`"%s"`, mainInfoLine))
if !mainJson.Exists() {
err = fmt.Errorf(errorMessageForErrorf+". Invalid json: %s", stepNumberForLog, mainInfoLine)
match := reg.FindAllStringSubmatch(body, -1)
if match == nil {
err = fmt.Errorf("0 match for mainInfoLineCatcherRegex: %s", mainInfoLineCatcherRegex)
return
}
for _, item := range match {
if len(item) < 2 {
// err = fmt.Errorf("len(item) = %d", len(item))
continue
}
mainInfoLine := item[1]

// 获取房间信息
mainJson := gjson.Parse(fmt.Sprintf(`"%s"`, mainInfoLine))
if !mainJson.Exists() {
// err = fmt.Errorf(errorMessageForErrorf+". Invalid json: %s", stepNumberForLog, mainInfoLine)
continue
}

mainJson = gjson.Parse(mainJson.String()).Get("3")
if !mainJson.Exists() {
// err = fmt.Errorf(errorMessageForErrorf+". Main json does not exist: %s", stepNumberForLog, mainInfoLine)
continue
}

fmt.Println(mainJson.Get("state.roomStore.roomInfo.room.status_str").String())
if mainJson.Get("state.roomStore.roomInfo.room.status_str").Exists() {
json = &mainJson
return
}
}
return nil, fmt.Errorf("MainInfoLine not found")
}

mainJson = gjson.Parse(mainJson.String()).Get("3")
if !mainJson.Exists() {
err = fmt.Errorf(errorMessageForErrorf+". Main json does not exist: %s", stepNumberForLog, mainInfoLine)
func (l *Live) getRoomInfoFromBody(body string) (info *live.Info, streamUrlInfos []live.StreamUrlInfo, err error) {
const errorMessageForErrorf = "getRoomInfoFromBody() failed on step %d"
stepNumberForLog := 1
mainJson, err := getMainInfoLine(body)
if err != nil {
err = fmt.Errorf(errorMessageForErrorf+". %s", stepNumberForLog, err.Error())
return
}

Expand All @@ -134,7 +159,7 @@ func (l *Live) getRoomInfoFromBody(body string) (info *live.Info, streamUrlInfos
streamIdPath := "state.streamStore.streamData.H264_streamData.common.stream"
streamId := mainJson.Get(streamIdPath).String()
if streamId == "" {
err = fmt.Errorf(errorMessageForErrorf+". %s does not exist: %s", stepNumberForLog, streamIdPath, mainInfoLine)
err = fmt.Errorf(errorMessageForErrorf+". %s does not exist", stepNumberForLog, streamIdPath)
return
}
stepNumberForLog++
Expand Down Expand Up @@ -187,7 +212,7 @@ func (l *Live) getRoomInfoFromBody(body string) (info *live.Info, streamUrlInfos
description.WriteString("\n")
return true
})
Priority := 0
Resolution := 0
resolution := strings.Split(paramsJson.Get("resolution").String(), "x")
if len(resolution) == 2 {
x, err := strconv.Atoi(resolution[0])
Expand All @@ -198,19 +223,25 @@ func (l *Live) getRoomInfoFromBody(body string) (info *live.Info, streamUrlInfos
if err != nil {
return true
}
Priority = x * y
Resolution = x * y
}
Vbitrate := int(paramsJson.Get("vbitrate").Int())
streamUrlInfos = append(streamUrlInfos, live.StreamUrlInfo{
Name: key.String(),
Description: description.String(),
Url: Url,
Priority: Priority,
Resolution: Resolution,
Vbitrate: Vbitrate,
})
return true
})
}
sort.Slice(streamUrlInfos, func(i, j int) bool {
return streamUrlInfos[i].Priority > streamUrlInfos[j].Priority
if streamUrlInfos[i].Resolution != streamUrlInfos[j].Resolution {
return streamUrlInfos[i].Resolution > streamUrlInfos[j].Resolution
} else {
return streamUrlInfos[i].Vbitrate > streamUrlInfos[j].Vbitrate
}
})
stepNumberForLog++

Expand Down
3 changes: 2 additions & 1 deletion src/live/lives.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ type StreamUrlInfo struct {
Url *url.URL
Name string
Description string
Priority int
Resolution int
Vbitrate int
}

type Live interface {
Expand Down

0 comments on commit 1d70c55

Please sign in to comment.