diff --git a/images/det_kan_jeg_desværre_ikke.png b/images/det_kan_jeg_desværre_ikke.png new file mode 100644 index 0000000..c10da68 Binary files /dev/null and b/images/det_kan_jeg_desværre_ikke.png differ diff --git a/internet.lua b/internet.lua index 3330892..178644d 100644 --- a/internet.lua +++ b/internet.lua @@ -60,8 +60,8 @@ end local function report_https_request_error (status, code) local f = io.stdout f:write 'Error when attempting request:\n' - f:write (' Status: '..status..'\n') - f:write (' Code: '..code..'\n') + f:write (' Status: '..tostring(status)..'\n') + f:write (' Code: '..tostring(code)..'\n') --f:write (' Headers:\n ') end @@ -83,6 +83,32 @@ local function search_clearbit_for_logo (topic) end end +-------------------------------------------------------------------------------- +-- Searching Shutterstock for stockphotoes + +local htmlparser = require 'htmlparser' + +local function search_shutterstock_for_stock_photoes (topic) + if not (type(topic) == 'string' and topic == topic:lower() and #topic > 0) then + return nil, 'Bad topic: '..tostring(topic) + elseif string_contains_scandi(topic) then + return nil, 'Splashbase does not like æøå: '..tostring(topic) + end + + local search_url = 'https://www.shutterstock.com/search/'..escape_url(topic) + local body, code, headers, status = https.request(search_url) + if not body then error(code) end + + local html = htmlparser.parse(body, 10000) + + if not html then return nil, 'HTML could not decode data for '..topic end + + local img_elems = html:select 'img.z_g_i' + local img_url = img_elems[math.random(#img_elems)].attributes.src + assert(type(img_url) == 'string') + return img_url +end + -------------------------------------------------------------------------------- -- Searching splashbase for fairly-licensed stockphotoes @@ -268,6 +294,7 @@ function internet.search_images (topics) -- Logoes if not val then val = search_clearbit_for_logo(topic:lower()) end -- Stock Photoes + if not val then val = search_shutterstock_for_stock_photoes(topic) end if not val then val = search_splashbase_for_stock_photoes(topic:lower()) end topic_to_image_url[topic] = val diff --git a/main.lua b/main.lua index 07ed506..a6b0e79 100644 --- a/main.lua +++ b/main.lua @@ -107,6 +107,18 @@ local function common_error(channel, text, ...) error(errmsg) end +local function send_response(channel, responses) + local response = choose(responses) + if type(response) == 'string' then + bot:sendChat(channel, response) + return '!BOT' + end + assert(type(response) == 'table' and response.type == 'image' and type(response.path) == 'string') + local url = memes.save_file_to_dcav(response.path) + bot:sendChat(channel, url) + return '!BOT' +end + local LAST_USER_LOGIN local LAST_TIME_USER_LOGGED_OUT = {} local IRC_ALLOWED_TIMEOUT = 20 --20 * 60 @@ -133,7 +145,7 @@ bot:hook('OnJoin', function(user, channel) -- And that user haven't been logged in for IRC_ALLOWED_TIMEOUT seconds. LAST_USER_LOGIN = user.nick human_delay() - bot:sendChat(channel, choose(MESSAGES.OTHER_HELLO):format(user.nick)) + send_response(channel, MESSAGES.OTHER_HELLO) io.write '[HILS]\n' end end) @@ -169,8 +181,7 @@ local function handle_message(bot, user, channel, message, is_fast_channel) end for match_str, responses in pairs(MESSAGES.DIRECT_MSG_RESPONSES) do if message:match(match_str) then - bot:sendChat(channel, choose(responses)) - return '!BOT' + return send_response(channel, responses) end end end @@ -246,7 +257,7 @@ bot:hook("OnChat", function(user, channel, message) -- Handle error if not success then if is_fast_channel == 'direct' or is_fast_channel == 'mention' then - bot:sendChat(channel, choose(MESSAGES.ERROR_OCCURED)) + send_response(channel, MESSAGES.ERROR_OCCURED) end status, message = 'ERROR', ('%s\n\n\t%s\n\n'):format(message, status) end diff --git a/memes.lua b/memes.lua index 3e9323b..7bbd5ac 100644 --- a/memes.lua +++ b/memes.lua @@ -67,9 +67,15 @@ local function save_file_to_dcav (filename, ext) copy_remotely('localhost', filename, CONFIG.STORAGE_SERVER, CONFIG.STORAGE_SERVER_PATH..remote_name) return CONFIG.STORAGE_DIR_URL..remote_name end +memes.save_file_to_dcav = save_file_to_dcav local function save_img (img) assert(img) + -- Set format if not yet set + if img:get_format() == nil then + img:set_format 'png' + end + -- local filename = os.tmpname() .. '.' .. img:get_format() img:save(filename) img:free() diff --git a/misc-messages.lua b/misc-messages.lua index ce698e9..9c5b69c 100644 --- a/misc-messages.lua +++ b/misc-messages.lua @@ -25,6 +25,7 @@ MESSAGES.ERROR_OCCURED = { 'Hvad sagde du?', 'Nej, bare nej.', 'Hvad siger du?', + { type = 'image', path = './images/det_kan_jeg_desværre_ikke.png' }, } MESSAGES.OTHER_HELLO = { @@ -66,6 +67,7 @@ MESSAGES.ATTEMPT_SHUTDOWN = { 'Du er ikke min far!', 'Du er hverken min mor, min far, min mormor, min morfar, min farmor eller min farfar!', 'Tag og fuck af; du er for grim for mig og din mor er med på den værste.', + { type = 'image', path = './images/det_kan_jeg_desværre_ikke.png' }, } --------------------------------------------------------------------------------