- 添加了 gjson 库用于解析 JSON 数据 - 修改循环语法从传统的 for i := 0; i < n; i++ 到 range 语法 - 实现了 CheckHasVideo 函数来检查用户是否有视频作品 - 根据用户是否有视频动态选择 HasVideoToken 或 NoVideoToken - 添加了新的 HTTP 请求头配置用于 API 调用 - 更新了依赖包并添加了相关间接依赖
This commit is contained in:
+54
-3
@@ -7,8 +7,11 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var httpClient = &http.Client{
|
||||
@@ -31,9 +34,9 @@ func init() {
|
||||
func InitConn() {
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
wg.Go(func() {
|
||||
for i := 0; i < 50; i++ {
|
||||
for range 50 {
|
||||
resp, err := httpClient.Get(config.APPConfig.Url + "/api/test")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -56,8 +59,19 @@ func UploadDataToServer(ctx context.Context, data string) error {
|
||||
<-limit
|
||||
}()
|
||||
|
||||
//根据账号是否有视频判断要存储的token
|
||||
params := url.Values{}
|
||||
params.Set("token", config.APPConfig.Token)
|
||||
secUserId := strings.Split(data, "----")[1]
|
||||
hasVideo, err := CheckHasVideo(secUserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if hasVideo {
|
||||
params.Set("token", config.APPConfig.HasVideoToken)
|
||||
} else {
|
||||
params.Set("token", config.APPConfig.NoVideoToken)
|
||||
}
|
||||
|
||||
params.Set("data", data)
|
||||
|
||||
//http://127.0.0.1:8080/api/data?token=123456&data=123456
|
||||
@@ -82,3 +96,40 @@ func UploadDataToServer(ctx context.Context, data string) error {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func CheckHasVideo(secUserId string) (bool, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(
|
||||
"GET",
|
||||
"https://imdesktop.douyin.com/aweme/v1/web/user/profile/other/?sec_user_id="+secUserId,
|
||||
nil,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false, err
|
||||
}
|
||||
req.Header.Add("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
|
||||
req.Header.Add("Accept", "*/*")
|
||||
req.Header.Add("Host", "imdesktop.douyin.com")
|
||||
req.Header.Add("Connection", "keep-alive")
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
if gjson.Get(string(body), "user.aweme_count").Int() > 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user