#!/usr/bin/env ruby require "json" require "open-uri" FEED_URL = "https://bookmarks.davideisinger.com/u:dce.json" index = (ARGV.pop || 1).to_i content = [] links = [] refs = [] item_content = ->(item) do title = "[#{item["title"]}][#{index}]" if item["referrer"] title += " ([via][#{index + 1}])" end <<~OUT * #{title} > #{item["description"].tr("\n", " ").squeeze(" ")} OUT end item_link = ->(item) do "[#{index}]: #{item["url"]}" end add_item = ->(item) do ref = `bin/archive "#{item["url"]}"`.gsub("references:\n", "") content << item_content[item] links << item_link[item] refs << ref index += 1 if item["referrer"] ref = `bin/archive "#{item["referrer"]}"`.gsub("references:\n", "") links << "[#{index}]: #{item["referrer"]}" refs << ref index += 1 end end result = -> do <<~OUT --- #{content.join("\n")} #{links.join("\n")} --- references: #{refs.join} OUT end process_item = ->(item) do puts <<~OUT #{item_content[item]} #{item_link[item]} OUT print "Include? (y/n/a): " case gets.chomp when "y" add_item[item] when "n" nil when "a" puts result.call abort else process_item[item] end end URI.parse(FEED_URL).open do |json| feed = JSON.parse(json.read) feed.each do |item| process_item[item] end puts result.call end