2025-02-20 17:32:53 +08:00

152 lines
4.9 KiB
Go

package tiktok
import (
"encoding/json"
"fmt"
"hk/pkg/util/httpUtil"
"strings"
)
type tiktokResp struct {
Code int64 `json:"code"`
Data tiktokData `json:"data"`
}
type tiktokData struct {
ID string `json:"id"`
AwesomeID string `json:"aweme_id"`
Region string `json:"region"`
Title string `json:"title"`
Cover string `json:"cover"`
AiDynamicCover string `json:"ai_dynamic_cover"` // 动态封面
OriginCover string `json:"origin_cover"` // 静态封面
Duration int `json:"duration"`
Play string `json:"play"` // 无水印视频
Wmplay string `json:"wmplay"` // 有水印视频
Size int `json:"size"`
WmSize int `json:"wm_size"`
Music string `json:"music"`
MusicInfo musicInfo `json:"music_info"`
PlayCount int `json:"play_count"`
DiggCount int `json:"digg_count"`
CommentCount int `json:"comment_count"`
ShareCount int `json:"share_count"`
DownloadCount int `json:"download_count"`
CollectCount int `json:"collect_count"`
CreateTime int `json:"create_time"`
AnchorsExtras string `json:"anchors_extras"`
IsAd bool `json:"is_ad"`
CommercialVideoInfo string `json:"commercial_video_info"`
ItemCommentSettings int `json:"item_comment_settings"`
Author authorDTO `json:"author"`
}
type musicInfo struct {
ID string `json:"id"`
Title string `json:"title"`
Play string `json:"play"` // 背景音乐
Cover string `json:"cover"`
Author string `json:"author"`
Original bool `json:"original"`
Duration int `json:"duration"`
Album string `json:"album"`
}
type authorDTO struct {
ID string `json:"id"`
UniqueID string `json:"unique_id"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
}
type VideoInfoResponse struct {
AuthorID string `json:"authorId"` // 作者ID
AuthorUniqueID string `json:"authorUniqueId"` // 作者账号
AuthorAvatar string `json:"authorAvatar"` // 作者头像
AuthorNickname string `json:"authorNickname"` // 作者昵称
VideoID string `json:"videoId"` // 视频ID
VideoTitle string `json:"videoTitle"` // 作品描述
OriginCover string `json:"originCover"` // 静态封面
DynamicCover string `json:"dynamicCover"` // 动态封面
DestinationURL string `json:"destinationUrl"` // 无水印下载地址
WatermarkVideoURL string `json:"watermarkVideoUrl"` // 有水印下载地址
MusicURL string `json:"musicUrl"` // 背景音乐链接
OriginalURL string `json:"originalUrl"` // 原始下载链接
DownloaderURL string `json:"downloaderUrl"` // 解析后下载地址
CreateTime string `json:"createTime"` // 创建时间
Result bool `json:"result"` // 结果
}
func GetTiktokVideoInfo(videoUrl string) (*VideoInfoResponse, error) {
if len(videoUrl) <= 0 {
return nil, nil
}
// 解析视频地址
// 定义需要匹配的前缀
prefixes := []string{
"https://www.tiktok.com/t/",
"https://m.tiktok.com/v/",
"https://vm.tiktok.com/",
"https://vt.tiktok.com/",
"https://dltik.com/",
}
// 检查是否有匹配的前缀
for _, prefix := range prefixes {
if strings.HasPrefix(videoUrl, prefix) {
return nil, nil
}
}
// 获取视频信息
var url = "https://tiktok-download-without-watermark.p.rapidapi.com/analysis?url=" + videoUrl
headers := map[string]string{
"x-rapidapi-host": "tiktok-download-without-watermark.p.rapidapi.com",
"x-rapidapi-key": "4a5bbe21bfmshbd98e7404b74063p19ba22jsne87a37e3b675",
}
body, err := httpUtil.NewRequest().SendGET(url, headers, nil)
if err != nil || len(body) <= 0 {
return nil, err
}
var tiktokResp tiktokResp
err = json.Unmarshal([]byte(body), &tiktokResp)
if err != nil {
fmt.Println("解析响应体时发生错误:", err)
return nil, err
}
// 比对 statusCode 是否等于0
if tiktokResp.Code != 0 {
return nil, nil
}
var videoInfo = VideoInfoResponse{}
// 作者信息
videoInfo.AuthorID = tiktokResp.Data.Author.ID
videoInfo.AuthorAvatar = tiktokResp.Data.Author.Avatar
videoInfo.AuthorUniqueID = tiktokResp.Data.Author.UniqueID
videoInfo.AuthorNickname = tiktokResp.Data.Author.Nickname
videoInfo.VideoID = tiktokResp.Data.ID
videoInfo.VideoTitle = tiktokResp.Data.Title
videoInfo.OriginCover = tiktokResp.Data.OriginCover
videoInfo.DynamicCover = tiktokResp.Data.AiDynamicCover
videoInfo.DestinationURL = tiktokResp.Data.Play
videoInfo.WatermarkVideoURL = tiktokResp.Data.Wmplay
videoInfo.MusicURL = tiktokResp.Data.MusicInfo.Play
videoInfo.OriginalURL = videoUrl
videoInfo.DownloaderURL = videoUrl
//videoInfo.CreateTime = tiktokResp.Data.CreateTime
videoInfo.Result = true
return &videoInfo, nil
}