-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f127bfb
commit 8d8c488
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ytdlp | ||
Check warning on line 1 in plugin/ytdlp/ytdlp.go GitHub Actions / lint
|
||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"time" | ||
|
||
ctrl "github.com/FloatTech/zbpctrl" | ||
"github.com/FloatTech/zbputils/control" | ||
"github.com/FloatTech/zbputils/ctxext" | ||
zero "github.com/wdvxdr1123/ZeroBot" | ||
) | ||
|
||
func init() { | ||
en := control.Register("ytdlp", &ctrl.Options[*zero.Ctx]{ | ||
DisableOnDefault: false, | ||
Brief: "ytdlp 下载器", | ||
Help: "/yt-dlp [youtube-video-link]", | ||
PrivateDataFolder: "ytdlp", | ||
}).ApplySingle(ctxext.DefaultSingle) | ||
// 初始化临时文件夹 | ||
tempFileDir := path.Join(en.DataFolder(), "temp") | ||
err := os.MkdirAll(tempFileDir, 0750) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// 注册指令 | ||
en.OnPrefix("/yt-dlp", zero.OnlyGroup, zero.SuperUserPermission). | ||
SetBlock(true). | ||
Handle(func(ctx *zero.Ctx) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
ctx.Send("系统错误,无法获取当前程序目录。") | ||
} | ||
_, err = exec.LookPath("yt-dlp") | ||
if err != nil { | ||
ctx.Send("目标服务器未安装 yt-dlp。") | ||
return | ||
} | ||
url := ctx.State["args"].(string) | ||
cmdVideoFileSize := exec.Command("yt-dlp", "--print", "%(filesize,filesize_approx)s", url) | ||
videoFileSizeInByte, err := cmdVideoFileSize.Output() | ||
if err != nil { | ||
ctx.Send("获取视频文件大小失败,可能是服务器无法访问 YouTube。") | ||
return | ||
} | ||
videoFileSize := string(videoFileSizeInByte) | ||
cmdVideoTitle := exec.Command("yt-dlp", "--print", "%(title)s", url) | ||
videoTitleInByte, err := cmdVideoTitle.Output() | ||
if err != nil { | ||
ctx.Send("获取视频标题失败,可能是服务器无法访问 YouTube。") | ||
return | ||
} | ||
videoTitle := string(videoTitleInByte) | ||
ctx.Send(fmt.Sprintf("视频标题:%s视频大小:%s即将开始下载视频,请稍候。", videoTitle, videoFileSize)) | ||
fileName := fmt.Sprintf("%d_%d.mp4", ctx.Event.Sender.ID, time.Now().Unix()) | ||
videoFilePath := path.Join(tempFileDir, fileName) | ||
cmdDownload := exec.Command("yt-dlp", url, "-o", videoFilePath) | ||
downloadLogInByte, err := cmdDownload.Output() | ||
downloadLog := string(downloadLogInByte) | ||
if err != nil { | ||
ctx.Send(fmt.Sprintf("视频文件下载失败,可能存在网络波动。\n%s", downloadLog)) | ||
return | ||
} | ||
ctx.Send("文件下载成功,正在上传,请稍候。") | ||
fullVideoFilePath := path.Join(wd, videoFilePath) | ||
ctx.UploadThisGroupFile(fullVideoFilePath, fileName, "") | ||
os.Remove(videoFilePath) | ||
}) | ||
} |