Important shutterstock update

This commit is contained in:
Jon Michael Aanes 2019-12-05 18:21:51 +01:00
parent 6f5ab8c167
commit a082f7ebdf
5 changed files with 52 additions and 6 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

View File

@ -60,8 +60,8 @@ end
local function report_https_request_error (status, code) local function report_https_request_error (status, code)
local f = io.stdout local f = io.stdout
f:write 'Error when attempting request:\n' f:write 'Error when attempting request:\n'
f:write (' Status: '..status..'\n') f:write (' Status: '..tostring(status)..'\n')
f:write (' Code: '..code..'\n') f:write (' Code: '..tostring(code)..'\n')
--f:write (' Headers:\n ') --f:write (' Headers:\n ')
end end
@ -83,6 +83,32 @@ local function search_clearbit_for_logo (topic)
end end
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 -- Searching splashbase for fairly-licensed stockphotoes
@ -268,6 +294,7 @@ function internet.search_images (topics)
-- Logoes -- Logoes
if not val then val = search_clearbit_for_logo(topic:lower()) end if not val then val = search_clearbit_for_logo(topic:lower()) end
-- Stock Photoes -- 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 if not val then val = search_splashbase_for_stock_photoes(topic:lower()) end
topic_to_image_url[topic] = val topic_to_image_url[topic] = val

View File

@ -107,6 +107,18 @@ local function common_error(channel, text, ...)
error(errmsg) error(errmsg)
end 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_USER_LOGIN
local LAST_TIME_USER_LOGGED_OUT = {} local LAST_TIME_USER_LOGGED_OUT = {}
local IRC_ALLOWED_TIMEOUT = 20 --20 * 60 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. -- And that user haven't been logged in for IRC_ALLOWED_TIMEOUT seconds.
LAST_USER_LOGIN = user.nick LAST_USER_LOGIN = user.nick
human_delay() human_delay()
bot:sendChat(channel, choose(MESSAGES.OTHER_HELLO):format(user.nick)) send_response(channel, MESSAGES.OTHER_HELLO)
io.write '[HILS]\n' io.write '[HILS]\n'
end end
end) end)
@ -169,8 +181,7 @@ local function handle_message(bot, user, channel, message, is_fast_channel)
end end
for match_str, responses in pairs(MESSAGES.DIRECT_MSG_RESPONSES) do for match_str, responses in pairs(MESSAGES.DIRECT_MSG_RESPONSES) do
if message:match(match_str) then if message:match(match_str) then
bot:sendChat(channel, choose(responses)) return send_response(channel, responses)
return '!BOT'
end end
end end
end end
@ -246,7 +257,7 @@ bot:hook("OnChat", function(user, channel, message)
-- Handle error -- Handle error
if not success then if not success then
if is_fast_channel == 'direct' or is_fast_channel == 'mention' 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 end
status, message = 'ERROR', ('%s\n\n\t%s\n\n'):format(message, status) status, message = 'ERROR', ('%s\n\n\t%s\n\n'):format(message, status)
end end

View File

@ -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) copy_remotely('localhost', filename, CONFIG.STORAGE_SERVER, CONFIG.STORAGE_SERVER_PATH..remote_name)
return CONFIG.STORAGE_DIR_URL..remote_name return CONFIG.STORAGE_DIR_URL..remote_name
end end
memes.save_file_to_dcav = save_file_to_dcav
local function save_img (img) local function save_img (img)
assert(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() local filename = os.tmpname() .. '.' .. img:get_format()
img:save(filename) img:save(filename)
img:free() img:free()

View File

@ -25,6 +25,7 @@ MESSAGES.ERROR_OCCURED = {
'Hvad sagde du?', 'Hvad sagde du?',
'Nej, bare nej.', 'Nej, bare nej.',
'Hvad siger du?', 'Hvad siger du?',
{ type = 'image', path = './images/det_kan_jeg_desværre_ikke.png' },
} }
MESSAGES.OTHER_HELLO = { MESSAGES.OTHER_HELLO = {
@ -66,6 +67,7 @@ MESSAGES.ATTEMPT_SHUTDOWN = {
'Du er ikke min far!', 'Du er ikke min far!',
'Du er hverken min mor, min far, min mormor, min morfar, min farmor eller min farfar!', '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.', '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' },
} }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------