Tilføjet 'omg his first word'-meme og en enkelt modifier.

This commit is contained in:
Jon Michael Aanes 2018-06-23 09:34:28 +02:00
parent 4d14c52252
commit 78b2d04eb4
3 changed files with 84 additions and 15 deletions

BIN
images/his_first_words.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

View File

@ -193,7 +193,6 @@ local function search_wikidata_for_image (topic, language)
local topic_to_image_url = topic_to_image_url or {}
-- Download and parse
--print("Searching "..site.." wikidata for images")
local body, code, headers, status = https.request(WIKIDATA_API_URL:format(site, escape_url(topic)))
if not body then error(code) end
local data = json.decode(body)
@ -208,9 +207,10 @@ local function search_wikidata_for_image (topic, language)
local entity = data.entities[entity_key]
-- Determine if hit disambiguation entity
if is_disambiguation_entity(entity) then
if is_disambiguation_entity (entity) then
local wikipedia_page = get_wikipedia_pages({topic}, language)
local links = get_disambiguation_links(wikipedia_page[topic])
if #links <= 0 then return nil, 'Ramte flertydig '..language..' wikipedia side for "'..topic..'", men kunne ikke finde nogle links!' end
assert(#links > 0)
return search_wikidata_for_image(links[math.random(#links)], language)
end
@ -262,6 +262,11 @@ end
-- Download file
function internet.download_file (url, filename)
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
@ -287,7 +292,6 @@ function internet.download_headers (url)
url = url,
method = 'HEAD'
}
print(_, code, headers, status)
--
return headers
end

View File

@ -56,21 +56,27 @@ local function copy_remotely (origin_server, origin_path, target_server, target_
end
end
local function save_file_to_dcav (filename)
local function save_file_to_dcav (filename, ext)
assert(type(filename) == 'string')
-- Upload to dcav
local ext = filename:match '%.(%a+)$'
local ext = ext or filename:match '%.(%a+)$'
local remote_name = 'otmemes_'..os.time()..'.'..ext
copy_remotely('localhost', filename, CONFIG.STORAGE_SERVER, CONFIG.STORAGE_SERVER_PATH..remote_name)
return CONFIG.STORAGE_DIR_URL..remote_name
end
local function save_img (img)
assert(img)
local filename = os.tmpname() .. '.' .. img:get_format()
img:save(filename)
img:free()
return filename
end
local function save_img_to_cloud (img)
assert(img)
--
local filename = CONFIG.IMGGEN_PATH_OUTPUT..'meme.png'
img:save (filename)
img:free()
local filename = save_img(img)
return save_file_to_dcav(filename)
end
@ -290,11 +296,35 @@ local function draw_droste_effect (target_image, droste_positions, iterations)
return target_image
end
--------------------------------------------------------------------------------
-- Meme modifiers
local function add_compression_artifacts_to_image (image_filename, quality)
-- Recommend quality < 10. Repeat runs at very low quality will
-- not further destroy the image.
assert(type(image_filename) == 'string')
assert(type(quality) == 'number' and quality == math.floor(quality) and quality > 0 and quality < 100)
--
local output_filename = os.tmpname()..'.jpg'
local img = imlib.image.load(image_filename)
img:save(output_filename)
img:free()
-- Mogrify to low quality
os.execute('mogrify -quality '..quality..' "'..output_filename..'"')
--
return output_filename
end
local CHANCE_OF_MODIFIER = 0.3
local modifiers = {
-- Compression artifacts
function (image_filename) return add_compression_artifacts_to_image(image_filename, math.random(1, 20)) end,
}
--------------------------------------------------------------------------------
-- Comparison memes
local DROSTE_EFFECT_TRIGGERS = {
['induktion'] = true,
['rekursion'] = true,
@ -360,7 +390,7 @@ local function generate_comparison_meme_generator (positions)
local font_name = choose_random_font()
local base_img = imlib.image.load(positions.base_img_path)
local base_img = assert(imlib.image.load(positions.base_img_path))
-- Randomize pos
local rand_positions = {}
@ -372,20 +402,30 @@ local function generate_comparison_meme_generator (positions)
-- Paste topic onto head
for index, pos in ipairs(rand_positions) do
-- Font colors
local font_colors = {}
if pos.font_color == COLOR_WHITE then font_colors[1] = COLOR_BLACK
elseif pos.font_color == COLOR_BLACK then font_colors[1] = COLOR_WHITE
end
table.insert(font_colors, pos.font_color)
-- Paste
assert(base_img, topics[index])
paste_topic_onto_image(base_img, topics[index], pos.x, pos.y, pos.w, pos.h, nil, font_name, font_colors)
end
-- Droste
base_img = draw_droste_effect(base_img, droste_positions_from_topics(topics, rand_positions))
--
return save_img_to_cloud(base_img)
-- Save img
local image_filename = save_img(base_img)
-- Use modifiers
if math.random() < CHANCE_OF_MODIFIER then
image_filename = choose(modifiers)(image_filename)
end
return save_file_to_dcav(image_filename)
end
end
@ -429,6 +469,26 @@ local generate_is_this_a_pidgeon = generate_comparison_meme_generator {
{ x = 15, xr = 0, y = 1200, yr = 0, w = 1587 - 15, h = 1443-1200-15, font_color = COLOR_WHITE },
}
local generate_omg_his_first_word do
local internal = generate_comparison_meme_generator {
base_img_path = './images/his_first_words.png',
{ x = 20, xr = 0, y = 10, yr = 0, w = 325, h = 83, font_color = COLOR_BLACK, type = 'text' },
{ x = 371, xr = 0, y = 10, yr = 0, w = 316, h = 100, font_color = COLOR_BLACK, type = 'text' },
{ x = 29, xr = 0, y = 374, yr = 0, w = 455, h = 155, font_color = COLOR_BLACK, type = 'text' },
}
generate_omg_his_first_word = function (text)
assert(type(text) == 'string')
local short = text:sub(1,1)..'...'..text:sub(1,2)..'...'
return internal {
{ type = 'text', text = short },
{ type = 'text', text = 'Åh gud! Hans første ord!' },
{ type = 'text', text = text }
}
end
end
local GENERATE_COMPARISON_MEME_OF_2 = {
generate_distracted_boyfriend,
generate_distracted_boyfriend_oldy_times,
@ -543,7 +603,6 @@ local curb = require 'curb_your_enthusiasm'
--------------------------------------------------------------------------------
local function is_image_link (str)
print(str)
if str:match '%.png$' or str:match '%.gif$' or str:match '%.jpg$' then
return true
end
@ -619,6 +678,12 @@ function memes.generate_for_message (user, message)
return img_link, 'KYNG'
end
-- OMG his first word
if math.random() < 0.01 then
local img_link = generate_omg_his_first_word(message)
return img_link, 'OMG'
end
-- Comparison memes
local topics = get_topics_from_comparison_message(message)
if not topics then return end