🦄 refactor: 修改https框架为axios

This commit is contained in:
zhiyu1998 2022-11-23 19:34:40 +08:00
parent e99d6d721c
commit 0266a79862

View File

@ -4,8 +4,8 @@ import fs from "node:fs";
import {segment} from "oicq";
// 其他库
import md5 from "md5";
import https from "https";
import axios from "axios";
import path from 'path'
export class tools extends plugin {
constructor() {
@ -25,7 +25,7 @@ export class tools extends plugin {
},
],
});
this.path = "./data/mp4/";
this.path = "./data/rcmp4/";
}
// 翻译插件
@ -45,32 +45,13 @@ export class tools extends plugin {
.catch((err) => logger.error(err));
return true;
}
//zengjie2001
async douyinRequest() {
// 抖音解析
async douyin(e) {
const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g;
const douUrl = urlRex.exec(e.msg.trim())[0];
const params = {
method: "HEAD",
headers: {
"User-Agent":
"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36",
},
timeout: 10000,
};
return new Promise((resolve, reject) => {
axios
.request(douUrl, params)
.then((res) => {
const location = res.headers["location"];
resolve(location);
})
.catch((err) => {
reject(err);
});
});
}
async douyin1(e) {
this.douyinRequest().then((res) => {
await this.douyinRequest(douUrl).then((res) => {
const douRex = /.*video\/(\d+)\/(.*?)/g;
const douId = douRex.exec(res)[1];
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`;
@ -85,52 +66,40 @@ export class tools extends plugin {
});
return true;
}
// 抖音解析
async douyin(e) {
const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g;
const douUrl = urlRex.exec(e.msg.trim())[0];
await https
.request(
douUrl,
{
method: "HEAD",
// 请求参数
async douyinRequest(url) {
const params = {
headers: {
"User-Agent":
"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36",
},
timeout: 10000,
},
(res) => {
const location = res.headers["location"];
const douRex = /.*video\/(\d+)\/(.*?)/g;
const douId = douRex.exec(location)[1];
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`;
e.reply("解析中...");
return fetch(url)
.then((resp) => resp.json())
.then((json) => json.item_list[0])
.then((item) => item.video.play_addr.url_list[0])
.then(async (url) => {
await e.reply(await segment.video(await this.downloadVideo(url)));
});
}
)
.on("error", (err) => {
console.error(err);
};
return new Promise((resolve, reject) => {
axios
.head(url, params)
.then((resp) => {
const location = resp.request.res.responseUrl
resolve(location);
})
.end();
return true;
.catch((err) => {
reject(err);
});
});
}
// 根URL据下载视频 / 音频
async downloadVideo(url) {
const target = `${this.path}${this.e.group_id || this.e.user_id}/temp.mp4`;
let target = `${this.path}${this.e.group_id || this.e.user_id}`;
if (!fs.existsSync(target)) {
this.mkdirsSync(`${this.path}${this.e.group_id || this.e.user_id}`);
}
target += '/temp.mp4'
// 待优化
if (fs.existsSync(target)) {
console.log(`视频已存在`);
fs.unlinkSync(target);
} else {
fs.mkdirSync(`${this.path}${this.e.group_id || this.e.user_id}`);
}
const res = await axios.get(url, {
headers: {
@ -148,4 +117,16 @@ export class tools extends plugin {
});
return target;
}
// 同步递归创建文件夹
mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (this.mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}
}