Skip to content

Instantly share code, notes, and snippets.

@jiftechnify
Last active March 4, 2023 10:36
Show Gist options
  • Save jiftechnify/1a6cfb1b3c1062b65978713010c5422a to your computer and use it in GitHub Desktop.
Save jiftechnify/1a6cfb1b3c1062b65978713010c5422a to your computer and use it in GitHub Desktop.
Play with Bluesky
import { AtpAgent } from "@atproto/api";
const now = () => new Date().toISOString();
const agent = new AtpAgent({
service: "https://bsky.social",
});
const bsky = agent.api.app.bsky;
const login = async () => {
const loginResp = await agent.login({
identifier: "email@addr",
password: "pw",
});
return loginResp.data;
};
const getFollowers = async (myId) => {
const followerResp = await bsky.graph.getFollowers({ user: myId.handle });
return followerResp.data.followers;
};
const getFollows = async (myId) => {
const getFollowResp = await bsky.graph.getFollows({ user: myId.handle });
return getFollowResp.data.follows;
};
const followUser = async (myId, target) => {
console.log(`following ${target.handle}...`);
const followResp = await bsky.graph.follow.create(
{ did: myId.did },
{
subject: { did: target.did, declarationCid: target.declaration.cid },
createdAt: now(),
}
);
console.log(followResp);
};
const main = async () => {
const myId = await login();
const [followers, followings] = await Promise.all([
getFollowers(myId),
getFollows(myId),
]);
// get followers that are not followed by me
const followersMap = new Map(followers.map((u) => [u.did, u]));
for (const following of followings) {
followersMap.delete(following.did);
}
for (const [, user] of followersMap) {
await followUser(myId, user);
}
};
main()
.then(() => console.log("fin"))
.catch((e) => console.error(e));
import { AtpAgent } from "@atproto/api";
const main = async () => {
const agent = new AtpAgent({
service: "https://bsky.social",
});
await agent.login({
identifier: "email@addr",
password: "pw",
});
const tl = await agent.api.app.bsky.feed.getTimeline({});
for (const p of tl.data.feed) {
console.log({
handle: p.post.author.handle,
name: p.post.author.displayName,
text: p.post.record.text,
reposts: p.post.repostCount,
likes: p.post.upvoteCount,
});
}
};
main()
.then(() => console.log("fin"))
.catch((e) => console.error(e));
import { AtpAgent } from "@atproto/api";
const main = async () => {
const agent = new AtpAgent({
service: "https://bsky.social",
});
const loginResp = await agent.login({
identifier: "email@addr",
password: "pw",
});
const myDid = loginResp.data.did;
await agent.api.app.bsky.feed.post.create(
{ did: myDid },
{ text: "ここにつぶやく内容を入れる", createdAt: new Date().toISOString() }
);
};
main()
.then(() => console.log("fin"))
.catch((e) => console.error(e));
import { AtpAgent } from "@atproto/api";
import { readFile } from "node:fs/promises";
const main = async () => {
const agent = new AtpAgent({
service: "https://bsky.social",
});
await agent.login({
identifier: "email@addr",
password: "pw",
});
// アイコンの画像ファイルを読み込む
const iconImage = await readFile("icon.jpg");
// バナーの画像ファイルを読み込む
const bannerImage = await readFile("banner.jpg");
const uploadIconResp = await agent.api.com.atproto.blob.upload(iconImage, {
encoding: "image/jpeg", // PNGの場合は "image/png" にする
});
const uploadBannerResp = await agent.api.com.atproto.blob.upload(
bannerImage,
{ encoding: "image/jpeg" } // PNGの場合は "image/png" にする
);
// GIFは使えないっぽい
// 初回の場合、updateProfileが使えないかもしれない
// → その場合は agent.api.app.bsky.actor.profile.create を使う
// 逆に、すでにプロフィールが設定されている状態で actor.profile.create を使おうとするとエラーになる
const createProfileResp = await agent.api.app.bsky.actor.updateProfile({
displayName: "表示名",
description: "説明欄",
avatar: { cid: uploadIconResp.data.cid, mimeType: "image/jpeg" }, // PNGの場合は "image/png" にする
banner: { cid: uploadBannerResp.data.cid, mimeType: "image/jpeg" }, // PNGの場合は "image/png" にする
});
console.log(createProfileResp);
};
main()
.then(() => console.log("fin"))
.catch((e) => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment