The-Navigators-Blog/ssb_import.rb

55 lines
1.7 KiB
Ruby

require "pry"
require "open-uri"
require "base64"
require "erb"
require "json"
USER_ID = "@z2M8msI2EUubNHnrEJncglDIy2/SUd+36jCyJnfeiHk=.ed25519"
BLOB_URL = "http://localhost:8989/blobs/get/%s?contentType=%s"
BLOB_PATH = "source/img/ssb/%s.%s"
POST_TEMPLATE = ERB.new(File.read("ssb_import.erb.markdown"))
txt = `ssb-server createHistoryStream --id #{USER_ID} | jq 'select(.value?.content?.type? == "post")' -c -M`
posts = []
txt.each_line do |x|
json = JSON.parse(x, symbolize_names: true)
if json.dig(:value, :content, :reply) == nil
t = Time.at(json.dig(:value, :timestamp) / 1000)
posts.push({
sequence: json.dig(:value, :sequence),
time: t.iso8601,
key: Base64.urlsafe_encode64(Base64.decode64(json.fetch(:key))),
text: (json.dig(:value, :content, :text) || ""),
links: json.dig(:value, :content, :mentions) || [],
})
end
end
posts.map do |post|
key = post.fetch(:key)
content = post[:text]
time = post[:time]
sequence = post[:sequence]
post[:links].map do |link|
hash = link[:link]
case hash[0]
when "@" # Just link usernames to the SSB Site
content.gsub!(hash, "https://scuttlebutt.nz/")
when "&"
b64 = Base64.urlsafe_encode64(Base64.decode64(hash))
type = link[:type]
url = BLOB_URL % [hash, type]
extension = type.split("/").last
path = BLOB_PATH % [b64, extension]
unless File.file?(path)
File.write(path, open(url).read)
`convert #{path} +dither -remap netscape: #{path}`
`convert #{path} -resize 640 #{path}`
end
content.gsub!(hash, path.gsub("source/", "http://tilde.town/~netscape_navigator/"))
end
end
File.write("source/blog/ssb-#{key}.html.markdown", POST_TEMPLATE.result(binding))
end