Template of a custom feed generator service for the Bluesky network in Ruby
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

added dry run mode to rebuild_feed

+37 -9
+37 -9
Rakefile
··· 76 76 task :rebuild_feed do 77 77 feed = get_feed 78 78 method = ENV['UNSAFE'] ? :tap : :transaction 79 + dry = ENV['DRY_RUN'] ? true : false 79 80 80 81 ActiveRecord::Base.send(method) do 81 82 if ENV['ONLY_EXISTING'] ··· 88 89 89 90 feed_posts.each do |fp| 90 91 if !feed.post_matches?(fp.post) 91 - puts "Deleting from feed: ##{fp.post.id} \"#{fp.post.text}\"" 92 - fp.destroy 92 + if dry 93 + puts "Post would be deleted: ##{fp.post.id} \"#{fp.post.text}\"" 94 + else 95 + puts "Deleting from feed: ##{fp.post.id} \"#{fp.post.text}\"" 96 + fp.destroy 97 + end 93 98 deleted += 1 94 99 end 95 100 end 96 101 97 - puts "Done (#{deleted} post(s) deleted)." 102 + if dry 103 + puts "#{deleted} post(s) would be deleted." 104 + else 105 + puts "Done (#{deleted} post(s) deleted)." 106 + end 98 107 else 99 108 days = ENV['DAYS'] ? ENV['DAYS'].to_i : 7 100 109 101 110 posts = Post.order('time, id') 102 - start = posts.where("time <= DATETIME('now', '-#{days} day')").last 111 + start = posts.where("time <= DATETIME('now', '-#{days} days')").last 103 112 stop = posts.last 104 113 first = posts.first 105 114 total = start ? (stop.id - start.id + 1) : (stop.id - first.id + 1) 106 115 107 116 if ENV['APPEND_ONLY'] 108 117 current_post_ids = FeedPost.where(feed_id: feed.feed_id).pluck('post_id') 109 - else 118 + elsif !dry 110 119 print "This will erase and replace the contents of the feed. Continue? [y/n]: " 111 120 answer = STDIN.readline 112 121 exit unless answer.strip.downcase == 'y' ··· 118 127 119 128 offset = 0 120 129 page = 100000 130 + matched_posts = [] 121 131 122 132 loop do 123 133 batch = if start ··· 129 139 break if batch.empty? 130 140 131 141 batch.each_with_index do |post, i| 132 - print "Processing posts... [#{offset + i + 1}/#{total}]\r" 133 - $stdout.flush 142 + $stderr.print "Processing posts... [#{offset + i + 1}/#{total}]\r" 143 + $stderr.flush 134 144 135 145 if !current_post_ids.include?(post.id) && feed.post_matches?(post) 136 - FeedPost.create!(feed_id: feed.feed_id, post: post, time: post.time) 146 + if dry 147 + matched_posts << post 148 + else 149 + FeedPost.create!(feed_id: feed.feed_id, post: post, time: post.time) 150 + end 137 151 end 138 152 end 139 153 ··· 141 155 start = batch.last 142 156 end 143 157 144 - puts "Processing posts... Done." + " " * 30 158 + $stderr.puts "Processing posts... Done." + " " * 30 159 + 160 + if dry 161 + if ENV['APPEND_ONLY'] 162 + puts "Added posts:" 163 + puts "==============================" 164 + puts 165 + end 166 + 167 + Signal.trap("SIGPIPE", "SYSTEM_DEFAULT") 168 + printer = PostConsolePrinter.new(feed) 169 + matched_posts.each do |p| 170 + printer.display(p) 171 + end 172 + end 145 173 end 146 174 end 147 175 end