import ky from 'https://esm.sh/ky'; import credentials from "./credentials.json" with { type: "json" }; var {matrix_token, pixiv_cookie, pixiv_user_id} = credentials; var ke = ky.create({ hooks: { beforeRequest: [request => console.log(`${request.method} ${request.url}`)], afterResponse: [(_request, _options, response) => console.log(response.status)] }, retry: { limit: 100, delay: attemptCount => (2 ** (attemptCount - 1)) * 1000 }, timeout: false }); var matrix = { ky: ke.extend({headers: {"Authorization": `Bearer ${credentials.matrix_token}`}}), rooms: { default: "!mxhaBcBiEmnGxrffll:matrix.org", pixiv_sfw: "!zStOGJAGxEaQgwxGYI:matrix.org", pixiv_nsfw: "!HtXDHeAhEKBFGfAJTi:matrix.org" }, async sendMessage({room = this.rooms.default, type = "m.text", body, filename, info, url}) { return await this.ky.put(`https://matrix-client.matrix.org/_matrix/client/v3/rooms/${encodeURIComponent(room)}/send/m.room.message/${Math.random().toString()}`, { json: {msgtype: type, body, filename, info, url} }).json(); }, async uploadFile(filename, data) { var {content_uri} = await this.ky.post(`https://matrix-client.matrix.org/_matrix/media/v3/upload?filename=${encodeURIComponent(filename)}`, {body: data}).json(); return content_uri; }, async sendImage({room = this.rooms.default, body, filename, data, info}) { var content_uri = await this.uploadFile(filename, data); return await this.sendMessage({room, type: "m.image", body: body || filename, filename, url: content_uri, info}); } } async function downloadPixivIllust(illust_id) { var html = await ke.get(`https://www.pixiv.net/en/artworks/${illust_id}`, {headers: {"Cookie": credentials.pixiv_cookie}}).text(); var illust = Object.values(JSON.parse(html.match(//)[1]).illust)[0]; var images = []; for (let i = 0; i < illust.pageCount; i++) { let url = illust.urls.original.replace('p0', 'p'+i); let data = await ke.get(url, {headers: {"Referer": "https://www.pixiv.net/"}}).then(res => res.blob()); images.push({url, name: url.split('/').pop(), data}); }; return {illust, images}; } export async function pixivToMatrix(illust_id) { try { var {illust, images} = await downloadPixivIllust(illust_id); var room = illust.xRestrict ? matrix.rooms.pixiv_nsfw : matrix.rooms.pixiv_sfw; await matrix.sendMessage({room, body: `https://www.pixiv.net/en/artworks/${illust_id}`}); for (var image of images) { try { await matrix.sendImage({ room, filename: image.name, data: image.data, info: { mimetype: image.data.type, size: image.data.size } }); } catch (error) { onError(error); } } } catch (error) { onError(error); } function onError(error) { console.error(error.stack); matrix.sendMessage({room: room || matrix.rooms.pixiv_sfw, body: error.stack}).catch(error => { console.error(error.stack); }); } } var new_ids = []; top: for (var offset = 0;;) { let url = `https://www.pixiv.net/ajax/user/${pixiv_user_id}/illusts/bookmarks?tag=&offset=${offset}&limit=100&rest=show&lang=en&version=5dc84ab282403a049abea4e2f2214b6a69d31da6`; console.log("get", url); let data = await fetch(url, {headers: {Cookie: pixiv_cookie}}).then(res => res.json()); let ids = data.body.works.map(x => x.id); if (!ids.length) break; for (let id of ids) { if (["119320087", "119534122"].includes(id)) break top; new_ids.push(id); } offset += ids.length; } console.log("new ids", new_ids); for (let id of new_ids.reverse()) { await pixivToMatrix(id); }