Module: Tempest::Commands::Feed
- Defined in:
- lib/tempest/commands/feed.rb
Constant Summary collapse
- DEFAULT_LIMIT =
50- MAX_LIMIT =
100
Class Method Summary collapse
- .call(argv:, session:, client:, stdout:, stderr:) ⇒ Object
- .emit(items, format:, stdout:) ⇒ Object
- .endpoint_for(subcommand, session:, positional:, client:) ⇒ Object
- .filter_by_date(items, opts) ⇒ Object
- .parse(argv, stderr:) ⇒ Object
Class Method Details
.call(argv:, session:, client:, stdout:, stderr:) ⇒ Object
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 54 55 56 57 58 59 60 61 62 |
# File 'lib/tempest/commands/feed.rb', line 18 def call(argv:, session:, client:, stdout:, stderr:) subcommand, rest = argv.first, argv.drop(1) unless %w[me timeline author].include?(subcommand) stderr.puts "usage: tempest feed me|timeline|author <handle> [opts]" return 64 end opts, positional = parse(rest, stderr: stderr) return 64 if opts.nil? nsid, base_query = endpoint_for(subcommand, session: session, positional: positional, client: client) if nsid.nil? stderr.puts "error: feed author requires a handle or DID" return 64 end if opts[:limit] > MAX_LIMIT stderr.puts "error: --limit must be <= #{MAX_LIMIT}" return 64 end items = [] cursor = nil max_pages = 5 pages = 0 loop do query = base_query.merge("limit" => opts[:limit]) query["cursor"] = cursor if cursor response = client.get(nsid, query: query) page_items = Array(response["feed"]).map { |entry| entry["post"] } items.concat(page_items) pages += 1 cursor = response["cursor"] break if cursor.nil? || cursor.empty? break if pages >= max_pages break unless opts[:since] oldest = page_items.last && page_items.last.dig("record", "createdAt") break if oldest.nil? break if Time.iso8601(oldest) < opts[:since] end stderr.puts "warning: pagination cap of #{max_pages} pages reached; result truncated" if pages >= max_pages && !cursor.nil? && !cursor.empty? items = filter_by_date(items, opts) emit(items, format: opts[:format], stdout: stdout) 0 end |
.emit(items, format:, stdout:) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/tempest/commands/feed.rb', line 122 def emit(items, format:, stdout:) format ||= stdout.respond_to?(:tty?) && stdout.tty? ? :line : :json case format when :json views = items.map { |i| Tempest::PostView.from_feed_view(i) } Tempest::Output::JsonWriter.new(stdout).write_posts(views) when :line posts = items.map { |i| Tempest::Post.from_feed_view(i) } Tempest::Output::LineWriter.new(stdout).write_posts(posts) when :raw Tempest::Output::JsonWriter.new(stdout).write_raw({ "feed" => items.map { |i| { "post" => i } } }) end end |
.endpoint_for(subcommand, session:, positional:, client:) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tempest/commands/feed.rb', line 97 def endpoint_for(subcommand, session:, positional:, client:) case subcommand when "me" ["app.bsky.feed.getAuthorFeed", { "actor" => session.did }] when "timeline" ["app.bsky.feed.getTimeline", {}] when "author" actor = positional.first if actor.nil? || actor.empty? return [nil, nil] end did = Tempest::HandleLookup.resolve(actor, client: client) ["app.bsky.feed.getAuthorFeed", { "actor" => did }] end end |
.filter_by_date(items, opts) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/tempest/commands/feed.rb', line 113 def filter_by_date(items, opts) return items if opts[:since].nil? && opts[:until_at].nil? items.select do |it| ts = it.dig("record", "createdAt") t = Time.iso8601(ts) (opts[:since].nil? || t >= opts[:since]) && (opts[:until_at].nil? || t < opts[:until_at]) end end |
.parse(argv, stderr:) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/tempest/commands/feed.rb', line 64 def parse(argv, stderr:) opts = { limit: DEFAULT_LIMIT, since: nil, until_at: nil, format: nil } positional = [] i = 0 while i < argv.length case argv[i] when /\A--limit=(\d+)\z/ then opts[:limit] = Regexp.last_match(1).to_i; i += 1 when "--limit" then opts[:limit] = argv[i + 1].to_i; i += 2 when /\A--since=(.+)\z/ then opts[:since] = Tempest::DateFilter.parse(Regexp.last_match(1)); i += 1 when "--since" then opts[:since] = Tempest::DateFilter.parse(argv[i + 1]); i += 2 when /\A--until=(.+)\z/ then opts[:until_at] = Tempest::DateFilter.parse(Regexp.last_match(1)); i += 1 when "--until" then opts[:until_at] = Tempest::DateFilter.parse(argv[i + 1]); i += 2 when /\A--format=(\S+)\z/ sym = Regexp.last_match(1).to_sym unless %i[line json raw].include?(sym) stderr.puts "error: invalid --format: #{Regexp.last_match(1).inspect}" return [nil, nil] end opts[:format] = sym i += 1 when "--no-color" Tempest::REPL::Formatter.color = false if defined?(Tempest::REPL::Formatter) i += 1 else positional << argv[i]; i += 1 end end [opts, positional] rescue ArgumentError => e stderr.puts "error: #{e.}" [nil, nil] end |