require("dotenv").config(); const fs = require("fs"); const path = require("path"); const { RichText, BskyAgent } = require("@atproto/api"); const axios = require("axios"); const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT }); async function getImages(item) { const responses = item.object.attachment .filter(attachment => attachment.mediaType.includes("image")); const ratios = responses.map(attachment => { return { width: attachment.width, height: attachment.height } }); const respromise = await Promise.all(responses.map(attachment => axios.get( attachment.url, { responseType: "arraybuffer" } ) )); return respromise.map((buf, index) => { return { blob: new Blob( [buf.data], { type: buf.headers["content-type"] } ), width: ratios[index].width, height: ratios[index].height } }) } const sampledata = JSON.parse(fs.readFileSync("example.json")); const firstdata = sampledata.orderedItems[0]; agent.login({ identifier: process.env.BLUESKY_HANDLE, password: process.env.BLUESKY_PASSWORD, }); sampledata.orderedItems.forEach(async element => { let images = await getImages(element); let imgdatas = await Promise.all(images.map(async img => { const buf = await img.blob.arrayBuffer(); const dataArray = new Uint8Array(buf); console.log(`detected img: ${buf},size:${dataArray.byteLength} bytes`); const data = await agent.uploadBlob( dataArray, { // 画像の形式を指定 ('image/jpeg' 等の MIME タイプ) encoding: img.type, } ); console.log(`blob: ${data.data.blob}`); return { alt: "", image: data.data.blob, aspectRatio: { width: img.width, height: img.height } }; })); });