minisky 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -15
- data/LICENSE.txt +1 -1
- data/README.md +22 -15
- data/SECURITY.md +16 -0
- data/lib/minisky/compat.rb +22 -0
- data/lib/minisky/errors.rb +76 -4
- data/lib/minisky/minisky.rb +52 -11
- data/lib/minisky/requests.rb +360 -41
- data/lib/minisky/version.rb +3 -1
- metadata +12 -17
- data/example/ask_password.rb +0 -76
- data/example/fetch_my_posts.rb +0 -46
- data/example/fetch_profile.rb +0 -53
- data/example/find_missing_follows.rb +0 -67
- data/example/post_skeet.rb +0 -36
- data/example/science_feed.rb +0 -53
data/example/ask_password.rb
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
# Example: print 10 latest posts from the user's home feed.
|
|
4
|
-
#
|
|
5
|
-
# Instead of using a config file to read & store authentication info, this example uses a customized client class
|
|
6
|
-
# which reads the password from the console and creates a throwaway access token.
|
|
7
|
-
#
|
|
8
|
-
# This approach makes sense for one-off scripts, but it shouldn't be used for things that need to be done repeatedly
|
|
9
|
-
# and often (the authentication-related endpoints have lower rate limits than others).
|
|
10
|
-
|
|
11
|
-
# load minisky from a local folder - you normally won't need this
|
|
12
|
-
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
13
|
-
|
|
14
|
-
require 'io/console'
|
|
15
|
-
require 'minisky'
|
|
16
|
-
|
|
17
|
-
class TransientClient
|
|
18
|
-
include Minisky::Requests
|
|
19
|
-
|
|
20
|
-
attr_reader :config, :host
|
|
21
|
-
|
|
22
|
-
def initialize(host, user)
|
|
23
|
-
@host = host
|
|
24
|
-
@config = { 'id' => user.gsub(/^@/, '') }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def ask_for_password
|
|
28
|
-
print "Enter password for @#{config['id']}: "
|
|
29
|
-
@config['pass'] = STDIN.noecho(&:gets).chomp
|
|
30
|
-
puts
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def save_config
|
|
34
|
-
# ignore
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
host, handle = ARGV
|
|
39
|
-
|
|
40
|
-
unless host && handle
|
|
41
|
-
puts "Usage: #{$PROGRAM_NAME} <pds_hostname> <handle>"
|
|
42
|
-
exit 1
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
# create a client instance & read password
|
|
46
|
-
bsky = TransientClient.new(host, handle)
|
|
47
|
-
bsky.ask_for_password
|
|
48
|
-
|
|
49
|
-
# fetch 10 posts from the user's home feed
|
|
50
|
-
result = bsky.get_request('app.bsky.feed.getTimeline', { limit: 10 })
|
|
51
|
-
|
|
52
|
-
result['feed'].each do |r|
|
|
53
|
-
reason = r['reason']
|
|
54
|
-
reply = r['reply']
|
|
55
|
-
post = r['post']
|
|
56
|
-
|
|
57
|
-
if reason && reason['$type'] == 'app.bsky.feed.defs#reasonRepost'
|
|
58
|
-
puts "[Reposted by @#{reason['by']['handle']}]"
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
handle = post['author']['handle']
|
|
62
|
-
timestamp = Time.parse(post['record']['createdAt']).getlocal
|
|
63
|
-
|
|
64
|
-
puts "@#{handle} • #{timestamp}"
|
|
65
|
-
puts
|
|
66
|
-
|
|
67
|
-
if reply
|
|
68
|
-
puts "[in reply to @#{reply['parent']['author']['handle']}]"
|
|
69
|
-
puts
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
puts post['record']['text']
|
|
73
|
-
puts
|
|
74
|
-
puts "=" * 120
|
|
75
|
-
puts
|
|
76
|
-
end
|
data/example/fetch_my_posts.rb
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
# Example: sync all posts from your account (excluding replies and reposts) to a local JSON file. When run again, it
|
|
4
|
-
# will only fetch new posts since the last time and append them to the file.
|
|
5
|
-
#
|
|
6
|
-
# Requires a bluesky.yml config file in the same directory with contents like this:
|
|
7
|
-
# id: your.handle
|
|
8
|
-
# pass: secretpass
|
|
9
|
-
|
|
10
|
-
# load minisky from a local folder - you normally won't need this
|
|
11
|
-
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
12
|
-
|
|
13
|
-
require 'minisky'
|
|
14
|
-
|
|
15
|
-
CONFIG_FILE = File.join(__dir__, 'bluesky.yml')
|
|
16
|
-
POSTS_FILE = File.join(__dir__, 'posts.json')
|
|
17
|
-
|
|
18
|
-
# create a client instance
|
|
19
|
-
bsky = Minisky.new('bsky.social', CONFIG_FILE)
|
|
20
|
-
|
|
21
|
-
# print progress dots when loading multiple pages
|
|
22
|
-
bsky.default_progress = '.'
|
|
23
|
-
|
|
24
|
-
# load previously saved posts; we will only fetch posts newer than the last saved before
|
|
25
|
-
posts = File.exist?(POSTS_FILE) ? JSON.parse(File.read(POSTS_FILE)) : []
|
|
26
|
-
latest_date = posts[0] && posts[0]['indexedAt']
|
|
27
|
-
|
|
28
|
-
# fetch all posts from my timeline (without replies) until the target timestamp
|
|
29
|
-
results = bsky.fetch_all('app.bsky.feed.getAuthorFeed',
|
|
30
|
-
{ actor: bsky.user.did, filter: 'posts_no_replies', limit: 100 },
|
|
31
|
-
field: 'feed',
|
|
32
|
-
break_when: latest_date && proc { |x| x['post']['indexedAt'] <= latest_date }
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
# trim some data to save space
|
|
36
|
-
new_posts = results.map { |x| x['post'] }
|
|
37
|
-
.reject { |x| x['author']['did'] != bsky.user.did } # skip reposts
|
|
38
|
-
.map { |x| x.except('author') } # skip author profile info
|
|
39
|
-
|
|
40
|
-
posts = new_posts + posts
|
|
41
|
-
|
|
42
|
-
puts
|
|
43
|
-
puts "Fetched #{new_posts.length} new posts (total = #{posts.length})"
|
|
44
|
-
|
|
45
|
-
# save all new and old posts back to the file
|
|
46
|
-
File.write(POSTS_FILE, JSON.pretty_generate(posts))
|
data/example/fetch_profile.rb
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
# Example: fetch the profile info of a given user and their last 10 posts (excluding reposts).
|
|
4
|
-
#
|
|
5
|
-
# This script connects to the AppView server at api.bsky.app, which allows calling such endpoints as getProfile or
|
|
6
|
-
# getAuthorFeed without authentication.
|
|
7
|
-
|
|
8
|
-
# load minisky from a local folder - you normally won't need this
|
|
9
|
-
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
10
|
-
|
|
11
|
-
require 'minisky'
|
|
12
|
-
require 'time'
|
|
13
|
-
|
|
14
|
-
if ARGV[0].to_s !~ /^@?[\w\-]+(\.[\w\-]+)+$/
|
|
15
|
-
puts "Usage: #{$PROGRAM_NAME} <handle>"
|
|
16
|
-
exit 1
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
handle = ARGV[0].gsub(/^@/, '')
|
|
20
|
-
|
|
21
|
-
# passing nil as config file to use an unauthenticated client
|
|
22
|
-
bsky = Minisky.new('api.bsky.app', nil)
|
|
23
|
-
|
|
24
|
-
# fetch profile info
|
|
25
|
-
profile = bsky.get_request('app.bsky.actor.getProfile', { actor: handle })
|
|
26
|
-
|
|
27
|
-
# fetch posts, without replies - we fetch a bit more than we need because we'll also filter out reposts
|
|
28
|
-
posts = bsky.get_request('app.bsky.feed.getAuthorFeed', { actor: handle, filter: 'posts_no_replies', limit: 40 })
|
|
29
|
-
|
|
30
|
-
# print the profile
|
|
31
|
-
|
|
32
|
-
puts
|
|
33
|
-
puts "====[ @#{handle} • #{profile['displayName']} • #{profile['did']} ]===="
|
|
34
|
-
puts
|
|
35
|
-
puts profile['description']
|
|
36
|
-
puts
|
|
37
|
-
puts '=' * 80
|
|
38
|
-
puts
|
|
39
|
-
|
|
40
|
-
# print the posts
|
|
41
|
-
|
|
42
|
-
posts['feed'].map { |r|
|
|
43
|
-
r['post']
|
|
44
|
-
}.select { |p|
|
|
45
|
-
# select only posts from this account
|
|
46
|
-
p['author']['handle'] == handle
|
|
47
|
-
}.slice(0, 10).each { |p|
|
|
48
|
-
time = Time.parse(p['record']['createdAt'])
|
|
49
|
-
timestamp = time.getlocal.strftime('%a %d.%m %H:%M')
|
|
50
|
-
|
|
51
|
-
puts "#{timestamp}: #{p['record']['text']}"
|
|
52
|
-
puts
|
|
53
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
# Example: fetch the list of accounts followed by a given user and check which of them have been deleted / deactivated.
|
|
4
|
-
|
|
5
|
-
# load minisky from a local folder - you normally won't need this
|
|
6
|
-
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
7
|
-
|
|
8
|
-
require 'didkit'
|
|
9
|
-
require 'minisky'
|
|
10
|
-
|
|
11
|
-
handle = ARGV[0].to_s.gsub(/^@/, '')
|
|
12
|
-
if handle.empty?
|
|
13
|
-
puts "Usage: #{$PROGRAM_NAME} <handle>"
|
|
14
|
-
exit 1
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
pds_host = DID.resolve_handle(handle).get_document.pds_endpoint
|
|
18
|
-
pds = Minisky.new(pds_host, nil, progress: '.')
|
|
19
|
-
|
|
20
|
-
print "Fetching all follows of @#{handle} from #{pds_host}: "
|
|
21
|
-
|
|
22
|
-
follows = pds.fetch_all('com.atproto.repo.listRecords',
|
|
23
|
-
{ repo: handle, collection: 'app.bsky.graph.follow', limit: 100 }, field: 'records')
|
|
24
|
-
|
|
25
|
-
puts
|
|
26
|
-
puts "Found #{follows.length} follows"
|
|
27
|
-
|
|
28
|
-
appview = Minisky.new('api.bsky.app', nil)
|
|
29
|
-
|
|
30
|
-
profiles = []
|
|
31
|
-
i = 0
|
|
32
|
-
|
|
33
|
-
puts
|
|
34
|
-
print "Fetching profiles of all followed accounts: "
|
|
35
|
-
|
|
36
|
-
# getProfiles lets us load multiple profiles in one request, but only up to 25 in one batch
|
|
37
|
-
|
|
38
|
-
while i < follows.length
|
|
39
|
-
batch = follows[i...i+25]
|
|
40
|
-
dids = batch.map { |x| x['value']['subject'] }
|
|
41
|
-
print '.'
|
|
42
|
-
result = appview.get_request('app.bsky.actor.getProfiles', { actors: dids })
|
|
43
|
-
profiles += result['profiles']
|
|
44
|
-
i += 25
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# these are DIDs that are on the follows list, but aren't being returned from getProfiles
|
|
48
|
-
missing = follows.map { |x| x['value']['subject'] } - profiles.map { |x| x['did'] }
|
|
49
|
-
|
|
50
|
-
puts
|
|
51
|
-
puts "#{missing.length} followed accounts are missing:"
|
|
52
|
-
puts
|
|
53
|
-
|
|
54
|
-
missing.each do |did|
|
|
55
|
-
begin
|
|
56
|
-
doc = DID.new(did).get_document
|
|
57
|
-
rescue OpenURI::HTTPError
|
|
58
|
-
puts "#{did} (?) => DID not found"
|
|
59
|
-
next
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# check account status on their assigned PDS
|
|
63
|
-
pds = Minisky.new(doc.pds_endpoint.gsub('http://', ''), nil)
|
|
64
|
-
status = pds.get_request('com.atproto.sync.getRepoStatus', { did: did }).slice('status', 'active') rescue 'deleted'
|
|
65
|
-
|
|
66
|
-
puts "#{did} (@#{doc.handles.first}) => #{status}"
|
|
67
|
-
end
|
data/example/post_skeet.rb
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
# Example: make a new post (aka "skeet") with text passed in the argument to the script.
|
|
4
|
-
#
|
|
5
|
-
# Requires a bluesky.yml config file in the same directory with contents like this:
|
|
6
|
-
# id: your.handle
|
|
7
|
-
# pass: secretpass
|
|
8
|
-
|
|
9
|
-
# load minisky from a local folder - you normally won't need this
|
|
10
|
-
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
11
|
-
|
|
12
|
-
require 'minisky'
|
|
13
|
-
|
|
14
|
-
if ARGV[0].to_s.empty?
|
|
15
|
-
puts "Usage: #{$PROGRAM_NAME} <text>"
|
|
16
|
-
exit 1
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
text = ARGV[0]
|
|
20
|
-
|
|
21
|
-
# create a client instance
|
|
22
|
-
bsky = Minisky.new('bsky.social', File.join(__dir__, 'bluesky.yml'))
|
|
23
|
-
|
|
24
|
-
# to make a post, we upload a post record to the posts collection (app.bsky.feed.post) in the user's repo
|
|
25
|
-
|
|
26
|
-
bsky.post_request('com.atproto.repo.createRecord', {
|
|
27
|
-
repo: bsky.user.did,
|
|
28
|
-
collection: 'app.bsky.feed.post',
|
|
29
|
-
record: {
|
|
30
|
-
text: text,
|
|
31
|
-
createdAt: Time.now.iso8601, # we need to set the date to current time manually
|
|
32
|
-
langs: ["en"] # if a post does not have a language set, it may be autodetected as an incorrect language
|
|
33
|
-
}
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
puts "Posted ✓"
|
data/example/science_feed.rb
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
# Example: load last 10 posts from the "What's Science" feed and print the post text, data and author handle to the
|
|
4
|
-
# terminal. Does not require any authentication.
|
|
5
|
-
#
|
|
6
|
-
# It's definitely not the most efficient way to do this, but it demonstrates how to load single records from the API.
|
|
7
|
-
# (A more efficient way would be e.g. to connect to the AppView at api.bsky.app and make one call to getPosts.)
|
|
8
|
-
|
|
9
|
-
# load minisky from a local folder - you normally won't need this
|
|
10
|
-
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
11
|
-
|
|
12
|
-
require 'minisky'
|
|
13
|
-
require 'time'
|
|
14
|
-
|
|
15
|
-
# the "What's Science" custom feed by @bossett.bsky.social
|
|
16
|
-
# the service host is hardcoded here, ideally you should fetch the feed record and read the hostname from there
|
|
17
|
-
FEED_HOST = "bs.bossett.io"
|
|
18
|
-
FEED_URI = "at://did:plc:jfhpnnst6flqway4eaeqzj2a/app.bsky.feed.generator/for-science"
|
|
19
|
-
|
|
20
|
-
# fetch the feed from the feed server (getFeedSkeleton returns only a list or URIs of posts)
|
|
21
|
-
# pass nil as the config file parameter to create an unauthenticated client
|
|
22
|
-
feed_api = Minisky.new(FEED_HOST, nil)
|
|
23
|
-
feed = feed_api.get_request('app.bsky.feed.getFeedSkeleton', { feed: FEED_URI, limit: 10 })
|
|
24
|
-
|
|
25
|
-
# second client instance for the Bluesky API - again, pass nil to use without authentication
|
|
26
|
-
bsky = Minisky.new('bsky.social', nil)
|
|
27
|
-
|
|
28
|
-
# for each post URI, fetch the post record and the profile of its author
|
|
29
|
-
entries = feed['feed'].map do |r|
|
|
30
|
-
# AT URI is always: at://<did>/<collection>/<rkey>
|
|
31
|
-
did, collection, rkey = r['post'].gsub('at://', '').split('/')
|
|
32
|
-
|
|
33
|
-
print '.'
|
|
34
|
-
post = bsky.get_request('com.atproto.repo.getRecord', { repo: did, collection: collection, rkey: rkey })
|
|
35
|
-
author = bsky.get_request('com.atproto.repo.describeRepo', { repo: did })
|
|
36
|
-
|
|
37
|
-
[post, author]
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
puts
|
|
41
|
-
|
|
42
|
-
entries.each do |post, author|
|
|
43
|
-
handle = author['handle']
|
|
44
|
-
timestamp = Time.parse(post['value']['createdAt']).getlocal
|
|
45
|
-
link = "https://bsky.app/profile/#{handle}/post/#{post['uri'].split('/').last}"
|
|
46
|
-
|
|
47
|
-
puts "@#{handle} • #{timestamp} • #{link}"
|
|
48
|
-
puts
|
|
49
|
-
puts post['value']['text']
|
|
50
|
-
puts
|
|
51
|
-
puts "=" * 120
|
|
52
|
-
puts
|
|
53
|
-
end
|