Compare commits

..

2 Commits

Author SHA1 Message Date
3b8270e9a7 !@ 2021-08-23 19:18:32 -07:00
f5ba580eb1 optimize
save data already downloaded so proxy doesnt have to download it again
2021-08-23 19:17:47 -07:00

View File

@ -22,23 +22,22 @@ client.on("messageCreate", async message => {
for (let pixiv_url of pixiv_urls) { for (let pixiv_url of pixiv_urls) {
try { try {
let illust = Object.values(JSON.parse((await (await fetch(pixiv_url)).text()).match(/<meta name="preload-data" id="meta-preload-data" content='(.*)'>/)[1]).illust)[0]; let illust = Object.values(JSON.parse((await (await fetch(pixiv_url)).text()).match(/<meta name="preload-data" id="meta-preload-data" content='(.*)'>/)[1]).illust)[0];
let image_urls = []; let images = [];
for (let i = 0; i < illust.pageCount; i++) image_urls.push(illust.urls.original.replace('p0', 'p'+i)); for (let i = 0; i < illust.pageCount; i++) images.push({url: illust.urls.original.replace('p0', 'p'+i)});
if (image_urls.length <= 10) { if (images.length <= 10) {
try { try {
let files = []; await message.channel.send({files: images.map(image => ({attachment: image.data = await (await fetch(image_url, {headers: {"Referer": "https://www.pixiv.net/"}})).buffer(), name: image.url.split('/').pop()}))});
for (let image_url of image_urls) {
files.push({attachment: await (await fetch(image_url,{headers:{"Referer":"https://www.pixiv.net/"}})).buffer(), name: image_url.split('/').pop()});
}
await message.channel.send({files});
} catch (error) { } catch (error) {
if (error.message == "Request entity too large") await fallback(); if (error.message == "Request entity too large") await fallback();
else throw error; else throw error;
} }
} else await fallback(); } else await fallback();
function fallback() { function fallback() {
image_urls = image_urls.map(url => url.replace("https://i.pximg.net/", `https://${config.web_hostname}/pixiv-proxy/`)); // save the data already downloaded
return message.channel.send(image_urls.join("\n")); for (let image of images) {
fs.writeFileSync(config.data_dir + "pixiv-cache/" + image.url.split('?')[0].replace(/\//g, '\\'), image.data);
}
return message.channel.send(images.map(image => image.url.replace("https://i.pximg.net/", `https://${config.web_hostname}/pixiv-proxy/`)).join('\n'));
} }
} catch (error) { } catch (error) {
console.error("pixiv embed error,", error.stack); console.error("pixiv embed error,", error.stack);