🦄 refactor: 修改https框架为axios

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

View File

@ -1,11 +1,11 @@
// 主库 // 主库
import fetch from "node-fetch"; import fetch from "node-fetch";
import fs from "node:fs"; import fs from "node:fs";
import { segment } from "oicq"; import {segment} from "oicq";
// 其他库 // 其他库
import md5 from "md5"; import md5 from "md5";
import https from "https";
import axios from "axios"; import axios from "axios";
import path from 'path'
export class tools extends plugin { export class tools extends plugin {
constructor() { 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)); .catch((err) => logger.error(err));
return true; return true;
} }
//zengjie2001
async douyinRequest() { // 抖音解析
async douyin(e) {
const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g; const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g;
const douUrl = urlRex.exec(e.msg.trim())[0]; const douUrl = urlRex.exec(e.msg.trim())[0];
const params = {
method: "HEAD", await this.douyinRequest(douUrl).then((res) => {
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) => {
const douRex = /.*video\/(\d+)\/(.*?)/g; const douRex = /.*video\/(\d+)\/(.*?)/g;
const douId = douRex.exec(res)[1]; const douId = douRex.exec(res)[1];
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`; 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; 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( async douyinRequest(url) {
douUrl, const params = {
{
method: "HEAD",
headers: { headers: {
"User-Agent": "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", "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, timeout: 10000,
}, };
(res) => { return new Promise((resolve, reject) => {
const location = res.headers["location"]; axios
const douRex = /.*video\/(\d+)\/(.*?)/g; .head(url, params)
const douId = douRex.exec(location)[1]; .then((resp) => {
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`; const location = resp.request.res.responseUrl
e.reply("解析中..."); resolve(location);
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);
}) })
.end(); .catch((err) => {
return true; reject(err);
});
});
} }
// 根URL据下载视频 / 音频 // 根URL据下载视频 / 音频
async downloadVideo(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)) { if (fs.existsSync(target)) {
console.log(`视频已存在`); console.log(`视频已存在`);
fs.unlinkSync(target); fs.unlinkSync(target);
} else {
fs.mkdirSync(`${this.path}${this.e.group_id || this.e.user_id}`);
} }
const res = await axios.get(url, { const res = await axios.get(url, {
headers: { headers: {
@ -148,4 +117,16 @@ export class tools extends plugin {
}); });
return target; return target;
} }
// 同步递归创建文件夹
mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (this.mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}
} }