|
| 1 | +import * as Skins from "../data/skins"; |
| 2 | +import { |
| 3 | + AppBskyEmbedImages, |
| 4 | + AppBskyFeedPost, |
| 5 | + AtpAgent, |
| 6 | + BlobRef, |
| 7 | + RichText, |
| 8 | +} from "@atproto/api"; |
| 9 | +import { |
| 10 | + TWEET_BOT_CHANNEL_ID, |
| 11 | + BLUESKY_USERNAME, |
| 12 | + BLUESKY_PASSWORD, |
| 13 | +} from "../config"; |
| 14 | +import { Client } from "discord.js"; |
| 15 | +import sharp from "sharp"; |
| 16 | +import SkinModel from "../data/SkinModel"; |
| 17 | +import UserContext from "../data/UserContext"; |
| 18 | +import { withBufferAsTempFile } from "../utils"; |
| 19 | +import fs from "fs"; |
| 20 | + |
| 21 | +const agent = new AtpAgent({ service: "https://bsky.social" }); |
| 22 | + |
| 23 | +export async function postToBluesky( |
| 24 | + discordClient: Client, |
| 25 | + md5: string | null |
| 26 | +): Promise<void> { |
| 27 | + if (md5 == null) { |
| 28 | + md5 = await Skins.getSkinToPostToBluesky(); |
| 29 | + } |
| 30 | + if (md5 == null) { |
| 31 | + console.error("No skins to post to Bluesky"); |
| 32 | + return; |
| 33 | + } |
| 34 | + const url = await post(md5); |
| 35 | + |
| 36 | + console.log("Going to post to discord"); |
| 37 | + const tweetBotChannel = await discordClient.channels.fetch( |
| 38 | + TWEET_BOT_CHANNEL_ID |
| 39 | + ); |
| 40 | + // @ts-ignore |
| 41 | + await tweetBotChannel.send(url); |
| 42 | + console.log("Posted to discord"); |
| 43 | +} |
| 44 | + |
| 45 | +async function post(md5: string): Promise<string> { |
| 46 | + const ctx = new UserContext(); |
| 47 | + const skin = await SkinModel.fromMd5Assert(ctx, md5); |
| 48 | + const screenshot = await Skins.getScreenshotBuffer(md5); |
| 49 | + const { width, height } = await sharp(screenshot).metadata(); |
| 50 | + |
| 51 | + const image = await sharp(screenshot) |
| 52 | + .resize(width * 2, height * 2, { |
| 53 | + kernel: sharp.kernel.nearest, |
| 54 | + }) |
| 55 | + .toBuffer(); |
| 56 | + |
| 57 | + const name = await skin.getFileName(); |
| 58 | + const url = skin.getMuseumUrl(); |
| 59 | + const screenshotFileName = await skin.getScreenshotFileName(); |
| 60 | + |
| 61 | + const status = `${name}\n`; // TODO: Should we add hashtags? |
| 62 | + |
| 63 | + await agent.login({ |
| 64 | + identifier: BLUESKY_USERNAME!, |
| 65 | + password: BLUESKY_PASSWORD!, |
| 66 | + }); |
| 67 | + |
| 68 | + const blob = await withBufferAsTempFile( |
| 69 | + image, |
| 70 | + screenshotFileName, |
| 71 | + async (filePath) => { |
| 72 | + return uploadImageFromFilePath(agent, filePath); |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + const postData = await buildPost( |
| 77 | + agent, |
| 78 | + status, |
| 79 | + buildImageEmbed(blob, width * 2, height * 2) |
| 80 | + ); |
| 81 | + const postResp = await agent.post(postData); |
| 82 | + console.log(postResp); |
| 83 | + |
| 84 | + const postId = postResp.cid; |
| 85 | + const postUrl = postResp.uri; |
| 86 | + |
| 87 | + await Skins.markAsPostedToBlueSky(md5, postId, postUrl); |
| 88 | + |
| 89 | + const prefix = "Try on the "; |
| 90 | + const suffix = "Winamp Skin Museum"; |
| 91 | + |
| 92 | + agent.post({ |
| 93 | + text: prefix + suffix, |
| 94 | + createdAt: new Date().toISOString(), |
| 95 | + facets: [ |
| 96 | + { |
| 97 | + $type: "app.bsky.richtext.facet", |
| 98 | + index: { |
| 99 | + byteStart: prefix.length, |
| 100 | + byteEnd: prefix.length + suffix.length, |
| 101 | + }, |
| 102 | + features: [ |
| 103 | + { |
| 104 | + $type: "app.bsky.richtext.facet#link", |
| 105 | + uri: url, |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ], |
| 110 | + reply: { |
| 111 | + root: postResp, |
| 112 | + parent: postResp, |
| 113 | + }, |
| 114 | + $type: "app.bsky.feed.post", |
| 115 | + }); |
| 116 | + |
| 117 | + // return permalink; |
| 118 | + return postUrl; |
| 119 | +} |
| 120 | + |
| 121 | +/** Build the embed data for an image. */ |
| 122 | +function buildImageEmbed( |
| 123 | + imgBlob: BlobRef, |
| 124 | + width: number, |
| 125 | + height: number |
| 126 | +): AppBskyEmbedImages.Main { |
| 127 | + const image = { |
| 128 | + image: imgBlob, |
| 129 | + aspectRatio: { width, height }, |
| 130 | + alt: "", |
| 131 | + }; |
| 132 | + return { |
| 133 | + $type: "app.bsky.embed.images", |
| 134 | + images: [image], |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +/** Build the post data for an image. */ |
| 139 | +async function buildPost( |
| 140 | + agent: AtpAgent, |
| 141 | + rawText: string, |
| 142 | + imageEmbed: AppBskyEmbedImages.Main |
| 143 | +): Promise<AppBskyFeedPost.Record> { |
| 144 | + const rt = new RichText({ text: rawText }); |
| 145 | + await rt.detectFacets(agent); |
| 146 | + const { text, facets } = rt; |
| 147 | + return { |
| 148 | + text, |
| 149 | + facets, |
| 150 | + $type: "app.bsky.feed.post", |
| 151 | + createdAt: new Date().toISOString(), |
| 152 | + embed: { |
| 153 | + $type: "app.bsky.embed.recordWithMedia", |
| 154 | + ...imageEmbed, |
| 155 | + }, |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +/** Upload an image from a URL to Bluesky. */ |
| 160 | +async function uploadImageFromFilePath( |
| 161 | + agent: AtpAgent, |
| 162 | + filePath: string |
| 163 | +): Promise<BlobRef> { |
| 164 | + const imageBuff = fs.readFileSync(filePath); |
| 165 | + const imgU8 = new Uint8Array(imageBuff); |
| 166 | + |
| 167 | + const dstResp = await agent.uploadBlob(imgU8); |
| 168 | + if (!dstResp.success) { |
| 169 | + console.log(dstResp); |
| 170 | + throw new Error("Failed to upload image"); |
| 171 | + } |
| 172 | + return dstResp.data.blob; |
| 173 | +} |
0 commit comments