Skip to content

Commit

Permalink
1. 添加 ParseVideoShareUrlByRegexp 方法, 直接通过分享信息调用
Browse files Browse the repository at this point in the history
2. 微视, 好看, 全民, 皮皮搞笑 兼容接口错误和视频状态错误
  • Loading branch information
wujunwei928 committed May 11, 2024
1 parent 0e829d5 commit f5140e8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 7 deletions.
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"os"
"os/signal"
"regexp"
"time"

"github.com/wujunwei928/parse-video/parser"
Expand All @@ -31,11 +30,8 @@ func main() {
})

r.GET("/video/share/url/parse", func(c *gin.Context) {
urlReg := regexp.MustCompile(`http[s]?:\/\/[\w.-]+[\w\/-]*[\w.-]*\??[\w=&:\-\+\%]*[/]*`)
videoShareUrl := urlReg.FindString(c.Query("url"))
//fmt.Println("123", videoShareUrl, c.Query("url"))

parseRes, err := parser.ParseVideoShareUrl(videoShareUrl)
paramUrl := c.Query("url")
parseRes, err := parser.ParseVideoShareUrlByRegexp(paramUrl)
jsonRes := HttpResponse{
Code: 200,
Msg: "解析成功",
Expand Down
5 changes: 5 additions & 0 deletions parser/haokan.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (h haoKan) parseVideoID(videoId string) (*VideoParseInfo, error) {
return nil, err
}

// 接口返回错误
if gjson.GetBytes(res.Body(), "errno").Int() != 0 {
return nil, errors.New(gjson.GetBytes(res.Body(), "error").String())
}

data := gjson.GetBytes(res.Body(), "data.apiData.curVideoMeta")
title := data.Get("title").String()
videoUrl := data.Get("playurl").String()
Expand Down
14 changes: 13 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ import (
"fmt"
"strings"
"sync"

"github.com/wujunwei928/parse-video/utils"
)

// ParseVideoShareUrl 根据视频分享链接解析视频信息
// ParseVideoShareUrlByRegexp 将分享链接信息, 进行正则表达式匹配到分享链接后, 再解析视频信息
func ParseVideoShareUrlByRegexp(shareMsg string) (*VideoParseInfo, error) {
videoShareUrl, err := utils.RegexpMatchUrlFromString(shareMsg)
if err != nil {
return nil, err
}

return ParseVideoShareUrl(videoShareUrl)
}

// ParseVideoShareUrl 根据视频分享链接解析视频信息: 分享链接需是正常http链接
func ParseVideoShareUrl(shareUrl string) (*VideoParseInfo, error) {
// 根据分享url判断source
source := ""
Expand Down
6 changes: 6 additions & 0 deletions parser/pipigaoxiao.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func (p piPiGaoXiao) parseVideoID(videoId string) (*VideoParseInfo, error) {
return nil, err
}

// 接口返回错误
apiErr := gjson.GetBytes(res.Body(), "msg")
if apiErr.Exists() {
return nil, errors.New(apiErr.String())
}

data := gjson.GetBytes(res.Body(), "data.post")
title := data.Get("content").String()
id := data.Get("imgs.0.id").String()
Expand Down
10 changes: 10 additions & 0 deletions parser/quanmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func (q quanMin) parseVideoID(videoId string) (*VideoParseInfo, error) {
return nil, err
}

// 接口返回错误
if gjson.GetBytes(res.Body(), "errno").Int() != 0 {
return nil, errors.New(gjson.GetBytes(res.Body(), "error").String())
}
// 视频状态错误
metaStatusText := gjson.GetBytes(res.Body(), "data.meta.statusText").String()
if len(metaStatusText) > 0 {
return nil, errors.New(metaStatusText)
}

data := gjson.GetBytes(res.Body(), "data")
author := data.Get("author.name").String()
avatar := data.Get("author.icon").String()
Expand Down
10 changes: 10 additions & 0 deletions parser/weishi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func (w weiShi) parseVideoID(videoId string) (*VideoParseInfo, error) {
return nil, err
}

// 接口返回错误
if gjson.GetBytes(res.Body(), "ret").Int() != 0 {
return nil, errors.New(gjson.GetBytes(res.Body(), "msg").String())
}
// 视频状态错误
errMsg := gjson.GetBytes(res.Body(), "data.errmsg").String()
if len(errMsg) > 0 {
return nil, errors.New(errMsg)
}

data := gjson.GetBytes(res.Body(), "data.feeds.0")
author := data.Get("poster.nick").String()
avatar := data.Get("poster.avatar").String()
Expand Down
20 changes: 20 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils

import (
"fmt"
"regexp"
)

func RegexpMatchUrlFromString(str string) (string, error) {
urlReg, err := regexp.Compile(`https?://[\w.-]+[\w/-]*[\w.-:]*\??[\w=&:\-+%.]*/*`)
if err != nil {
return "", fmt.Errorf("match url regexp compile error: %s", err.Error())
}

findStr := urlReg.FindString(str)
if len(findStr) <= 0 {
return "", fmt.Errorf("str not have url")
}

return findStr, nil
}

0 comments on commit f5140e8

Please sign in to comment.