import { Bot, RichText } from "@skyware/bot"; import { Jimp } from "jimp"; import { PIXIV_COOKIE, BSKY_USER, BSKY_PASSWORD } from "./credentials.mjs"; var USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"; async function fetch(url, options) { console.log("fetch", url); var res = await globalThis.fetch(url, options); if (!res.ok) throw new Error(`HTTP ${res.status} ${res.statusText}`); return res; } const bot = new Bot({ emitEvents: false }); await bot.login({ identifier: BSKY_USER, password: BSKY_PASSWORD }); var page = 1 + Math.floor(Math.random() * 1000); console.log("page", page); var res = await fetch(`https://www.pixiv.net/ajax/search/artworks/%E5%88%9D%E9%9F%B3%E3%83%9F%E3%82%AF?word=%E5%88%9D%E9%9F%B3%E3%83%9F%E3%82%AF&order=date_d&mode=safe&p=${page}&csw=0&s_mode=s_tag&type=all&lang=en&version=59e08d0871b7c68569ebe89084d52eca68a1685d`, { headers: { Cookie: PIXIV_COOKIE, "User-Agent": USER_AGENT }, }); if (res.status != 200) { console.error(res.status, await res.text()); } else { var data = await res.json(); } if (data.error) throw data; var illusts = data.body.illustManga.data; var randomIllust = illusts[Math.floor(Math.random() * illusts.length)]; var illust = await fetch(`https://www.pixiv.net/ajax/illust/${randomIllust.id}?lang=en`, { headers: { Cookie: PIXIV_COOKIE, "User-Agent": USER_AGENT } }).then(res => res.json()); if (illust.error) throw illust; illust = illust.body; var illustUrl = `https://www.pixiv.net/en/artworks/${illust.id}`; console.log(illustUrl); var images = []; for (let i = 0; i < Math.min(4, illust.pageCount); i++) { let url = illust.urls.regular.replace('p0', 'p'+i); let data = await fetch(url, { headers: {"Referer": "https://www.pixiv.net/"} }).then(res => res.blob()); if (data.size >= 1e6) { console.log(`converting image size ${data.size} type ${data.type}`); let j = await Jimp.fromBuffer(await data.arrayBuffer()); data = await j.getBuffer("image/jpeg", { quality: 80 }); data = new Blob([data], {type: "image/jpeg"}); console.log(`converted size: ${data.size}`); } images.push({ alt: url.split('/').pop(), data }); } var text = new RichText(); var illustDate = new Date(illust.createDate).toLocaleString("en-US", {timeZone: "JST", month: "long", day: "numeric", year: "numeric"}); text.addText(`${illust.title} / ${illust.userName} / ${illustDate}\n`); if (illust.aiType == 2) text.addTag(`AIgenerated`).addText(' '); for (let tag of randomIllust.tags) { if (["loli","lolicon","shota","shotacon"].includes(tag)) { tag = tag.split().join("\u2063"); text.addText(`#${tag} `); } else { text.addTag(tag).addText(' '); } } if (illust.pageCount > 4) { text.addText(`\n${illust.pageCount - 4} more images are on Pixiv`); } text.addText('\n'); text.addLink(illustUrl.replace("https://",""), illustUrl); await bot.post({text, images});