Skip to content

Commit

Permalink
feature: 部分渠道区分 resty.ErrAutoRedirectDisabled 错误,精细化控制错误返回
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunwei928 committed Apr 11, 2024
1 parent 95b9806 commit 599ca00
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
8 changes: 6 additions & 2 deletions parser/douyin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ func (d douYin) parseVideoID(videoId string) (*VideoParseInfo, error) {

func (d douYin) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
client := resty.New()
// disable redirects in the HTTP client, get params before redirects
client.SetRedirectPolicy(resty.NoRedirectPolicy())
res, _ := client.R().
res, err := client.R().
SetHeader(HttpHeaderUserAgent, DefaultUserAgent).
Get(shareUrl)
// 这里会返回err, auto redirect is disabled
// 非 resty.ErrAutoRedirectDisabled 错误时,返回错误
if !errors.Is(err, resty.ErrAutoRedirectDisabled) {
return nil, err
}

locationRes, err := res.RawResponse.Location()
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions parser/huoshan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ func (h huoShan) parseVideoID(videoId string) (*VideoParseInfo, error) {

func (h huoShan) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
client := resty.New()
// disable redirects in the HTTP client, get params before redirects
client.SetRedirectPolicy(resty.NoRedirectPolicy())
res, _ := client.R().
res, err := client.R().
SetHeader(HttpHeaderUserAgent, DefaultUserAgent).
Get(shareUrl)
// 这里会返回err, auto redirect is disabled
// 非 resty.ErrAutoRedirectDisabled 错误时,返回错误
if !errors.Is(err, resty.ErrAutoRedirectDisabled) {
return nil, err
}

locationRes, err := res.RawResponse.Location()
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions parser/kuaishou.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ 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, _ := client.R().
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)
//这里会返回err, auto redirect is disabled
// 非 resty.ErrAutoRedirectDisabled 错误时,返回错误
if !errors.Is(err, resty.ErrAutoRedirectDisabled) {
return nil, err
}

// 获取 cookies: did,didv
cookies := res.RawResponse.Cookies()
Expand Down
8 changes: 6 additions & 2 deletions parser/pipixia.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ func (p piPiXia) parseVideoID(videoId string) (*VideoParseInfo, error) {

func (p piPiXia) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
client := resty.New()
// disable redirects in the HTTP client, get params before redirects
client.SetRedirectPolicy(resty.NoRedirectPolicy())
res, _ := client.R().
res, err := client.R().
SetHeader(HttpHeaderUserAgent, DefaultUserAgent).
Get(shareUrl)
// 这里会返回err, auto redirect is disabled
// 非 resty.ErrAutoRedirectDisabled 错误时,返回错误
if !errors.Is(err, resty.ErrAutoRedirectDisabled) {
return nil, err
}

locationRes, err := res.RawResponse.Location()
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions parser/xigua.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ type xiGua struct {

func (x xiGua) parseShareUrl(shareUrl string) (*VideoParseInfo, error) {
client := resty.New()
// disable redirects in the HTTP client, get params before redirects
client.SetRedirectPolicy(resty.NoRedirectPolicy())
res, _ := client.R().
res, err := client.R().
SetHeader(HttpHeaderUserAgent, DefaultUserAgent).
Get(shareUrl)
// 这里会返回err, auto redirect is disabled
// 非 resty.ErrAutoRedirectDisabled 错误时,返回错误
if !errors.Is(err, resty.ErrAutoRedirectDisabled) {
return nil, err
}

locationRes, err := res.RawResponse.Location()
if err != nil {
Expand Down

0 comments on commit 599ca00

Please sign in to comment.