138 lines
3.7 KiB
Go
138 lines
3.7 KiB
Go
package controller
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"go-bot/bot/global"
|
||
"go-bot/bot/structs"
|
||
"io/ioutil"
|
||
"net/http"
|
||
"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 := msg.Content[3:]
|
||
|
||
// 获取抖音视频信息
|
||
data, err := GetDouYinUrlInfo(douYinUrl)
|
||
if err != nil {
|
||
fmt.Printf("获取抖音信息失败, 失败原因: %s\n", err.Error())
|
||
msg.ReplyText(fmt.Sprintf("获取抖音信息失败, 失败原因: %s", err.Error()))
|
||
return
|
||
}
|
||
|
||
// 图片解析
|
||
if data.Type == "image" {
|
||
msg.ReplyText(fmt.Sprintf("解析类型: 图集\n解析时长: %f\n图片ID: %s\n图片描述: %s\n作者昵称: %s\n作者ID: %s\n", data.TotalTime, data.AwemeID, data.Desc, data.Author.Nickname, data.Author.UID))
|
||
|
||
text := ""
|
||
for _, imageUrl := range data.ImageData.NoWatermarkImageList {
|
||
text = text + "1. " + imageUrl + "\n"
|
||
// data, err := DownloadFile(imageUrl)
|
||
// if err != nil {
|
||
// fmt.Printf("下载图片失败失败, 失败原因: %s\n", err.Error())
|
||
// msg.ReplyText(fmt.Sprintf("下载图片失败, 失败原因: %s", err.Error()))
|
||
// return
|
||
// }
|
||
|
||
// _, err = msg.ReplyImage(bytes.NewReader(data))
|
||
// if err != nil {
|
||
// fmt.Printf("发送图片失败, 失败原因: %s\n", err.Error())
|
||
// msg.ReplyText(fmt.Sprintf("发送图片失败, 失败原因: %s", err.Error()))
|
||
// return
|
||
// }
|
||
}
|
||
|
||
msg.ReplyText(text)
|
||
|
||
return
|
||
}
|
||
|
||
// 视频解析
|
||
if data.Type == "video" {
|
||
msg.ReplyText(fmt.Sprintf("解析类型: 视频\n解析时长: %f\n视频ID: %s\n视频描述: %s\n作者昵称: %s\n作者ID: %s\n", data.TotalTime, data.AwemeID, data.Desc, data.Author.Nickname, data.Author.UID))
|
||
|
||
data, err := DownloadFile(data.VideoData.NwmVideoURLHQ)
|
||
if err != nil {
|
||
fmt.Printf("下载视频失败, 失败原因: %s\n", err.Error())
|
||
msg.ReplyText(fmt.Sprintf("下载视频失败, 失败原因: %s", err.Error()))
|
||
return
|
||
}
|
||
|
||
_, err = msg.ReplyVideo(bytes.NewReader(data))
|
||
if err != nil {
|
||
fmt.Printf("发送视频失败, 失败原因: %s\n", err.Error())
|
||
msg.ReplyText(fmt.Sprintf("发送视频失败, 失败原因: %s", err.Error()))
|
||
return
|
||
}
|
||
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)
|
||
|
||
resp, err := http.Get(resolutionApiUrl)
|
||
if err != nil {
|
||
err = fmt.Errorf("请求链接错误: %s", err)
|
||
return
|
||
}
|
||
|
||
defer resp.Body.Close()
|
||
|
||
// 读取返回数据
|
||
resolutionBytes, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
err = fmt.Errorf("读取返回数据错误: %s", err)
|
||
return
|
||
}
|
||
|
||
// 解析返回数据
|
||
err = json.Unmarshal(resolutionBytes, &resolutionResponse)
|
||
if err != nil {
|
||
err = fmt.Errorf("解析返回数据错误: %s", err)
|
||
return
|
||
}
|
||
|
||
if resolutionResponse.Status != "success" {
|
||
err = fmt.Errorf("解析返回数据错误: %s", resolutionResponse.Message)
|
||
return
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
// DownloadVideo 下载文件
|
||
// Author: NothAmor
|
||
func DownloadFile(url string) (data []byte, err error) {
|
||
resp, err := http.Get(url)
|
||
if err != nil {
|
||
err = fmt.Errorf("请求链接错误: %s", err)
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
data, err = ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
err = fmt.Errorf("读取返回数据错误: %s", err)
|
||
return
|
||
}
|
||
|
||
return
|
||
}
|