🐞 fix: 热修复tiktok问题

This commit is contained in:
zhiyu1998 2022-11-24 19:00:59 +08:00
parent 8e03019b44
commit 9c7d7869f8

View File

@ -29,7 +29,7 @@ export class tools extends plugin {
}, },
], ],
}); });
this.path = "./data/rcmp4/"; this.defaultPath = `./data/rcmp4/`
} }
// 翻译插件 // 翻译插件
@ -64,8 +64,10 @@ export class tools extends plugin {
.then((resp) => resp.json()) .then((resp) => resp.json())
.then((json) => json.item_list[0]) .then((json) => json.item_list[0])
.then((item) => item.video.play_addr.url_list[0]) .then((item) => item.video.play_addr.url_list[0])
.then(async (url) => { .then((url) => {
await e.reply(await segment.video(await this.downloadVideo(url))); this.downloadVideo(url).then(video => {
e.reply(segment.video(`${this.defaultPath}${this.e.group_id || this.e.user_id}/temp.mp4`));
})
}); });
}); });
return true; return true;
@ -80,8 +82,10 @@ export class tools extends plugin {
e.reply("识别tiktok, 解析中..."); e.reply("识别tiktok, 解析中...");
fetch(tiktokApi) fetch(tiktokApi)
.then(resp => resp.json()) .then(resp => resp.json())
.then(async json => { .then(json => {
await e.reply(await segment.video(await this.downloadVideo(json.wm_video_url.replace("https","http")))) this.downloadVideo(json.wm_video_url.replace("https","http")).then(video => {
e.reply(segment.video(`${this.defaultPath}${this.e.group_id || this.e.user_id}/temp.mp4`))
})
}) })
return true return true
} }
@ -110,11 +114,10 @@ export class tools extends plugin {
// 根URL据下载视频 / 音频 // 根URL据下载视频 / 音频
async downloadVideo(url) { async downloadVideo(url) {
let target = `${this.path}${this.e.group_id || this.e.user_id}`; if (!fs.existsSync(this.defaultPath)) {
if (!fs.existsSync(target)) { this.mkdirsSync(this.defaultPath);
this.mkdirsSync(`${this.path}${this.e.group_id || this.e.user_id}`);
} }
target += '/temp.mp4' const target = this.defaultPath + `${this.e.group_id || this.e.user_id}/temp.mp4`
// 待优化 // 待优化
if (fs.existsSync(target)) { if (fs.existsSync(target)) {
console.log(`视频已存在`); console.log(`视频已存在`);
@ -130,11 +133,11 @@ export class tools extends plugin {
console.log(`开始下载: ${url}`); console.log(`开始下载: ${url}`);
const writer = fs.createWriteStream(target); const writer = fs.createWriteStream(target);
res.data.pipe(writer); res.data.pipe(writer);
new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
writer.on("finish", resolve); writer.on("finish", resolve);
writer.on("error", reject); writer.on("error", reject);
}); });
return target;
} }
// 同步递归创建文件夹 // 同步递归创建文件夹