avatar downloads working

This commit is contained in:
scuti 2024-12-05 16:50:00 -08:00
parent 3efcb65455
commit 4ab3993fa8

View File

@ -36,11 +36,12 @@ function tokenizeString(inputString, delimiter)
end
function get(link, filename)
print(link)
print("http/s GET: ".. link)
local filename = filename or nil
local args = {}
if filename then
args = {
"--timeout=10",
"-qO",
filename,
link
@ -48,10 +49,10 @@ function get(link, filename)
else
args = {
"-qO-",
"--timeout=10",
link
}
end
local command = "wget " .. table.concat(args, ' ')
local success, retval = pcall(
function ()
@ -86,14 +87,17 @@ function get_short_date(timestamp)
)
end
function write_comments(pleroma_posts, instance, show_avatars)
show_avatars = show_avatars or false
function write_comments(pleroma_posts, instance, avatar_path)
local avatar_mode = 1
if avatar_path then
avatar_mode = 2
end
-- img mode : 0 = omit; 1 = display (hotlink); 2 = download
function get_user(acct_data, instance, img_mode, folder)
-- specify path to store avatars
-- should be empty string if img_mode != 2
local folder = folderh or ""
local folder = folder or ""
-- related to output
local user_info = "" -- template
@ -105,6 +109,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
handle = acct_data["acct"],
host=instance
}
local filename = nil
local avatar_url = acct_data["avatar_static"]
if img_mode then
user_info = [[
@ -121,8 +126,8 @@ function write_comments(pleroma_posts, instance, show_avatars)
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
filename = name .. extension
vars.avatar = folder .. filename
end
result = user_info:gsub("%$(%w+)%$", vars)
else
@ -130,7 +135,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
result = user_info:gsub("%$(%w+)%$", vars)
end
-- print("vars: " .. vars.avatar)
return result, vars.avatar, avatar_url
return result, filename, avatar_url
end
function get_card(card, instance)
@ -259,7 +264,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
table.insert(attrs, get_poll(post["poll"]))
local user, img_file, img_url = get_user(
post["account"], instance, 2, "images/avatars/")
post["account"], instance, avatar_mode, avatar_path)
add_unique(images, img_file)
add_unique(links, img_url)
@ -317,7 +322,10 @@ function get_replies(host, id)
return got["descendants"]
end
function get_images(filenames, urls)
function get_images(filenames, urls, folder)
if not folder then
folder = ""
end
if not filenames or not urls then
return
end
@ -325,16 +333,32 @@ function get_images(filenames, urls)
return
end
for i = 1, #urls, 1 do
get(urls[i], filenames[i])
-- still possible to have a ilst of nil (file names)
if not filenames[i] then
break
end
get(urls[i], folder .. filenames[i])
end
end
function Meta(meta)
local pleroma_urls = meta["pleroma-urls"]
-- print(elem)
if pleroma_urls == nil then
return -- abort
end
-- most servers appear to serve hotilnked avatars however
-- images will be missing in case of downtime or shutdown
local is_hotlink = true
-- where to save downloaded avatars
local pleroma_avatar_save_path = meta["pleroma-avatar-save-path"]
-- (page) path written on the page-
local pleroma_avatar_path = meta["pleroma-avatar-path"]
-- if both are defined, then do not hotlink avatars
if pleroma_avatar_save_path and pleroma_avatar_path then
is_hotlink = false
end
local all_replies = {}
local hrefs = {}
local host = ""
@ -357,10 +381,15 @@ function Meta(meta)
return ta > tb
end
)
local c, i, l = write_comments(all_replies, "https://" .. host)
-- returns comments, images, links (img urls)
local c, i, l = write_comments(
all_replies,
"https://" .. host,
"images/avatars/"
)
printTable(i)
printTable(l)
get_images(i, l)
get_images(i, l, "test/images/avatars/")
meta["pleroma-comments"] = c
meta["pleroma-comments-count"] = #c
meta["pleroma-has-comments"] = (#c > 0)