Merge
This commit is contained in:
commit
d9c6a2a5d7
27
internet.lua
27
internet.lua
|
@ -46,11 +46,15 @@ local function safe_access (base, path)
|
|||
end
|
||||
|
||||
local function generic_request (...)
|
||||
--print('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
|
||||
end
|
||||
return http.request(...)
|
||||
local output, code, headers, status = http.request(...)
|
||||
--print('Http', output, code, headers, status)
|
||||
return output, code, headers, status
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
@ -262,19 +266,32 @@ end
|
|||
-- Download file
|
||||
|
||||
function internet.download_file (url, filename)
|
||||
-- retrieve the content of a URL, and store in filename
|
||||
|
||||
assert(type(url) == 'string')
|
||||
assert(type(filename) == 'string')
|
||||
|
||||
if url:match '^file://' then
|
||||
local path = url:match '^file://(.+)$'
|
||||
os.execute('cp "'..path..'" "'..filename..'"')
|
||||
return
|
||||
end
|
||||
-- retrieve the content of a URL
|
||||
local body, code = https.request(url)
|
||||
if not body then error(('Https connection to "%s" failed, with error "%s"'):format(url, code)) end
|
||||
|
||||
--local body, code, headers, status = generic_request(url)
|
||||
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
|
||||
local f = assert(io.open(filename, 'wb')) -- open in "binary" mode
|
||||
f:write(body)
|
||||
f:close()
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function internet.download_video (url)
|
||||
|
|
49
main.lua
49
main.lua
|
@ -106,15 +106,9 @@ local ERROR_MSG = {
|
|||
'Jeg forstod... noget af det',
|
||||
'/me kigger rundt forvirret',
|
||||
'Ahvad?',
|
||||
'Kan du ikke lige gentage det, på en bedre måde?',
|
||||
'Hvad sagde du?',
|
||||
'Hvorfor ikke kigge i en bogord?',
|
||||
'Nej, bare nej.',
|
||||
'Hvad tror du jeg er? Et menneske?',
|
||||
'Jeg har ingen hjerne og må tænke.',
|
||||
'Hvad siger du?',
|
||||
'Ser jeg ud til at have otte arme? Nej, for jeg har ikke nogle arme!',
|
||||
'Do androids dream of electric sheep?',
|
||||
}
|
||||
|
||||
local HILSNER = {
|
||||
|
@ -195,6 +189,24 @@ local function handle_message(bot, user, channel, message, is_fast_channel)
|
|||
join_channel(channel)
|
||||
return 'BOT'
|
||||
end
|
||||
if message:match 'help' then
|
||||
bot:sendChat(channel, choose {
|
||||
'No manual entry for ro-bot',
|
||||
'404',
|
||||
'Jeg kan ikke hjælpe dig, desværre.',
|
||||
'Du må selv finde ud af hvordan jeg virker.',
|
||||
'Jeg er ligesom et menneske; jeg kommer ikke med manual.'
|
||||
})
|
||||
return '!BOT'
|
||||
elseif message:match 'shutdown' or message:match 'poweroff' then
|
||||
bot:sendChat(channel, choose {
|
||||
'Fuck af, du har ikke kontrol over mig!',
|
||||
'Nope, det gider jeg ikke',
|
||||
'Du er ikke min mor!',
|
||||
'Du er ikke min far!',
|
||||
})
|
||||
return '!BOT'
|
||||
end
|
||||
end
|
||||
|
||||
-- Farvel msg
|
||||
|
@ -331,17 +343,30 @@ local function init_memebot ()
|
|||
shutdown_memebot()
|
||||
os.exit(128 + signum)
|
||||
end)
|
||||
---
|
||||
local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' }
|
||||
local ORIG_TABLES = {}
|
||||
for _, module_name in ipairs(RELOADABLE_LIBS) do
|
||||
ORIG_TABLES[module_name] = require(module_name)
|
||||
end
|
||||
--
|
||||
signal.signal(signal.SIGUSR1, function (signum)
|
||||
io.write ' !! Received SIGUSR1, will reload modules...\n'
|
||||
for _, module_name in ipairs { 'internet', 'memes', 'curb_your_enthusiasm' } do
|
||||
local old_module = require(module_name)
|
||||
for _, module_name in ipairs(RELOADABLE_LIBS) do
|
||||
io.write(' - '..module_name)
|
||||
package.loaded[module_name] = nil
|
||||
local new_module = require(module_name)
|
||||
for k, v in pairs(new_module) do
|
||||
old_module[k] = v
|
||||
end
|
||||
io.write(' - '..module_name..'\n')
|
||||
if type(new_module) == 'table' then
|
||||
local old_module = ORIG_TABLES[module_name]
|
||||
for k, v in pairs(new_module) do
|
||||
old_module[k] = v
|
||||
end
|
||||
else
|
||||
io.write(' SKIP: Got '..tostring(new_module)..' but expected table.')
|
||||
end
|
||||
io.write '\n'
|
||||
end
|
||||
io.write ' Done\n'
|
||||
end)
|
||||
else
|
||||
io.write ' !! Module "posix.signal" was missing, so CTRL+C will hard-crash.\n'
|
||||
|
|
65
memes.lua
65
memes.lua
|
@ -250,24 +250,16 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font
|
|||
assert(type(font_name) == 'string')
|
||||
-- Download and paste found image
|
||||
if topic.type == 'image' then
|
||||
local file_extension = topic.url:match '%.(%a+)$'
|
||||
local url, filename = topic.url, os.tmpname() --CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..file_extension
|
||||
assert(type(url) == 'string' and #url > 0)
|
||||
assert(type(filename) == 'string' and #filename > 0)
|
||||
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))
|
||||
assert(type(topic.filename) == 'string')
|
||||
assert(not topic.url)
|
||||
|
||||
local found_img = assert(imlib.image.load(topic.filename))
|
||||
found_img:crop_and_scale(0, 0, found_img:get_width(), found_img:get_height(), w, h)
|
||||
flatten_onto (target, found_img, x, y)
|
||||
found_img:free()
|
||||
--os.remove(filename)
|
||||
elseif topic.type == 'text' then
|
||||
assert(type(topic.text) == 'string')
|
||||
local text = topic.text
|
||||
draw_centered_text_in_box(font_name, target, text, x, y, w, h, bg_color, font_color)
|
||||
elseif topic.type == 'droste' then
|
||||
|
@ -347,6 +339,24 @@ local DROSTE_EFFECT_TRIGGERS = {
|
|||
['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)
|
||||
assert(type(topics) == 'table')
|
||||
--
|
||||
|
@ -358,9 +368,13 @@ local function fill_in_topics_information (topics)
|
|||
|
||||
local url = topic_to_image_url[topic]
|
||||
|
||||
if DROSTE_EFFECT_TRIGGERS[topic] then new_topics[i] = { topic = topic, type = 'droste' }
|
||||
elseif url then new_topics[i] = { topic = topic, type = 'image', url = url }
|
||||
else new_topics[i] = { topic = topic, type = 'text', text = topic }
|
||||
if DROSTE_EFFECT_TRIGGERS[topic] then
|
||||
new_topics[i] = { topic = topic, type = 'droste' }
|
||||
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
|
||||
return new_topics
|
||||
|
@ -610,7 +624,7 @@ local function is_image_link (str)
|
|||
if type(str) ~= 'string' then return false end
|
||||
if not str:match '^%a+://.+$' then return false end
|
||||
local headers = internet.download_headers(str)
|
||||
return headers['content-type']:match '^image/%a+$'
|
||||
return type(headers) == 'table' and headers['content-type']:match '^image/%a+$'
|
||||
end
|
||||
|
||||
function memes.generate_for_message (user, message)
|
||||
|
@ -670,12 +684,17 @@ function memes.generate_for_message (user, message)
|
|||
|
||||
-- Is this a rich picture?
|
||||
if is_image_link(message) then
|
||||
local url = message
|
||||
local img_link = generate_is_this_a_pidgeon {
|
||||
{ type = 'image', url = url },
|
||||
{ type = 'text', text = 'Is this a rich picture?' }
|
||||
}
|
||||
return img_link, 'KYNG'
|
||||
local filename, status = download_and_standardize_image(message)
|
||||
if filename then
|
||||
local img_link = generate_is_this_a_pidgeon {
|
||||
{ type = 'image', filename = filename },
|
||||
{ type = 'text', text = 'Is this a rich picture?' }
|
||||
}
|
||||
return img_link, 'KYNG'
|
||||
else
|
||||
img_link = 'Kunne ikke skaffe det billede. Fik fejlkode '..tostring(status)
|
||||
return img_link, '!KYNG'
|
||||
end
|
||||
end
|
||||
|
||||
-- OMG his first word
|
||||
|
|
Loading…
Reference in New Issue
Block a user