Skip to content

Instantly share code, notes, and snippets.

@vividtone
Created June 22, 2017 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vividtone/45785d0bf36358071549134119eeb990 to your computer and use it in GitHub Desktop.
Save vividtone/45785d0bf36358071549134119eeb990 to your computer and use it in GitHub Desktop.
Converts a toot of Mastodon to HTML
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
require 'time'
toot_url = ARGV[0]
toot_url =~ %r|^https://(.+?)/@(.*?)/(\d+)$|
host = $1
username = $2
status_id = $3
if host.nil? || username.nil? || status_id.nil?
STDERR.puts "usage: #{$0} https://mastodon.example.com/@username/status_id"
exit 1
end
json = open("https://#{host}/api/v1/statuses/#{status_id}").read
status = JSON.parse(json)
avatar_static = status['account']['avatar_static']
created_at = Time.parse(status['created_at']).localtime.strftime('%Y-%m-%d %H:%M %Z')
account_url = status['account']['url']
display_name = status['account']['display_name']
content = status['content']
html = <<EOT
<div style="padding:16px 8px;overflow:hidden;border-top:1px solid #888;">
<img src="#{avatar_static}" style="float:left;margin-right:8px;width:48px;height:48px;">
<div style="font-size: 90%; float:right;color:#888;">
<a style="color:#888; font-weight: bold;" href="#{toot_url}" target="_blank">#{created_at}</a>
</div>
<span style="font-size: 90%; font-weight: bold;"><a style="color: #888;" href="#{account_url}" target="_blank">#{display_name}</a></span>
<span style="font-size: 90%; color:#888;">@#{username}</span>
#{content}
</div>
EOT
puts html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment