feat: 新增ins解析 & 修复部分问题

This commit is contained in:
zhiyu1998 2023-03-23 01:54:24 +08:00
parent 284de715b7
commit 61775f99b7

View File

@ -6,7 +6,6 @@ import { segment } from "oicq";
import axios from "axios";
import _ from "lodash";
import tunnel from "tunnel";
import { TwitterApi } from "twitter-api-v2";
import HttpProxyAgent from "https-proxy-agent";
import { mkdirsSync } from "../utils/file.js";
import { downloadBFile, getDownloadUrl, mergeFileToMp4, getDynamic } from "../utils/bilibili.js";
@ -57,6 +56,10 @@ export class tools extends plugin {
reg: "(xhslink.com|xiaohongshu.com)",
fnc: "redbook",
},
{
reg: "(instagram.com)",
fnc: "instagram",
},
{
reg: "(doi.org)",
fnc: "literature",
@ -135,23 +138,47 @@ export class tools extends plugin {
// const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${ douId }`;
// const url = `https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id=${ douId }&aid=1128&version_name=23.5.0&device_platform=android&os_version=2333`;
// 感谢 Evil0ctalhttps://github.com/Evil0ctal提供的header 和 B1gM8chttps://github.com/B1gM8c的逆向算法X-Bogus
fetch("https://ttwid.bytedance.com/ttwid/union/register/", {
method: "POST",
mode: "cors",
credentials: "include",
body: JSON.stringify({
region: "cn",
aid: 1768,
needFid: false,
service: "www.ixigua.com",
migrate_info: {
ticket: "",
source: "node",
},
cbUrlProtocol: "https",
union: true,
}),
}).then(resp => {
const ttwid = resp.headers.get("set-cookie");
const odin_tt =
"324fb4ea4a89c0c05827e18a1ed9cf9bf8a17f7705fcc793fec935b637867e2a5a9b8168c885554d029919117a18ba69";
const passport_csrf_token = "f61602fc63757ae0e4fd9d6bdcee4810";
const headers = {
'accept-encoding': 'gzip, deflate, br',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'referer': 'https://www.douyin.com/',
'cookie': "s_v_web_id=verify_leytkxgn_kvO5kOmO_SdMs_4t1o_B5ml_BUqtWM1mP6BF;"
}
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
referer: "https://www.douyin.com/",
Cookie: `msToken=${generateRandomStr(107)}; ttwid=${ttwid};odin_tt=${odin_tt}; passport_csrf_token=${passport_csrf_token}`,
};
const dyApi = "https://www.douyin.com/aweme/v1/web/aweme/detail/?";
const params = `msToken=${generateRandomStr(107)}&device_platform=webapp&aid=6383&channel=channel_pc_web&aweme_id=${douId}&pc_client_type=1&version_code=190500&version_name=19.5.0&cookie_enabled=true&screen_width=1344&screen_height=756&browser_language=zh-CN&browser_platform=Win32&browser_name=Firefox&browser_version=110.0&browser_online=true&engine_name=Gecko&engine_version=109.0&os_name=Windows&os_version=10&cpu_core_num=16&device_memory=&platform=PC&webid=7158288523463362079`;
const params = `aweme_id=${douId}&aid=1128&version_name=23.5.0&device_platform=android&os_version=2333`;
// xg参数
const xbParam = getXB(params.replaceAll("&", "%26"));
// const param = resp.data.result[0].paramsencode;
const resDyApi = `${dyApi}${params}&X-Bogus=${xbParam}`;
axios
.get(resDyApi, {
headers,
})
.then(async resp => {
if (_.isEmpty(resp?.data)) {
e.reply("解析失败,请重试!");
return;
}
const item = resp.data.aweme_detail;
e.reply(`识别:抖音, ${item.desc}`);
const urlTypeCode = item.aweme_type;
@ -161,8 +188,7 @@ export class tools extends plugin {
this.downloadVideo(url_2, false, headers).then(_ => {
e.reply(
segment.video(
`${this.defaultPath}${
this.e.group_id || this.e.user_id
`${this.defaultPath}${this.e.group_id || this.e.user_id
}/temp.mp4`,
),
);
@ -190,6 +216,7 @@ export class tools extends plugin {
}
});
});
});
return true;
}
@ -547,6 +574,71 @@ export class tools extends plugin {
}
}
// ins解析
async instagram(e) {
let suffix = e.msg.match(/(?<=com\/)[\/a-z0-9A-Z].*/)[0];
if (suffix.startsWith("reel")) {
suffix = suffix.replace("reel/", "p/")
}
const API = `https://imginn.com/${suffix}`
logger.info(API)
let imgPromise = []
const downloadPath = `${this.defaultPath}${this.e.group_id || this.e.user_id}`;
// 简单封装图片下载
const downloadImg = (url, destination)=>{
return new Promise((resolve, reject) => {
fetch(url, {
timeout: 10000,
agent: new HttpProxyAgent(this.myProxy),
redirect: 'follow',
follow: 10,
})
.then(res => {
const dest = fs.createWriteStream(destination);
res.body.pipe(dest);
dest.on('finish', () => resolve(destination));
})
.catch(err => reject(err));
});
}
await fetch(API, {
timeout: 10000,
agent: new HttpProxyAgent(this.myProxy),
redirect: 'follow',
follow: 10,
})
.then(async resp => {
const html = await resp.text();
const images = html.match(/<div class=\"swiper-slide.*?\">/g)
if (!_.isNull(images)) {
images.map((item, index) => {
const imgUrl = /(?<=data-src=").*?(?=")/.exec(item)[0].replace(/#38/g, "").replace(/;/g, "")
imgPromise.push(downloadImg(imgUrl, `${downloadPath}/${index}.jpg`))
})
}
// TODO 视频会出bug暂时不做
// if (html.includes("data-video")) {
// const video = html.match(/(?<=data-video=").*?(?=")/g)[0].replace(/#38/g, "").replace(/;/g, "")
// this.downloadVideo(video, true).then(path => {
// e.reply(segment.video(path));
// })
// }
})
if (imgPromise.length > 0) {
const images = await Promise.all(imgPromise).then(paths => {
return paths.map(item => {
return {
message: segment.image(fs.readFileSync(item)),
nickname: e.sender.card || e.user_id,
user_id: e.user_id,
};
});
});
await this.reply(await Bot.makeForwardMsg(images));
}
return true;
}
async bodianMusic(e) {
const msg = e.msg.replace("#波点音乐").trim();
const API = `https://xiaobai.klizi.cn/API/music/bodian.php?msg=${msg}&n=&max=`;
@ -669,6 +761,7 @@ export class tools extends plugin {
"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",
Referer: 'https://imginn.com',
httpAgent: tunnel.httpOverHttp({
proxy: { host: this.proxyAddr, port: this.proxyPort },
}),
@ -761,6 +854,8 @@ export class tools extends plugin {
httpsAgent: tunnel.httpOverHttp({
proxy: { host: this.proxyAddr, port: this.proxyPort },
}),
}).catch(err => {
logger.error("下载视频发生错误!")
});
} else {
res = await axios.get(url, {
@ -769,9 +864,10 @@ export class tools extends plugin {
"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",
}).catch(err => {
logger.error("下载视频发生错误!")
});
}
logger.mark(`开始下载: ${url}`);
const writer = fs.createWriteStream(target);
res.data.pipe(writer);