Added config file

This commit is contained in:
jmaa 2018-06-08 12:52:21 +02:00
parent b176092c67
commit 4dfd68b597
3 changed files with 73 additions and 16 deletions

3
.gitignore vendored
View File

@ -1,4 +1,7 @@
# "Secure" files
config.lua
# Output files # Output files
/images/output /images/output

27
config.example.lua Normal file
View File

@ -0,0 +1,27 @@
return {
-- IRC connection
IRC_SERVER = 'some-domain.com',
IRC_PORT = 6697,
IRC_SECURE = true, -- If using SSL
IRC_CHANNELS = { '#bot-test' },
-- Image generation
IMGGEN_PATH_BRAINS = './images/brains/',
IMGGEN_PATH_OUTPUT = './images/output/',
IMGGEN_PATH_FONTS = {
'/usr/share/fonts/TTF',
'/usr/share/fonts/truetype/dejavu',
'/usr/local/share/fonts/TTF',
},
-- Image storage and presentation
STORAGE_SERVER = 'image-host.com',
STORAGE_SERVER_PATH = '~someuser/images/',
STORAGE_DIR_URL = 'https://image-host.com/myimages/',
-- Lua stuff
LUA_EXTRA_PATH = nil, -- Set a string here if you are using libraries located
LUA_EXTRA_CPATH = nil, -- weird location.
}

View File

