From 149c46cf5181e99061968914bbdb6e34673936c6 Mon Sep 17 00:00:00 2001 From: scuti Date: Sat, 7 Dec 2024 20:06:47 -0800 Subject: [PATCH] can fetch from auth-required instance --- pleroma-comments.lua | 65 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/pleroma-comments.lua b/pleroma-comments.lua index 716d7bf..a9560a8 100644 --- a/pleroma-comments.lua +++ b/pleroma-comments.lua @@ -54,34 +54,73 @@ function tokenizeString(inputString, delimiter) return tokens end +-- function get(link) +-- print("http/s GET: ".. link) +-- local args = {link} +-- +-- if pleroma_auth_token then +-- local auth = string.format( +-- "\'Authorization: Bearer %s\' ", pleroma_auth_token) +-- table.insert(args, 1, "-H") +-- table.insert(args, 2, auth) +-- end +-- +-- local command = "curl " .. table.concat(args, ' ') +-- print(command) +-- local success, retval = pcall( +-- function () +-- return pandoc.pipe("curl", args, "") +-- end +-- ) +-- if not success then +-- print("warning: error while performing http/s GET") +-- print("\treturned: " .. tostring(retval)) +-- end +-- return retval +-- end + function get(link, filename) print("http/s GET: ".. link) local filename = filename or nil local args = {} - if filename then + if filename then -- when requesting avatars args = { "--timeout=10", "-qO", filename, link } - else + else -- when requesting json args = { "-qO-", "--timeout=10", link } end + -- don't use auth bearer for downloading images' + if pleroma_auth_token and not filename then + local h = "--header=\"Authorization: Bearer %s\" " + table.insert( + args, 1, string.format(h, pleroma_auth_token) + ) + end + local command = "wget " .. table.concat(args, ' ') + print(command) local success, retval = pcall( function () --- print("!: ".. table.concat(args)) - return pandoc.pipe("wget", args, "") + local handle = io.popen(command) + local result = handle:read("*a") + handle:close() + return result + + -- couldn't get pandoc.pipe() send headers' +-- return pandoc.pipe("wget", args, "") end ) if not success then print("warning: error while performing http/s GET") - print(retval) + print("\treturned: " .. tostring(retval)) end return retval end @@ -330,8 +369,19 @@ end function get_status(host, post_id) local url = "https://" .. host .. "/api/v1/statuses/" .. post_id --- print(url) - return json.decode(get(url)) + local success, retval = pcall( + function () + local got = get(url) +-- print(got) + return json.decode(got) + end + ) + -- if an error occurred in retrieving a status + -- it will cause errors in write_comments + --consider skipping over statuses later + assert(success) + assert(not retval["error"]) + return retval end function get_replies(host, id) @@ -382,7 +432,6 @@ function Meta(meta) -- if pleroma_avatar_save_path and pleroma_avatar_path then -- is_hotlink = false -- end - local all_replies = {} local hrefs = {} local host = ""