mirror of
https://github.com/Jerryplusy/rc-plugin.git
synced 2025-10-14 16:19:18 +00:00
🦄 refactor: 修改https框架为axios
This commit is contained in:
parent
e99d6d721c
commit
0266a79862
261
apps/tools.js
261
apps/tools.js
@ -1,151 +1,132 @@
|
|||||||
// 主库
|
// 主库
|
||||||
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() {
|
||||||
super({
|
super({
|
||||||
name: "工具和学习类",
|
name: "工具和学习类",
|
||||||
dsc: "工具相关指令",
|
dsc: "工具相关指令",
|
||||||
event: "message.group",
|
event: "message.group",
|
||||||
priority: 500,
|
priority: 500,
|
||||||
rule: [
|
rule: [
|
||||||
{
|
{
|
||||||
reg: "^#(翻译)(.*)$",
|
reg: "^#(翻译)(.*)$",
|
||||||
fnc: "trans",
|
fnc: "trans",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
reg: "(.*)(v.douyin.com)",
|
reg: "(.*)(v.douyin.com)",
|
||||||
fnc: "douyin",
|
fnc: "douyin",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
|
||||||
this.path = "./data/mp4/";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 翻译插件
|
|
||||||
async trans(e) {
|
|
||||||
let place = e.msg.replace(/#|翻译/g, "").trim();
|
|
||||||
let url = /[\u4E00-\u9FFF]+/g.test(place)
|
|
||||||
? `http://api.fanyi.baidu.com/api/trans/vip/translate?from=zh&to=en&appid=20210422000794040&salt=542716863&sign=${md5(
|
|
||||||
"20210422000794040" + place + "542716863" + "HooD_ndgwcGH6SAnxGrM"
|
|
||||||
)}&q=${place}`
|
|
||||||
: `http://api.fanyi.baidu.com/api/trans/vip/translate?from=en&to=zh&appid=20210422000794040&salt=542716863&sign=${md5(
|
|
||||||
"20210422000794040" + place + "542716863" + "HooD_ndgwcGH6SAnxGrM"
|
|
||||||
)}&q=${place}`;
|
|
||||||
await fetch(url)
|
|
||||||
.then((resp) => resp.json())
|
|
||||||
.then((text) => text.trans_result)
|
|
||||||
.then((res) => this.reply(`${res[0].dst}`, true))
|
|
||||||
.catch((err) => logger.error(err));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//zengjie2001
|
|
||||||
async douyinRequest() {
|
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
});
|
this.path = "./data/rcmp4/";
|
||||||
}
|
}
|
||||||
async douyin1(e) {
|
|
||||||
this.douyinRequest().then((res) => {
|
// 翻译插件
|
||||||
const douRex = /.*video\/(\d+)\/(.*?)/g;
|
async trans(e) {
|
||||||
const douId = douRex.exec(res)[1];
|
let place = e.msg.replace(/#|翻译/g, "").trim();
|
||||||
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`;
|
let url = /[\u4E00-\u9FFF]+/g.test(place)
|
||||||
e.reply("解析中...");
|
? `http://api.fanyi.baidu.com/api/trans/vip/translate?from=zh&to=en&appid=20210422000794040&salt=542716863&sign=${md5(
|
||||||
return fetch(url)
|
"20210422000794040" + place + "542716863" + "HooD_ndgwcGH6SAnxGrM"
|
||||||
.then((resp) => resp.json())
|
)}&q=${place}`
|
||||||
.then((json) => json.item_list[0])
|
: `http://api.fanyi.baidu.com/api/trans/vip/translate?from=en&to=zh&appid=20210422000794040&salt=542716863&sign=${md5(
|
||||||
.then((item) => item.video.play_addr.url_list[0])
|
"20210422000794040" + place + "542716863" + "HooD_ndgwcGH6SAnxGrM"
|
||||||
.then(async (url) => {
|
)}&q=${place}`;
|
||||||
await e.reply(await segment.video(await this.downloadVideo(url)));
|
await fetch(url)
|
||||||
});
|
.then((resp) => resp.json())
|
||||||
});
|
.then((text) => text.trans_result)
|
||||||
return true;
|
.then((res) => this.reply(`${res[0].dst}`, true))
|
||||||
}
|
.catch((err) => logger.error(err));
|
||||||
// 抖音解析
|
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];
|
// 抖音解析
|
||||||
|
async douyin(e) {
|
||||||
await https
|
const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g;
|
||||||
.request(
|
const douUrl = urlRex.exec(e.msg.trim())[0];
|
||||||
douUrl,
|
|
||||||
{
|
await this.douyinRequest(douUrl).then((res) => {
|
||||||
method: "HEAD",
|
const douRex = /.*video\/(\d+)\/(.*?)/g;
|
||||||
headers: {
|
const douId = douRex.exec(res)[1];
|
||||||
"User-Agent":
|
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`;
|
||||||
"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",
|
e.reply("解析中...");
|
||||||
},
|
return fetch(url)
|
||||||
timeout: 10000,
|
.then((resp) => resp.json())
|
||||||
},
|
.then((json) => json.item_list[0])
|
||||||
(res) => {
|
.then((item) => item.video.play_addr.url_list[0])
|
||||||
const location = res.headers["location"];
|
.then(async (url) => {
|
||||||
const douRex = /.*video\/(\d+)\/(.*?)/g;
|
await e.reply(await segment.video(await this.downloadVideo(url)));
|
||||||
const douId = douRex.exec(location)[1];
|
});
|
||||||
const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${douId}`;
|
});
|
||||||
e.reply("解析中...");
|
return true;
|
||||||
return fetch(url)
|
}
|
||||||
.then((resp) => resp.json())
|
|
||||||
.then((json) => json.item_list[0])
|
// 请求参数
|
||||||
.then((item) => item.video.play_addr.url_list[0])
|
async douyinRequest(url) {
|
||||||
.then(async (url) => {
|
const params = {
|
||||||
await e.reply(await segment.video(await this.downloadVideo(url)));
|
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",
|
||||||
)
|
},
|
||||||
.on("error", (err) => {
|
timeout: 10000,
|
||||||
console.error(err);
|
};
|
||||||
})
|
return new Promise((resolve, reject) => {
|
||||||
.end();
|
axios
|
||||||
return true;
|
.head(url, params)
|
||||||
}
|
.then((resp) => {
|
||||||
|
const location = resp.request.res.responseUrl
|
||||||
// 根URL据下载视频 / 音频
|
resolve(location);
|
||||||
async downloadVideo(url) {
|
})
|
||||||
const target = `${this.path}${this.e.group_id || this.e.user_id}/temp.mp4`;
|
.catch((err) => {
|
||||||
if (fs.existsSync(target)) {
|
reject(err);
|
||||||
console.log(`视频已存在`);
|
});
|
||||||
fs.unlinkSync(target);
|
});
|
||||||
} else {
|
}
|
||||||
fs.mkdirSync(`${this.path}${this.e.group_id || this.e.user_id}`);
|
|
||||||
|
// 根URL据下载视频 / 音频
|
||||||
|
async downloadVideo(url) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
const res = await axios.get(url, {
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
responseType: "stream",
|
||||||
|
});
|
||||||
|
console.log(`开始下载: ${url}`);
|
||||||
|
const writer = fs.createWriteStream(target);
|
||||||
|
res.data.pipe(writer);
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
writer.on("finish", resolve);
|
||||||
|
writer.on("error", reject);
|
||||||
|
});
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 同步递归创建文件夹
|
||||||
|
mkdirsSync(dirname) {
|
||||||
|
if (fs.existsSync(dirname)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
if (this.mkdirsSync(path.dirname(dirname))) {
|
||||||
|
fs.mkdirSync(dirname);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const res = await axios.get(url, {
|
|
||||||
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",
|
|
||||||
},
|
|
||||||
responseType: "stream",
|
|
||||||
});
|
|
||||||
console.log(`开始下载: ${url}`);
|
|
||||||
const writer = fs.createWriteStream(target);
|
|
||||||
res.data.pipe(writer);
|
|
||||||
new Promise((resolve, reject) => {
|
|
||||||
writer.on("finish", resolve);
|
|
||||||
writer.on("error", reject);
|
|
||||||
});
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user