🐞 fix: B站动态封装

This commit is contained in:
zhiyu1998 2023-02-28 11:56:39 +08:00
parent 73f1866806
commit 901435cc35
2 changed files with 111 additions and 103 deletions

View File

@ -254,20 +254,21 @@ export class tools extends plugin {
} }
const dynamicId = /[^/]+(?!.*\/)/.exec(url)[0]; const dynamicId = /[^/]+(?!.*\/)/.exec(url)[0];
// console.log(dynamicId) // console.log(dynamicId)
getDynamic(dynamicId).then(resp => { getDynamic(dynamicId).then(async resp => {
if (resp.dynamicSrc.length > 0) { if (resp.dynamicSrc.length > 0) {
e.reply(`识别:哔哩哔哩动态, ${ resp.dynamicDesc }`); e.reply(`识别:哔哩哔哩动态, ${ resp.dynamicDesc }`);
// let dynamicSrcMsg = [] let dynamicSrcMsg = []
// resp.dynamicSrc.forEach(item => {
// dynamicSrcMsg.push({
// message: segment.image(item),
// nickname: e.sender.card || e.user_id,
// user_id: e.user_id,
// })
// })
resp.dynamicSrc.forEach(item => { resp.dynamicSrc.forEach(item => {
e.reply(segment.image(item)); dynamicSrcMsg.push({
}); message: segment.image(item),
nickname: e.sender.card || e.user_id,
user_id: e.user_id,
})
})
await this.reply(await Bot.makeForwardMsg(dynamicSrcMsg));
// resp.dynamicSrc.forEach(item => {
// e.reply(segment.image(item));
// });
} else { } else {
e.reply(`识别:哔哩哔哩动态, 但是失败!`); e.reply(`识别:哔哩哔哩动态, 但是失败!`);
} }
@ -425,85 +426,6 @@ export class tools extends plugin {
return true; return true;
} }
// 请求参数
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,
};
return new Promise((resolve, reject) => {
axios
.head(url, params)
.then(resp => {
const location = resp.request.res.responseUrl;
resolve(location);
})
.catch(err => {
reject(err);
});
});
}
// 工具根URL据下载视频 / 音频
async downloadVideo (url, isProxy = false) {
const groupPath = `${ this.defaultPath }${ this.e.group_id || this.e.user_id }`;
if (!fs.existsSync(groupPath)) {
mkdirsSync(groupPath);
}
const target = `${ groupPath }/temp.mp4`;
// 待优化
if (fs.existsSync(target)) {
console.log(`视频已存在`);
fs.unlinkSync(target);
}
let res;
if (!isProxy) {
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",
});
} else {
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",
httpAgent: tunnel.httpOverHttp({
proxy: { host: this.proxyAddr, port: this.proxyPort },
}),
httpsAgent: tunnel.httpOverHttp({
proxy: { host: this.proxyAddr, port: this.proxyPort },
}),
});
}
console.log(`开始下载: ${ url }`);
const writer = fs.createWriteStream(target);
res.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
// 工具找到tiktok的视频id
async getIdVideo (url) {
const matching = url.includes("/video/");
if (!matching) {
this.e.reply("没找到,正在获取随机视频!");
return null;
}
const idVideo = url.substring(url.indexOf("/video/") + 7, url.length);
return idVideo.length > 19 ? idVideo.substring(0, idVideo.indexOf("?")) : idVideo;
}
// acfun解析 // acfun解析
async acfun (e) { async acfun (e) {
const path = `${ this.defaultPath }${ this.e.group_id || this.e.user_id }/temp/`; const path = `${ this.defaultPath }${ this.e.group_id || this.e.user_id }/temp/`;
@ -675,22 +597,82 @@ export class tools extends plugin {
}); });
} }
// 工具下载pdf文件 // 请求参数
async downloadPDF (url, filename) { async douyinRequest (url) {
return axios({ const params = {
url: url,
responseType: "stream",
headers: { headers: {
"User-Agent": "User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 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",
}, },
}).then(response => { timeout: 10000,
const writer = fs.createWriteStream(filename); };
response.data.pipe(writer); return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => { axios
writer.on("finish", resolve); .head(url, params)
writer.on("error", reject); .then(resp => {
}); const location = resp.request.res.responseUrl;
resolve(location);
})
.catch(err => {
reject(err);
});
}); });
} }
// 工具根URL据下载视频 / 音频
async downloadVideo (url, isProxy = false) {
const groupPath = `${ this.defaultPath }${ this.e.group_id || this.e.user_id }`;
if (!fs.existsSync(groupPath)) {
mkdirsSync(groupPath);
}
const target = `${ groupPath }/temp.mp4`;
// 待优化
if (fs.existsSync(target)) {
console.log(`视频已存在`);
fs.unlinkSync(target);
}
let res;
if (!isProxy) {
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",
});
} else {
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",
httpAgent: tunnel.httpOverHttp({
proxy: { host: this.proxyAddr, port: this.proxyPort },
}),
httpsAgent: tunnel.httpOverHttp({
proxy: { host: this.proxyAddr, port: this.proxyPort },
}),
});
}
console.log(`开始下载: ${ url }`);
const writer = fs.createWriteStream(target);
res.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
// 工具找到tiktok的视频id
async getIdVideo (url) {
const matching = url.includes("/video/");
if (!matching) {
this.e.reply("没找到,正在获取随机视频!");
return null;
}
const idVideo = url.substring(url.indexOf("/video/") + 7, url.length);
return idVideo.length > 19 ? idVideo.substring(0, idVideo.indexOf("?")) : idVideo;
}
} }

View File

@ -1,5 +1,7 @@
import schedule from "node-schedule"; import schedule from "node-schedule";
import common from "../../../lib/common/common.js"; import common from "../../../lib/common/common.js";
import axios from "axios";
import fs from "node:fs";
/** /**
* 请求模板 * 请求模板
@ -68,4 +70,28 @@ function retry(func, maxRetries = 3, delay = 1000) {
}); });
} }
/**
* 工具下载pdf文件
* @param url
* @param filename
* @returns {Promise<unknown>}
*/
function downloadPDF (url, filename) {
return axios({
url: url,
responseType: "stream",
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
},
}).then(response => {
const writer = fs.createWriteStream(filename);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
});
}
export { jFeatch, autoTask, retry }; export { jFeatch, autoTask, retry };