178 lines
4.1 KiB
Go
178 lines
4.1 KiB
Go
package controller
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"go-bot/bot/global"
|
||
"go-bot/bot/structs"
|
||
"io/ioutil"
|
||
"net/http"
|
||
"os"
|
||
"strings"
|
||
|
||
"github.com/eatmoreapple/openwechat"
|
||
)
|
||
|
||
// DouYinResolution 抖音解析
|
||
// Author: NothAmor
|
||
func DouYinResolution(msg *openwechat.Message) {
|
||
split := strings.Split(msg.Content, " ")
|
||
|
||
if len(split) == 1 {
|
||
msg.ReplyText("请输入正确的抖音链接")
|
||
return
|
||
}
|
||
|
||
// 获取抖音链接
|
||
douyinUrl := split[1]
|
||
|
||
// 获取抖音视频信息
|
||
data, err := GetDouYinUrlInfo(douyinUrl)
|
||
if err != nil {
|
||
msg.ReplyText(fmt.Sprintf("获取抖音信息失败, 失败原因: %s", err.Error()))
|
||
return
|
||
}
|
||
|
||
// 图片解析
|
||
if data.Type == "image" {
|
||
msg.ReplyText(fmt.Sprintf("解析类型: %s\n解析时长: %f\n图片ID: %s\n图片描述: %s\n作者昵称: %s\n作者ID: %s\n", data.Type, data.TotalTime, data.AwemeID, data.Desc, data.Author.Nickname, data.Author.UID))
|
||
|
||
for _, imageUrl := range data.ImageData.NoWatermarkImageList {
|
||
image, err := DownloadPic(imageUrl)
|
||
if err != nil {
|
||
msg.ReplyText(fmt.Sprintf("下载图片失败, 失败原因: %s", err.Error()))
|
||
return
|
||
}
|
||
|
||
msg.ReplyImage(image)
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
// 视频解析
|
||
if data.Type == "video" {
|
||
msg.ReplyText(fmt.Sprintf("解析类型: %s\n解析时长: %f\n视频ID: %s\n视频描述: %s\n作者昵称: %s\n作者ID: %s\n", data.Type, data.TotalTime, data.AwemeID, data.Desc, data.Author.Nickname, data.Author.UID))
|
||
|
||
video, err := DownloadVideo(data.VideoData.NwmVideoURLHQ)
|
||
if err != nil {
|
||
msg.ReplyText(fmt.Sprintf("下载视频失败, 失败原因: %s", err.Error()))
|
||
return
|
||
}
|
||
|
||
msg.ReplyVideo(video)
|
||
return
|
||
}
|
||
}
|
||
|
||
// GetDouYinUrlInfo 获取抖音链接信息
|
||
// Author: NothAmor
|
||
func GetDouYinUrlInfo(url string) (resolutionResponse structs.ResolutionResponse, err error) {
|
||
// 拼接GET链接,请求API服务
|
||
resolutionApiUrl := fmt.Sprintf("%s://%s:%s/%s?url=%s", global.Config.DouYin.Protocol, global.Config.DouYin.Address, global.Config.DouYin.Port, global.Config.DouYin.Router.Api, url)
|
||
|
||
client := &http.Client{}
|
||
req, err := http.NewRequest("GET", resolutionApiUrl, nil)
|
||
if err != nil {
|
||
err = fmt.Errorf("请求链接错误: %s", err)
|
||
return
|
||
}
|
||
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
err = fmt.Errorf("请求链接错误: %s", err)
|
||
return
|
||
}
|
||
|
||
defer resp.Body.Close()
|
||
|
||
err = json.NewDecoder(resp.Body).Decode(&resolutionResponse)
|
||
if err != nil {
|
||
err = fmt.Errorf("解析返回数据错误: %s", err)
|
||
return
|
||
}
|
||
|
||
if resolutionResponse.Status != "success" {
|
||
err = fmt.Errorf("解析返回数据错误: %s", resolutionResponse.Message)
|
||
return
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
// DownloadPic 下载图片
|
||
// Author: NothAmor
|
||
func DownloadPic(url string) (image *os.File, err error) {
|
||
resp, err := http.Get(url)
|
||
if err != nil {
|
||
err = fmt.Errorf("请求链接错误: %s", err)
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
imageBytes, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
err = fmt.Errorf("读取返回数据错误: %s", err)
|
||
return
|
||
}
|
||
|
||
tmpFile, err := ioutil.TempFile("", "image*.jpg")
|
||
if err != nil {
|
||
err = fmt.Errorf("创建临时文件错误: %s", err)
|
||
return
|
||
}
|
||
defer tmpFile.Close()
|
||
|
||
_, err = tmpFile.Write(imageBytes)
|
||
if err != nil {
|
||
err = fmt.Errorf("写入临时文件错误: %s", err)
|
||
return
|
||
}
|
||
|
||
_, err = tmpFile.Seek(0, 0)
|
||
if err != nil {
|
||
err = fmt.Errorf("设置临时文件指针错误: %s", err)
|
||
return
|
||
}
|
||
|
||
return tmpFile, nil
|
||
}
|
||
|
||
// DownloadVideo 下载视频
|
||
// Author: NothAmor
|
||
func DownloadVideo(url string) (video *os.File, err error) {
|
||
resp, err := http.Get(url)
|
||
if err != nil {
|
||
err = fmt.Errorf("请求链接错误: %s", err)
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
videoBytes, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
err = fmt.Errorf("读取返回数据错误: %s", err)
|
||
return
|
||
}
|
||
|
||
tmpFile, err := ioutil.TempFile("", "video*.mp4")
|
||
if err != nil {
|
||
err = fmt.Errorf("创建临时文件错误: %s", err)
|
||
return
|
||
}
|
||
defer tmpFile.Close()
|
||
|
||
_, err = tmpFile.Write(videoBytes)
|
||
if err != nil {
|
||
err = fmt.Errorf("写入临时文件错误: %s", err)
|
||
return
|
||
}
|
||
|
||
_, err = tmpFile.Seek(0, 0)
|
||
if err != nil {
|
||
err = fmt.Errorf("设置临时文件指针错误: %s", err)
|
||
return
|
||
}
|
||
|
||
return tmpFile, nil
|
||
}
|