Improved error handling when attempting to download bad files.
This commit is contained in:
parent
34a24fad4e
commit
e47102ec8c
24
internet.lua
24
internet.lua
|
@ -46,11 +46,15 @@ local function safe_access (base, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function generic_request (...)
|
local function generic_request (...)
|
||||||
|
--print('Request', ...)
|
||||||
local output, code, headers, status = https.request(...)
|
local output, code, headers, status = https.request(...)
|
||||||
if status == nil and code ~= 'connection refused' then
|
--print('Https', output, code, headers, status)
|
||||||
|
if code ~= nil and status ~= 'connection refused' then
|
||||||
return output, code, headers, status
|
return output, code, headers, status
|
||||||
end
|
end
|
||||||
return http.request(...)
|
local output, code, headers, status = http.request(...)
|
||||||
|
--print('Http', output, code, headers, status)
|
||||||
|
return output, code, headers, status
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -262,14 +266,25 @@ end
|
||||||
-- Download file
|
-- Download file
|
||||||
|
|
||||||
function internet.download_file (url, filename)
|
function internet.download_file (url, filename)
|
||||||
|
assert(type(url) == 'string')
|
||||||
|
assert(type(filename) == 'string')
|
||||||
|
|
||||||
-- retrieve the content of a URL
|
-- retrieve the content of a URL
|
||||||
local body, code = https.request(url)
|
--local body, code, headers, status = generic_request(url)
|
||||||
if not body then error(('Https connection to "%s" failed, with error "%s"'):format(url, code)) end
|
local body, code, headers, status = https.request(url)
|
||||||
|
|
||||||
|
if code ~= 200 then
|
||||||
|
return false, code
|
||||||
|
--error(('Connection to "%s" failed, with error "%s"'):format(url, status))
|
||||||
|
end
|
||||||
|
assert(type(body) == 'string')
|
||||||
|
|
||||||
-- save the content to a file
|
-- save the content to a file
|
||||||
local f = assert(io.open(filename, 'wb')) -- open in "binary" mode
|
local f = assert(io.open(filename, 'wb')) -- open in "binary" mode
|
||||||
f:write(body)
|
f:write(body)
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function internet.download_video (url)
|
function internet.download_video (url)
|
||||||
|
@ -287,7 +302,6 @@ function internet.download_headers (url)
|
||||||
url = url,
|
url = url,
|
||||||
method = 'HEAD'
|
method = 'HEAD'
|
||||||
}
|
}
|
||||||
print(_, code, headers, status)
|
|
||||||
--
|
--
|
||||||
return headers
|
return headers
|
||||||
end
|
end
|
||||||
|
|
55
memes.lua
55
memes.lua
|
@ -244,24 +244,16 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font
|
||||||
assert(type(font_name) == 'string')
|
assert(type(font_name) == 'string')
|
||||||
-- Download and paste found image
|
-- Download and paste found image
|
||||||
if topic.type == 'image' then
|
if topic.type == 'image' then
|
||||||
local file_extension = topic.url:match '%.(%a+)$'
|
assert(type(topic.filename) == 'string')
|
||||||
local url, filename = topic.url, os.tmpname() --CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..file_extension
|
assert(not topic.url)
|
||||||
assert(type(url) == 'string' and #url > 0)
|
|
||||||
assert(type(filename) == 'string' and #filename > 0)
|
local found_img = assert(imlib.image.load(topic.filename))
|
||||||
internet.download_file(url, filename)
|
|
||||||
-- Convert svg to png
|
|
||||||
if url:match '%.svg$' then
|
|
||||||
local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png'
|
|
||||||
os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null')
|
|
||||||
filename = filename_2
|
|
||||||
end
|
|
||||||
--
|
|
||||||
local found_img = assert(imlib.image.load(filename))
|
|
||||||
found_img:crop_and_scale(0, 0, found_img:get_width(), found_img:get_height(), w, h)
|
found_img:crop_and_scale(0, 0, found_img:get_width(), found_img:get_height(), w, h)
|
||||||
flatten_onto (target, found_img, x, y)
|
flatten_onto (target, found_img, x, y)
|
||||||
found_img:free()
|
found_img:free()
|
||||||
--os.remove(filename)
|
--os.remove(filename)
|
||||||
elseif topic.type == 'text' then
|
elseif topic.type == 'text' then
|
||||||
|
assert(type(topic.text) == 'string')
|
||||||
local text = topic.text
|
local text = topic.text
|
||||||
draw_centered_text_in_box(font_name, target, text, x, y, w, h, bg_color, font_color)
|
draw_centered_text_in_box(font_name, target, text, x, y, w, h, bg_color, font_color)
|
||||||
elseif topic.type == 'droste' then
|
elseif topic.type == 'droste' then
|
||||||
|
@ -317,6 +309,24 @@ local DROSTE_EFFECT_TRIGGERS = {
|
||||||
['loop'] = true,
|
['loop'] = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local function download_and_standardize_image (image_url)
|
||||||
|
local file_extension = image_url:match '%.(%a+)$'
|
||||||
|
local url, filename = image_url, os.tmpname()
|
||||||
|
assert(type(url) == 'string' and #url > 0)
|
||||||
|
assert(type(filename) == 'string' and #filename > 0)
|
||||||
|
local success, errmsg = internet.download_file(url, filename)
|
||||||
|
|
||||||
|
-- Convert svg to png
|
||||||
|
if success and url:match '%.svg$' then
|
||||||
|
local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png'
|
||||||
|
os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null')
|
||||||
|
filename = filename_2
|
||||||
|
end
|
||||||
|
--
|
||||||
|
|
||||||
|
return success and filename, errmsg
|
||||||
|
end
|
||||||
|
|
||||||
local function fill_in_topics_information (topics)
|
local function fill_in_topics_information (topics)
|
||||||
assert(type(topics) == 'table')
|
assert(type(topics) == 'table')
|
||||||
--
|
--
|
||||||
|
@ -328,9 +338,13 @@ local function fill_in_topics_information (topics)
|
||||||
|
|
||||||
local url = topic_to_image_url[topic]
|
local url = topic_to_image_url[topic]
|
||||||
|
|
||||||
if DROSTE_EFFECT_TRIGGERS[topic] then new_topics[i] = { topic = topic, type = 'droste' }
|
if DROSTE_EFFECT_TRIGGERS[topic] then
|
||||||
elseif url then new_topics[i] = { topic = topic, type = 'image', url = url }
|
new_topics[i] = { topic = topic, type = 'droste' }
|
||||||
else new_topics[i] = { topic = topic, type = 'text', text = topic }
|
elseif url then
|
||||||
|
local filename = download_and_standardize_image(url)
|
||||||
|
new_topics[i] = { topic = topic, type = 'image', filename = filename }
|
||||||
|
else
|
||||||
|
new_topics[i] = { topic = topic, type = 'text', text = topic }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return new_topics
|
return new_topics
|
||||||
|
@ -610,12 +624,17 @@ function memes.generate_for_message (user, message)
|
||||||
|
|
||||||
-- Is this a rich picture?
|
-- Is this a rich picture?
|
||||||
if is_image_link(message) then
|
if is_image_link(message) then
|
||||||
local url = message
|
local filename, status = download_and_standardize_image(message)
|
||||||
|
if filename then
|
||||||
local img_link = generate_is_this_a_pidgeon {
|
local img_link = generate_is_this_a_pidgeon {
|
||||||
{ type = 'image', url = url },
|
{ type = 'image', filename = filename },
|
||||||
{ type = 'text', text = 'Is this a rich picture?' }
|
{ type = 'text', text = 'Is this a rich picture?' }
|
||||||
}
|
}
|
||||||
return img_link, 'KYNG'
|
return img_link, 'KYNG'
|
||||||
|
else
|
||||||
|
img_link = 'Kunne ikke skaffe det billede. Fik fejlkode '..tostring(status)
|
||||||
|
return img_link, '!KYNG'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Comparison memes
|
-- Comparison memes
|
||||||
|
|
Loading…
Reference in New Issue
Block a user