Skip to content

Instantly share code, notes, and snippets.

@akkartik
Last active February 17, 2023 06:06
Show Gist options
  • Save akkartik/29e4ece114212403be6f22cb297b77ad to your computer and use it in GitHub Desktop.
Save akkartik/29e4ece114212403be6f22cb297b77ad to your computer and use it in GitHub Desktop.
https example in Lua
-- https://github.com/rxi/json.lua
json = require 'json'
-- https://github.com/brunoos/luasec
https = require 'ssl.https'
-- Construct an URL to query the Mastodon API.
-- I have no idea if ActivityPub carves out the id space between federating
-- hosts (how would it even do that?!) so I'm going to remember two things for
-- each Mastodon link: a host and an id. The host includes the protocol.
function url_from_ml(ml)
return ('%s/api/v1/statuses/%d'):format(ml.host, ml.id)
end
-- We can parse the Mastodon link from the following formats:
-- https://merveilles.town/users/bd/statuses/109791902220118251
-- https://merveilles.town/@bd/109791902220118251
function ml_from_url(url)
local host, user, id = url:match('^([^/]*://[^/]*)/@([^/]*)/(%d*)$')
if host then return {host=host, user=user, id=id} end
local host, user, id = url:match('^([^/]*://[^/]*)/users/([^/]*)/statuses/(%d*)$')
if host then return {host=host, user=user, id=id} end
end
-- Given a mastodon link, return all information about it.
function get(ml)
local url = url_from_ml(ml)
local response, code, response_headers, status = https.request(url)
local basic_info = json.decode(response)
local response, code, response_headers, status = https.request(url..'/context')
local rels = json.decode(response)
return {host=ml.host, user=ml.user, id=ml.id, url=basic_info.url, content=basic_info.content, replies_count=basic_info.replies_count, ancestors=rels.ancestors, descendants=rels.descendants}
end
posts = {}
to_process = {}
if #arg == 0 then
io.stderr:write('provide a Mastodon URL on the command line\n')
os.exit(1)
end
local result = get(ml_from_url(arg[1]))
if result.ancestors then
result = get(ml_from_url(result.ancestors[1].url))
end
local filename = os.tmpname()
local f = io.open(filename, 'w')
f:write(json.encode(result))
f:close()
print(filename)
--? os.execute('love . '..filename)
--? os.remove(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment