Skip to content

show_profile.rb

Example: fetch profile info of a given user and their last 10 posts (excluding reposts).

This script connects to the AppView server at api.bsky.app, which allows calling such endpoints as getProfile or getAuthorFeed without authentication.

rb
require 'didkit'
require 'minisky'
require 'time'

$handle = ARGV[0]

if $handle.nil?
  puts "Usage: #{$PROGRAM_NAME} <handle | did>"
  exit 1
end

$did = DID.resolve_handle($handle).to_s

if $did.empty?
  puts "Couldn't resolve handle: #{$handle}"
  exit 1
end

# passing nil as config file to use an unauthenticated client
bsky = Minisky.new('api.bsky.app', nil)

# fetch profile info
profile = bsky.get_request('app.bsky.actor.getProfile', { actor: $did })

# fetch posts, without replies - we fetch a bit more than we need because we'll also
# filter out reposts on our side
posts = bsky.get_request('app.bsky.feed.getAuthorFeed', {
  actor: $did,
  filter: 'posts_no_replies',
  limit: 40
})

# print the profile info

puts
puts "====[ @#{profile['handle']}#{profile['displayName']}#{profile['did']} ]===="
puts
puts profile['description']
puts
puts '=' * 80
puts

# print the posts

posts['feed'].map { |r|
  r['post']
}.select { |p|
  # select only posts from this account
  p['author']['did'] == $did
}.slice(0, 10).each { |p|
  time = Time.parse(p['record']['createdAt'])
  timestamp = time.getlocal.strftime('%a %d.%m %H:%M')

  puts "#{timestamp}: #{p['record']['text']}"
  puts
}