mirror of
https://notabug.org/scuti/pleroma-comments
synced 2025-03-12 12:09:12 +05:30
avatar downloads working
This commit is contained in:
parent
3efcb65455
commit
4ab3993fa8
@ -36,11 +36,12 @@ function tokenizeString(inputString, delimiter)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function get(link, filename)
|
function get(link, filename)
|
||||||
print(link)
|
print("http/s GET: ".. link)
|
||||||
local filename = filename or nil
|
local filename = filename or nil
|
||||||
local args = {}
|
local args = {}
|
||||||
if filename then
|
if filename then
|
||||||
args = {
|
args = {
|
||||||
|
"--timeout=10",
|
||||||
"-qO",
|
"-qO",
|
||||||
filename,
|
filename,
|
||||||
link
|
link
|
||||||
@ -48,10 +49,10 @@ function get(link, filename)
|
|||||||
else
|
else
|
||||||
args = {
|
args = {
|
||||||
"-qO-",
|
"-qO-",
|
||||||
|
"--timeout=10",
|
||||||
link
|
link
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local command = "wget " .. table.concat(args, ' ')
|
local command = "wget " .. table.concat(args, ' ')
|
||||||
local success, retval = pcall(
|
local success, retval = pcall(
|
||||||
function ()
|
function ()
|
||||||
@ -86,14 +87,17 @@ function get_short_date(timestamp)
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function write_comments(pleroma_posts, instance, show_avatars)
|
function write_comments(pleroma_posts, instance, avatar_path)
|
||||||
show_avatars = show_avatars or false
|
local avatar_mode = 1
|
||||||
|
if avatar_path then
|
||||||
|
avatar_mode = 2
|
||||||
|
end
|
||||||
|
|
||||||
-- img mode : 0 = omit; 1 = display (hotlink); 2 = download
|
-- img mode : 0 = omit; 1 = display (hotlink); 2 = download
|
||||||
function get_user(acct_data, instance, img_mode, folder)
|
function get_user(acct_data, instance, img_mode, folder)
|
||||||
-- specify path to store avatars
|
-- specify path to store avatars
|
||||||
-- should be empty string if img_mode != 2
|
-- should be empty string if img_mode != 2
|
||||||
local folder = folderh or ""
|
local folder = folder or ""
|
||||||
|
|
||||||
-- related to output
|
-- related to output
|
||||||
local user_info = "" -- template
|
local user_info = "" -- template
|
||||||
@ -105,6 +109,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
|||||||
handle = acct_data["acct"],
|
handle = acct_data["acct"],
|
||||||
host=instance
|
host=instance
|
||||||
}
|
}
|
||||||
|
local filename = nil
|
||||||
local avatar_url = acct_data["avatar_static"]
|
local avatar_url = acct_data["avatar_static"]
|
||||||
if img_mode then
|
if img_mode then
|
||||||
user_info = [[
|
user_info = [[
|
||||||
@ -121,8 +126,8 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
|||||||
local extension = (avatar_url:match("([^?]+)")):match("^.+(%..+)$")
|
local extension = (avatar_url:match("([^?]+)")):match("^.+(%..+)$")
|
||||||
-- replace '@'s and '.'
|
-- replace '@'s and '.'
|
||||||
local name = (acct_data["acct"]:gsub("@", "_at_")):gsub("%.", "_")
|
local name = (acct_data["acct"]:gsub("@", "_at_")):gsub("%.", "_")
|
||||||
local filename = folder .. name .. extension
|
filename = name .. extension
|
||||||
vars.avatar = filename
|
vars.avatar = folder .. filename
|
||||||
end
|
end
|
||||||
result = user_info:gsub("%$(%w+)%$", vars)
|
result = user_info:gsub("%$(%w+)%$", vars)
|
||||||
else
|
else
|
||||||
@ -130,7 +135,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
|||||||
result = user_info:gsub("%$(%w+)%$", vars)
|
result = user_info:gsub("%$(%w+)%$", vars)
|
||||||
end
|
end
|
||||||
-- print("vars: " .. vars.avatar)
|
-- print("vars: " .. vars.avatar)
|
||||||
return result, vars.avatar, avatar_url
|
return result, filename, avatar_url
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_card(card, instance)
|
function get_card(card, instance)
|
||||||
@ -259,7 +264,7 @@ 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(
|
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(images, img_file)
|
||||||
add_unique(links, img_url)
|
add_unique(links, img_url)
|
||||||
|
|
||||||
@ -317,7 +322,10 @@ function get_replies(host, id)
|
|||||||
return got["descendants"]
|
return got["descendants"]
|
||||||
end
|
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
|
if not filenames or not urls then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -325,16 +333,32 @@ function get_images(filenames, urls)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
for i = 1, #urls, 1 do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
function Meta(meta)
|
function Meta(meta)
|
||||||
local pleroma_urls = meta["pleroma-urls"]
|
local pleroma_urls = meta["pleroma-urls"]
|
||||||
-- print(elem)
|
|
||||||
if pleroma_urls == nil then
|
if pleroma_urls == nil then
|
||||||
return -- abort
|
return -- abort
|
||||||
end
|
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 all_replies = {}
|
||||||
local hrefs = {}
|
local hrefs = {}
|
||||||
local host = ""
|
local host = ""
|
||||||
@ -357,10 +381,15 @@ function Meta(meta)
|
|||||||
return ta > tb
|
return ta > tb
|
||||||
end
|
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(i)
|
||||||
printTable(l)
|
printTable(l)
|
||||||
get_images(i, l)
|
get_images(i, l, "test/images/avatars/")
|
||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user