-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscience_feed.rb
executable file
·53 lines (41 loc) · 2.02 KB
/
science_feed.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
# Example: load last 10 posts from the "What's Science" feed and print the post text, data and author handle to the
# terminal. Does not require any authentication.
#
# It's definitely not the most efficient way to do this, but it demonstrates how to load single records from the API.
# (A more efficient way would be e.g. to connect to the AppView at api.bsky.app and make one call to getPosts.)
# load minisky from a local folder - you normally won't need this
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
require 'minisky'
require 'time'
# the "What's Science" custom feed by @bossett.bsky.social
# the service host is hardcoded here, ideally you should fetch the feed record and read the hostname from there
FEED_HOST = "bs.bossett.io"
FEED_URI = "at://did:plc:jfhpnnst6flqway4eaeqzj2a/app.bsky.feed.generator/for-science"
# fetch the feed from the feed server (getFeedSkeleton returns only a list or URIs of posts)
# pass nil as the config file parameter to create an unauthenticated client
feed_api = Minisky.new(FEED_HOST, nil)
feed = feed_api.get_request('app.bsky.feed.getFeedSkeleton', { feed: FEED_URI, limit: 10 })
# second client instance for the Bluesky API - again, pass nil to use without authentication
bsky = Minisky.new('bsky.social', nil)
# for each post URI, fetch the post record and the profile of its author
entries = feed['feed'].map do |r|
# AT URI is always: at://<did>/<collection>/<rkey>
did, collection, rkey = r['post'].gsub('at://', '').split('/')
print '.'
post = bsky.get_request('com.atproto.repo.getRecord', { repo: did, collection: collection, rkey: rkey })
author = bsky.get_request('com.atproto.repo.describeRepo', { repo: did })
[post, author]
end
puts
entries.each do |post, author|
handle = author['handle']
timestamp = Time.parse(post['value']['createdAt']).getlocal
link = "https://bsky.app/profile/#{handle}/post/#{post['uri'].split('/').last}"
puts "@#{handle} • #{timestamp} • #{link}"
puts
puts post['value']['text']
puts
puts "=" * 120
puts
end