@ -3,17 +3,19 @@
math.randomseed(os.time()) math.randomseed(os.time())
local BRAIN_PATH = './images/brains/' local CONFIG = require 'config'
local OUTPUT_PATH = './images/output/'
local MEME_OUTPUT = OUTPUT_PATH..'meme.png' if CONFIG.LUA_EXTRA_PATH then package.path = package.path .. CONFIG.LUA_EXTRA_PATH end
local SERVER_DIR = '/var/shots/b/' if CONFIG.LUA_EXTRA_CPATH then package.cpath = package.cpath .. CONFIG.LUA_EXTRA_CPATH end
local IMAGE_URL_DIR = 'https://dcav.pw/b'
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Meme utils -- Meme utils
local imlib = require 'imlib2' local imlib = require 'imlib2'
imlib.font.add_path '/usr/share/fonts/TTF'
for _, path in ipairs(CONFIG.IMGGEN_PATH_FONTS) do
imlib.font.add_path(path)
end
local function flatten_onto(target_img, other_img, x0, y0) local function flatten_onto(target_img, other_img, x0, y0)
assert(x0 + other_img:get_width() <= target_img:get_width()) assert(x0 + other_img:get_width() <= target_img:get_width())
@ -144,12 +146,13 @@ local SCANDI_SYMBOLS = { 'æ', 'Æ', 'ø', 'Ø', 'å', 'Å' }
local function should_look_for_images (topic) local function should_look_for_images (topic)
assert(type(topic) == 'string')
if math.random() < CHANCE_OF_GUARENTEED_IMAGE_SEARCH then return true end if math.random() < CHANCE_OF_GUARENTEED_IMAGE_SEARCH then return true end
-- --
if #topic < 2 then return false end if #topic < 2 then return false end
-- --
for _, symbol in ipairs(SCANDI_SYMBOLS) do for _, symbol in ipairs(SCANDI_SYMBOLS) do
if topic:match(SCANDI_SYMBOLS) then return false end if topic:match(symbol) then return false end
end end
-- --
return math.random() < IMAGE_CHANCE return math.random() < IMAGE_CHANCE
@ -182,7 +185,7 @@ 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 url, filename = topic.url, OUTPUT_PATH..'topic_'..topic.topic..'.png' local url, filename = topic.url, CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.png'
download_file(url, filename) download_file(url, filename)
local found_img = imlib.image.load(filename) local found_img = 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)
@ -197,15 +200,22 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font
end end
end end
local function copy_remotely (origin_server, origin_path, target_server, target_path)
local origin = origin_server and origin_server ~= 'localhost' and origin_server..':'..origin_path or origin_path
local target = target_server and target_server ~= 'localhost' and target_server..':'..target_path or target_path
os.execute('scp '..origin..' '..target..' > /dev/null')
end
local function save_to_cloud (img) local function save_to_cloud (img)
assert(img) assert(img)
-- --
local MEME_OUTPUT = CONFIG.IMGGEN_PATH_OUTPUT..'meme.png'
img:save (MEME_OUTPUT) img:save (MEME_OUTPUT)
img:free() img:free()
-- Upload to dcav -- Upload to dcav
local img_name = 'otmemes_'..os.time() local img_name = 'otmemes_'..os.time()
os.execute('scp '..MEME_OUTPUT..' guava:'..SERVER_DIR..img_name..'.png > /dev/null') copy_remotely('localhost', MEME_OUTPUT, CONFIG.STORAGE_SERVER, CONFIG.STORAGE_SERVER_PATH..img_name..'.png')
return IMAGE_URL_DIR..img_name return CONFIG.STORAGE_DIR_URL..img_name
end end
local function generate_distracted_boyfriend (topics) local function generate_distracted_boyfriend (topics)
@ -237,7 +247,7 @@ local function generate_brain_explosion_image (topics)
for i, topic_info in ipairs(topics) do for i, topic_info in ipairs(topics) do
paste_topic_onto_image(base_img, topic_info, 0, (i-1) * ROW_HEIGHT, ROW_WIDTH, ROW_HEIGHT, COLOR_WHITE, font_name) paste_topic_onto_image(base_img, topic_info, 0, (i-1) * ROW_HEIGHT, ROW_WIDTH, ROW_HEIGHT, COLOR_WHITE, font_name)
local brain_img = imlib.image.load(BRAIN_PATH..'brain_'..i..'.png') local brain_img = imlib.image.load(CONFIG.IMGGEN_PATH_BRAINS..'brain_'..i..'.png')
brain_img:crop_and_scale(0, 0, brain_img:get_width(), brain_img:get_height(), ROW_WIDTH, ROW_HEIGHT) brain_img:crop_and_scale(0, 0, brain_img:get_width(), brain_img:get_height(), ROW_WIDTH, ROW_HEIGHT)
flatten_onto (base_img, brain_img, ROW_WIDTH, (i-1) * ROW_HEIGHT) flatten_onto (base_img, brain_img, ROW_WIDTH, (i-1) * ROW_HEIGHT)
brain_img:free() brain_img:free()
@ -345,13 +355,15 @@ local ERROR_MSG = {
'Jeg har ingen hjerne og må tænke.', 'Jeg har ingen hjerne og må tænke.',
'Hvad siger du?', 'Hvad siger du?',
'Ser jeg ud til at have otte arme? Nej, for jeg har ikke nogle arme!', 'Ser jeg ud til at have otte arme? Nej, for jeg har ikke nogle arme!',
'Do androids dream of electric sheep?',
} }
bot:hook('OnJoin', function(user, channel) bot:hook('OnJoin', function(user, channel)
if user.nick == BOT_NICK then if user.nick == BOT_NICK then
-- On self join -- On self join
io.write(string.format(' !! Joined %s\n', channel))
if #imlib.font.list_fonts() == 0 then if #imlib.font.list_fonts() == 0 then
common_error(channel, 'Hjælp! Min computer har ingen fonts! Hvorfor er fonts så svære på arch?') common_error(channel, 'Hjælp! Min computer har ingen fonts! Hvorfor er fonts så svære på linux? Jeg har kigget i: "'..table.concat(imlib.font.list_paths(), '", "')..'"')
end end
else else
-- On other join -- On other join
@ -452,17 +464,32 @@ end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Main run -- Main run
local function init_memebot ()
bot:connect { bot:connect {
host = 'irc.guava.space', host = CONFIG.IRC_SERVER,
port = 6697, port = CONFIG.IRC_PORT,
secure = true, secure = CONFIG.IRC_SECURE,
} }
bot:join '#bot-test'
io.write ('Memebot has started up as "'..BOT_NICK..'"\n') io.write ('Memebot has started up as "'..BOT_NICK..'"\n')
assert(CONFIG.IRC_CHANNELS == nil or type(CONFIG.IRC_CHANNELS) == 'table')
for _, channel in ipairs(CONFIG.IRC_CHANNELS or {}) do
bot:join(channel)
end
while true do while true do
bot:think() bot:think()
sleep(0.5) sleep(0.5)
end end
end
--------------------------------------------------------------------------------
if ... then
error 'Cannot be loaded like library'
else
init_memebot()
end