Skip to content

Commit

Permalink
fix #47 修复快手解析
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunwei928 committed Oct 20, 2024
1 parent dd16b1f commit e8bd8bf
Showing 1 changed file with 25 additions and 44 deletions.
69 changes: 25 additions & 44 deletions parser/kuaishou.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package parser

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

"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
Expand All @@ -13,66 +14,46 @@ type kuaiShou struct{}

func (k kuaiShou) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
client := resty.New()
// disable redirects in the HTTP client, get params before redirects
client.SetRedirectPolicy(resty.NoRedirectPolicy())
res, err := client.R().
SetHeader(HttpHeaderUserAgent, DefaultUserAgent).
SetHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7").
Get(shareUrl)
// 非 resty.ErrAutoRedirectDisabled 错误时,返回错误
if !errors.Is(err, resty.ErrAutoRedirectDisabled) {
return nil, err
}

// 获取 cookies: did,didv
cookies := res.RawResponse.Cookies()
if len(cookies) <= 0 {
return nil, errors.New("get cookies from share url fail")
}

locationRes, err := res.RawResponse.Location()
if err != nil {
return nil, err
}

// 分享的中间跳转链接不太一样, 有些是 /fw/long-video , 有些 /fw/photo
referUri := strings.ReplaceAll(locationRes.String(), "v.m.chenzhongtech.com/fw/long-video", "m.gifshow.com/fw/photo")
referUri = strings.ReplaceAll(referUri, "v.m.chenzhongtech.com/fw/photo", "m.gifshow.com/fw/photo")
videoId := strings.ReplaceAll(strings.Trim(locationRes.Path, "/"), "fw/long-video/", "")
videoId = strings.ReplaceAll(videoId, "fw/photo/", "")
if len(videoId) <= 0 {
return nil, errors.New("parse video id from share url fail")
re := regexp.MustCompile(`window.INIT_STATE\s*=\s*(.*?)</script>`)
findRes := re.FindSubmatch(res.Body())
if len(findRes) < 2 {
return nil, errors.New("parse video json info from html fail")
}
jsonBytes := bytes.TrimSpace(findRes[1])

postData := map[string]interface{}{
"fid": "0",
"shareResourceType": "PHOTO_OTHER",
"shareChannel": "share_copylink",
"kpn": "KUAISHOU",
"subBiz": "BROWSE_SLIDE_PHOTO",
"env": "SHARE_VIEWER_ENV_TX_TRICK",
"h5Domain": "m.gifshow.com",
"photoId": videoId,
"isLongVideo": false,
var (
videoRes gjson.Result
isFindInfo bool
)

for _, jsonItem := range gjson.ParseBytes(jsonBytes).Map() {
jsonItemMap := jsonItem.Map()
_, hasResult := jsonItemMap["result"]
_, hasPhoto := jsonItemMap["photo"]
if hasResult && hasPhoto {
videoRes = jsonItem
isFindInfo = true
break
}
}
videoRes, err := client.R().
SetHeader("Origin", "https://m.gifshow.com").
SetHeader(HttpHeaderReferer, strings.ReplaceAll(referUri, "m.gifshow.com/fw/photo", "m.gifshow.com/fw/photo")).
SetHeader(HttpHeaderContentType, "application/json").
SetHeader(HttpHeaderUserAgent, DefaultUserAgent).
SetCookies(cookies).
SetBody(postData).
Post("https://m.gifshow.com/rest/wd/photo/info?kpn=KUAISHOU&captchaToken=")

if err != nil {
return nil, err
if !isFindInfo {
return nil, errors.New("parse video json fail")
}

if resultCode := gjson.GetBytes(videoRes.Body(), "result").Int(); resultCode != 1 {
if resultCode := videoRes.Get("result").Int(); resultCode != 1 {
return nil, fmt.Errorf("获取作品信息失败:result=%d", resultCode)
}

data := gjson.GetBytes(videoRes.Body(), "photo")
data := videoRes.Get("photo")
avatar := data.Get("headUrl").String()
author := data.Get("userName").String()
title := data.Get("caption").String()
Expand Down

0 comments on commit e8bd8bf

Please sign in to comment.