Skip to content

Instantly share code, notes, and snippets.

@ajmas
Created March 12, 2025 01:54
Show Gist options
  • Save ajmas/a247013b66bcc2642ce36212d050603c to your computer and use it in GitHub Desktop.
Save ajmas/a247013b66bcc2642ce36212d050603c to your computer and use it in GitHub Desktop.
Fetches the posts a person has made in Bluesky
import { AtpAgent, AtpSessionEvent, AtpSessionData } from '@atproto/api'
import { FeedViewPost } from '@atproto/api/dist/client/types/app/bsky/feed/defs';
import { ExternalEmbedView, ImagesEmbedView, VideoEmbedView } from '@atproto/bsky/dist/views/types';
const identifier = 'myuser.bsky.social';
const password = 'xxxx-xxxx-xxx-xxxx';
const bSkyService = 'https://bsky.social';
const bSkyPublicService = 'https://public.api.bsky.app';
function displayPost (feedEntry: FeedViewPost) {
const post = feedEntry.post;
const simplifiedPost = {
author: post.author.displayName,
createdAt: post.record.createdAt,
body: post.record.text,
lang: post.record.langs,
media: [] as any[]
};
if (post.embed && post.embed && post.embed.$type === 'app.bsky.embed.images#view') {
const images = (post.embed as ImagesEmbedView).images;
images.forEach((image: Record<string, any>) => {
simplifiedPost.media.push({
type: 'image',
fullSzie: image.fullsize,
aspectRatio: image.aspectRatio,
alt: image.alt
});
});
}
if (post.embed && post.embed && post.embed.$type === 'app.bsky.embed.video#view') {
const video = (post.embed as VideoEmbedView);
simplifiedPost.media.push({
type: 'video',
playlist: video.playlist,
thumbnail: video.thumbnail,
aspectRatio: video.aspectRatio,
alt: video.alt
});
}
if (post.embed && post.embed && post.embed.$type === 'app.bsky.embed.external#view') {
const externalEmbed = post.embed as ExternalEmbedView;
simplifiedPost.media.push({
type: 'external',
uri: externalEmbed.external.uri,
title: externalEmbed.external.title,
description: externalEmbed.external.description,
thumbnail: externalEmbed.external.thumb
});
}
return simplifiedPost;
}
async function mainWithAuth (userName: string) {
const agent = new AtpAgent({
service: bSkyService,
persistSession: (evt: AtpSessionEvent, sess?: AtpSessionData) => {
// store the session-data for reuse
},
});
await agent.login({
identifier,
password,
});
const response = await agent.getAuthorFeed({
actor: userName,
includePins: false,
filter: 'posts_no_replies'
});
console.log('');
const feed = response.data.feed;
feed.forEach(feedEntry => console.log(JSON.stringify(displayPost(feedEntry), undefined, 2)));
}
async function mainWithoutAuth (userName: string) {
const agent = new AtpAgent({
service: bSkyPublicService
});
const response = await agent.getAuthorFeed({
actor: userName,
includePins: false,
filter: 'posts_no_replies'
});
const feed = response.data.feed;
feed.forEach(feedEntry => console.log(JSON.stringify(displayPost(feedEntry), undefined, 2)));
}
mainWithoutAuth('xxxx.bsky.social').catch(error => {
console.log(error);
process.exit(1);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment