feat: xhs支持小程序链接分享 & 修复下载图片的bug

This commit is contained in:
zhiyu1998 2023-03-21 14:48:36 +08:00
parent d26a45398e
commit 7d70790457

View File

@ -471,7 +471,7 @@ export class tools extends plugin {
for (let item of resp.includes.media) { for (let item of resp.includes.media) {
if (item.type === "photo") { if (item.type === "photo") {
// 图片 // 图片
task.push(this.downloadImg(item.url, downloadPath)); task.push(this.downloadImg(item.url, downloadPath, "", true));
} else if (item.type === "video") { } else if (item.type === "video") {
// 视频 // 视频
await this.downloadVideo(resp.includes.media[0].variants[0].url, true).then( await this.downloadVideo(resp.includes.media[0].variants[0].url, true).then(
@ -538,19 +538,20 @@ export class tools extends plugin {
// 小红书解析 // 小红书解析
async redbook(e) { async redbook(e) {
// 解析短号 // 解析短号
const msgUrl = /(http:|https:)\/\/(xhslink|xiaohongshu).com\/[A-Za-z\d._?%&+\-=\/#@]*/.exec( let msgUrl = /(http:|https:)\/\/(xhslink|xiaohongshu).com\/[A-Za-z\d._?%&+\-=\/#@]*/.exec(
e.msg, e.msg,
)[0]; )?.[0] || /(http:|https:)\/\/www\.xiaohongshu\.com\/discovery\/item\/(\w+)/.exec(e.message[0].data)?.[0];
console.log(msgUrl)
let id; let id;
if (msgUrl.includes("xhslink")) { if (msgUrl.includes("xhslink")) {
await fetch(msgUrl, { await fetch(msgUrl, {
redirect: "follow", redirect: "follow",
}).then(resp => { }).then(resp => {
const uri = decodeURIComponent(resp.url); const uri = decodeURIComponent(resp.url);
id = /explore\/(\w+)/.exec(uri)[1]; id = /explore\/(\w+)/.exec(uri)?.[1];
}); });
} else { } else {
id = /explore\/(\w+)/.exec(msgUrl)[1]; id = /explore\/(\w+)/.exec(msgUrl)?.[1] || /discovery\/item\/(\w+)/.exec(msgUrl)?.[1];
} }
const downloadPath = `${this.defaultPath}${this.e.group_id || this.e.user_id}`; const downloadPath = `${this.defaultPath}${this.e.group_id || this.e.user_id}`;
// 获取信息 // 获取信息
@ -749,29 +750,42 @@ export class tools extends plugin {
* @param img * @param img
* @param dir * @param dir
* @param fileName * @param fileName
* @param isProxy
* @returns {Promise<unknown>} * @returns {Promise<unknown>}
*/ */
async downloadImg(img, dir, fileName = "") { async downloadImg(img, dir, fileName = "", isProxy = false) {
if (fileName === "") { if (fileName === "") {
fileName = img.split("/").pop(); fileName = img.split("/").pop();
} }
const filepath = `${dir}/${fileName}`; const filepath = `${dir}/${fileName}`;
const writer = fs.createWriteStream(filepath); const writer = fs.createWriteStream(filepath);
return axios let req;
.get(img, { if (isProxy) {
headers: { req = axios
"User-Agent": .get(img, {
"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", headers: {
}, "User-Agent":
responseType: "stream", "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",
httpAgent: tunnel.httpOverHttp({ },
proxy: { host: this.proxyAddr, port: this.proxyPort }, responseType: "stream",
}), httpAgent: tunnel.httpOverHttp({
httpsAgent: tunnel.httpOverHttp({ proxy: { host: this.proxyAddr, port: this.proxyPort },
proxy: { host: this.proxyAddr, port: this.proxyPort }, }),
}), httpsAgent: tunnel.httpOverHttp({
}) proxy: { host: this.proxyAddr, port: this.proxyPort },
.then(res => { }),
})
} else {
req = axios
.get(img, {
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",
})
}
return req.then(res => {
res.data.pipe(writer); res.data.pipe(writer);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
writer.on("finish", () => { writer.on("finish", () => {