48 lines
1.5 KiB
Ruby
48 lines
1.5 KiB
Ruby
|
require "pry"
|
||
|
require "open-uri"
|
||
|
require "base64"
|
||
|
require "erb"
|
||
|
|
||
|
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(1556651174818 / 1000)
|
||
|
posts.push({
|
||
|
time: t.strftime("%Y-%M-%d %H:%M %z"),
|
||
|
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]
|
||
|
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]
|
||
|
File.write(path, open(url).read) unless File.file?(path)
|
||
|
content.gsub!(hash, path.gsub("source/", "/"))
|
||
|
end
|
||
|
end
|
||
|
File.write("source/blog/ssb-#{key}.html.markdown", POST_TEMPLATE.result(binding))
|
||
|
end
|