working - retrieves avatars - needs folder paths

This commit is contained in:
scuti 2024-12-05 00:31:30 -08:00
parent c4fb5e1b7b
commit 3efcb65455

View File

@ -5,11 +5,28 @@ local json = require("cjson")
-- for testing -- for testing
function printTable(t) function printTable(t)
for key, value in pairs(t) do if t then
print(key, value) for key, value in pairs(t) do
print(key, value)
end
end end
end end
function add_unique(list, item)
if type(list) ~= "table" or item == nil then
-- print("item "..item)
return false
end
for _, value in ipairs(list) do
if value == item then
return false
end
end
table.insert(list, item)
-- print("added "..item)
return true
end
function tokenizeString(inputString, delimiter) function tokenizeString(inputString, delimiter)
local tokens = {} local tokens = {}
for token in inputString:gmatch("[^" .. delimiter .. "]+") do for token in inputString:gmatch("[^" .. delimiter .. "]+") do
@ -18,15 +35,35 @@ function tokenizeString(inputString, delimiter)
return tokens return tokens
end end
function get(link) function get(link, filename)
local args = { print(link)
"-qO-", local filename = filename or nil
link local args = {}
} if filename then
local data = pandoc.pipe("wget", args, "") args = {
local parsed = json.decode(data) "-qO",
-- print(link) filename,
return parsed link
}
else
args = {
"-qO-",
link
}
end
local command = "wget " .. table.concat(args, ' ')
local success, retval = pcall(
function ()
-- print("!: ".. table.concat(args))
return pandoc.pipe("wget", args, "")
end
)
if not success then
print("warning: error while performing http/s GET")
print(retval)
end
return retval
end end
function get_epoch_time(timestamp) function get_epoch_time(timestamp)
@ -52,30 +89,48 @@ end
function write_comments(pleroma_posts, instance, show_avatars) function write_comments(pleroma_posts, instance, show_avatars)
show_avatars = show_avatars or false show_avatars = show_avatars or false
function get_user(acct_data, instance, include_img) -- img mode : 0 = omit; 1 = display (hotlink); 2 = download
-- user data function get_user(acct_data, instance, img_mode, folder)
local user_info = "" -- specify path to store avatars
-- should be empty string if img_mode != 2
local folder = folderh or ""
-- related to output
local user_info = "" -- template
local result = "" local result = ""
local vars = { local vars = {
alias = acct_data["display_name"], alias = acct_data["display_name"],
uid = acct_data["id"], uid = acct_data["id"],
handle = acct_data["acct"], handle = acct_data["acct"],
host=instance host=instance
} }
if include_img then local avatar_url = acct_data["avatar_static"]
if img_mode then
user_info = [[ user_info = [[
<figure> <figure>
<img src="$avatar$" loading="lazy" alt="avatar"/> <img src="$avatar$" loading="lazy" alt="avatar"/>
<figcaption>$alias$ <a href="$host$/users/$uid$">@$handle$</a> </figcaption> <figcaption>$alias$ <a href="$host$/users/$uid$">@$handle$</a> </figcaption>
</figure> </figure>
]] ]]
vars.avatar = acct_data["avatar_static"] if img_mode == 1 then
vars.avatar = acct_data["avatar_static"]
else
-- save to file such as user_at_instance_tld.png
-- omit query e.g ?name="foo" - get the extension only
local extension = (avatar_url:match("([^?]+)")):match("^.+(%..+)$")
-- replace '@'s and '.'
local name = (acct_data["acct"]:gsub("@", "_at_")):gsub("%.", "_")
local filename = folder .. name .. extension
vars.avatar = filename
end
result = user_info:gsub("%$(%w+)%$", vars) result = user_info:gsub("%$(%w+)%$", vars)
else else
user_info = "<p>$alias$ <a href=\"$host$/users/$uid$\">@$handle$</a></p>" user_info = "<p>$a`lias$ <a href=\"$host$/users/$uid$\">@$handle$</a></p>"
result = user_info:gsub("%$(%w+)%$", vars) result = user_info:gsub("%$(%w+)%$", vars)
end end
return result -- print("vars: " .. vars.avatar)
return result, vars.avatar, avatar_url
end end
function get_card(card, instance) function get_card(card, instance)
@ -124,7 +179,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
mime = v["pleroma"]["mime_type"] mime = v["pleroma"]["mime_type"]
} }
local foo = item:gsub("%$(%w+)%$", vars) local foo = item:gsub("%$(%w+)%$", vars)
print(foo) -- print(foo)
table.insert(media_list, foo) table.insert(media_list, foo)
end end
table.insert(media_list, "</ol>") table.insert(media_list, "</ol>")
@ -188,6 +243,10 @@ function write_comments(pleroma_posts, instance, show_avatars)
]] ]]
local comments = {} local comments = {}
local replies = pleroma_posts-- ["descendants"] local replies = pleroma_posts-- ["descendants"]
local links = {}
local images = {}
for i, post in ipairs(replies) do for i, post in ipairs(replies) do
local pid = post["id"] local pid = post["id"]
local datetime = get_short_date(post["created_at"]) local datetime = get_short_date(post["created_at"])
@ -199,12 +258,17 @@ function write_comments(pleroma_posts, instance, show_avatars)
) )
table.insert(attrs, get_poll(post["poll"])) table.insert(attrs, get_poll(post["poll"]))
local user, img_file, img_url = get_user(
post["account"], instance, 2, "images/avatars/")
add_unique(images, img_file)
add_unique(links, img_url)
local interpolated = template:gsub("%$(%w+)%$", { local interpolated = template:gsub("%$(%w+)%$", {
i= #replies - i + 1, i= #replies - i + 1,
host=instance, host=instance,
pid=pid, pid=pid,
datetime=datetime, datetime=datetime,
user=get_user(post["account"], instance, true), user=user,
text = text, text = text,
card = get_card(post["card"], instance), card = get_card(post["card"], instance),
attributes = table.concat(attrs) attributes = table.concat(attrs)
@ -214,8 +278,8 @@ function write_comments(pleroma_posts, instance, show_avatars)
comments, pandoc.RawBlock("html", interpolated) comments, pandoc.RawBlock("html", interpolated)
) )
end end
-- print(comments) -- printTable(dl_list)
return comments return comments, images, links
end end
function combine_tables(a,b) function combine_tables(a,b)
@ -242,17 +306,29 @@ end
function get_status(host, post_id) function get_status(host, post_id)
local url = "https://" .. host .. "/api/v1/statuses/" .. post_id local url = "https://" .. host .. "/api/v1/statuses/" .. post_id
print(url) -- print(url)
return get(url) return json.decode(get(url))
end end
function get_replies(host, id) function get_replies(host, id)
local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context"
print(url) -- print(url)
local got = get(url) local got = json.decode(get(url))
return got["descendants"] return got["descendants"]
end end
function get_images(filenames, urls)
if not filenames or not urls then
return
end
if #filenames ~= #urls then
return
end
for i = 1, #urls, 1 do
get(urls[i], filenames[i])
end
end
function Meta(meta) function Meta(meta)
local pleroma_urls = meta["pleroma-urls"] local pleroma_urls = meta["pleroma-urls"]
-- print(elem) -- print(elem)
@ -281,7 +357,10 @@ function Meta(meta)
return ta > tb return ta > tb
end end
) )
local c = write_comments(all_replies, "https://" .. host) local c, i, l = write_comments(all_replies, "https://" .. host)
printTable(i)
printTable(l)
get_images(i, l)
meta["pleroma-comments"] = c meta["pleroma-comments"] = c
meta["pleroma-comments-count"] = #c meta["pleroma-comments-count"] = #c
meta["pleroma-has-comments"] = (#c > 0) meta["pleroma-has-comments"] = (#c > 0)