Skip to content

Commit

Permalink
支持小红书分享链接解析 #40
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunwei928 committed Sep 22, 2024
1 parent 3b9a30d commit fe5cc87
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Golang短视频去水印, 视频目前支持20个平台, 图集目前支持2个
## 视频
| 平台 | 状态 |
|----------|----|
| 小红书 ||
| 皮皮虾 ||
| 抖音短视频 ||
| 火山短视频 ||
Expand Down
47 changes: 47 additions & 0 deletions parser/redbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package parser

import (
"bytes"
"errors"
"fmt"
"regexp"

"github.com/tidwall/gjson"

"github.com/go-resty/resty/v2"
)

type redBook struct{}

func (r redBook) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
client := resty.New()
videoRes, err := client.R().
SetHeader(HttpHeaderUserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0").
Get(shareUrl)
if err != nil {
return nil, err
}

re := regexp.MustCompile(`window.__INITIAL_STATE__\s*=\s*(.*?)</script>`)
findRes := re.FindSubmatch(videoRes.Body())
if len(findRes) < 2 {
return nil, errors.New("parse video json info from html fail")
}

jsonBytes := bytes.TrimSpace(findRes[1])

nodeId := gjson.GetBytes(jsonBytes, "note.currentNoteId").String()
data := gjson.GetBytes(jsonBytes, fmt.Sprintf("note.noteDetailMap.%s.note", nodeId))
fmt.Println(data.Get("imageList").String())

parseInfo := &VideoParseInfo{
Title: data.Get("title").String(),
VideoUrl: data.Get("video.media.stream.h264.0.masterUrl").String(),
CoverUrl: data.Get("imageList.0.infoList.1.url").String(),
}
parseInfo.Author.Uid = data.Get("user.userId").String()
parseInfo.Author.Name = data.Get("user.nickname").String()
parseInfo.Author.Avatar = data.Get("user.avatar").String()

return parseInfo, nil
}
8 changes: 8 additions & 0 deletions parser/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
SourceSixRoom = "sixroom" // 六间房
SourceXinPianChang = "xinpianchang" // 新片场
SourceHaoKan = "haokan" // 好看视频
SourceRedBook = "redbook" // 小红书
)

// http 相关
Expand Down Expand Up @@ -174,4 +175,11 @@ var videoSourceInfoMapping = map[string]videoSourceInfo{
VideoShareUrlParser: haoKan{},
VideoIdParser: haoKan{},
},
SourceRedBook: {
VideoShareUrlDomain: []string{
"www.xiaohongshu.com",
"xhslink.com",
},
VideoShareUrlParser: redBook{},
},
}

0 comments on commit fe5cc87

Please sign in to comment.