Added pulling memes from subreddits
This commit is contained in:
parent
13a505c975
commit
b1121a94fa
33
internet.lua
33
internet.lua
|
@ -85,7 +85,7 @@ local function search_splashbase_for_stock_photoes (topic)
|
||||||
return nil, 'Splashbase does not like æøå: '..tostring(topic)
|
return nil, 'Splashbase does not like æøå: '..tostring(topic)
|
||||||
end
|
end
|
||||||
|
|
||||||
local search_url = string.format('http://www.splashbase.co/api/v1/images/search?query=%s', topic:gsub('%s', '%%20'))
|
local search_url = escape_url('http://www.splashbase.co/api/v1/images/search?query='..topic)
|
||||||
local body, code, headers, status = https.request(search_url)
|
local body, code, headers, status = https.request(search_url)
|
||||||
if not body then error(code) end
|
if not body then error(code) end
|
||||||
local data = json.decode(body)
|
local data = json.decode(body)
|
||||||
|
@ -262,6 +262,37 @@ function internet.search_images (topics)
|
||||||
return topic_to_image_url
|
return topic_to_image_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Find images on reddit
|
||||||
|
|
||||||
|
function internet.find_reddit_memes (subreddit, filter)
|
||||||
|
|
||||||
|
-- Error check
|
||||||
|
assert(type(subreddit) == 'string')
|
||||||
|
filter = filter or function() return true end
|
||||||
|
assert(type(filter) == 'function')
|
||||||
|
|
||||||
|
--
|
||||||
|
local search_url = escape_url('https://www.reddit.com/r/'..subreddit..'/new.json')
|
||||||
|
local body, code, headers, status = https.request(search_url)
|
||||||
|
if not body then error(code) end
|
||||||
|
|
||||||
|
local data = json.decode(body)
|
||||||
|
print(require'pretty'(data))
|
||||||
|
|
||||||
|
local memes = {}
|
||||||
|
for _, meme_data in pairs(data.data.children) do
|
||||||
|
meme_data = meme_data.data
|
||||||
|
local success = filter(meme_data)
|
||||||
|
print(meme_data.title, meme_data.score, meme_data.created, success)
|
||||||
|
if success then
|
||||||
|
memes[#memes+1] = meme_data
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return memes
|
||||||
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Download file
|
-- Download file
|
||||||
|
|
||||||
|
|
44
main.lua
44
main.lua
|
@ -23,6 +23,8 @@ if CONFIG.LUA_EXTRA_CPATH then package.cpath = package.cpath .. CONFIG.LUA_EXTR
|
||||||
local FARVEL_INTERVAL = 120
|
local FARVEL_INTERVAL = 120
|
||||||
local MEME_INTERVAL = 30
|
local MEME_INTERVAL = 30
|
||||||
local MEME_CHANCE = 0.3
|
local MEME_CHANCE = 0.3
|
||||||
|
local MEME_DELAY, MEME_CHECKING_INTERVAL = 4*60*60, 30*60
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Make sure all required modules can be loaded
|
-- Make sure all required modules can be loaded
|
||||||
|
@ -351,38 +353,50 @@ local function init_memebot ()
|
||||||
shutdown_memebot()
|
shutdown_memebot()
|
||||||
os.exit(128 + signum)
|
os.exit(128 + signum)
|
||||||
end)
|
end)
|
||||||
---
|
---
|
||||||
local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' }
|
local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' }
|
||||||
local ORIG_TABLES = {}
|
local ORIG_TABLES = {}
|
||||||
for _, module_name in ipairs(RELOADABLE_LIBS) do
|
for _, module_name in ipairs(RELOADABLE_LIBS) do
|
||||||
ORIG_TABLES[module_name] = require(module_name)
|
ORIG_TABLES[module_name] = require(module_name)
|
||||||
end
|
end
|
||||||
--
|
--
|
||||||
signal.signal(signal.SIGUSR1, function (signum)
|
signal.signal(signal.SIGUSR1, function (signum)
|
||||||
io.write ' !! Received SIGUSR1, will reload modules...\n'
|
io.write ' !! Received SIGUSR1, will reload modules...\n'
|
||||||
for _, module_name in ipairs(RELOADABLE_LIBS) do
|
for _, module_name in ipairs(RELOADABLE_LIBS) do
|
||||||
io.write(' - '..module_name)
|
io.write(' - '..module_name)
|
||||||
package.loaded[module_name] = nil
|
package.loaded[module_name] = nil
|
||||||
local new_module = require(module_name)
|
local new_module = require(module_name)
|
||||||
if type(new_module) == 'table' then
|
if type(new_module) == 'table' then
|
||||||
local old_module = ORIG_TABLES[module_name]
|
local old_module = ORIG_TABLES[module_name]
|
||||||
for k, v in pairs(new_module) do
|
for k, v in pairs(new_module) do
|
||||||
old_module[k] = v
|
old_module[k] = v
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
io.write(' SKIP: Got '..tostring(new_module)..' but expected table.')
|
io.write(' SKIP: Got '..tostring(new_module)..' but expected table.')
|
||||||
end
|
end
|
||||||
io.write '\n'
|
io.write '\n'
|
||||||
end
|
end
|
||||||
io.write ' Done\n'
|
io.write ' Done\n'
|
||||||
end)
|
end)
|
||||||
else
|
else
|
||||||
io.write ' !! Module "posix.signal" was missing, so CTRL+C will hard-crash.\n'
|
io.write ' !! Module "posix.signal" was missing; CTRL+C will hard-crash.\n'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local NEXT_TIME_TO_CHECK_FOR_MEMES = 0
|
||||||
|
|
||||||
-- Think loop
|
-- Think loop
|
||||||
while BOT_SHOULD_CONTINUE_RUNNING do
|
while BOT_SHOULD_CONTINUE_RUNNING do
|
||||||
|
-- Bot thinking
|
||||||
bot:think()
|
bot:think()
|
||||||
|
|
||||||
|
-- Attempt to post memes
|
||||||
|
if NEXT_TIME_TO_CHECK_FOR_MEMES < os.time() then
|
||||||
|
NEXT_TIME_TO_CHECK_FOR_MEMES = os.time() + MEME_CHECKING_INTERVAL
|
||||||
|
local meme_msg = memes.generate_meme_report({'dankmark'}, { os.time() - MEME_DELAY - MEME_CHECKING_INTERVAL, os.time() - MEME_DELAY })
|
||||||
|
if meme_msg then send_to_all(meme_msg) end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Sleeping
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
21
memes.lua
21
memes.lua
|
@ -760,6 +760,27 @@ function memes.generate_for_message (user, message)
|
||||||
return img_link, 'COMPAR'
|
return img_link, 'COMPAR'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Find memes from dankmark
|
||||||
|
|
||||||
|
function memes.generate_meme_report (subreddits, time_interval)
|
||||||
|
assert(type(subreddits) == 'table' and #subreddits == 1)
|
||||||
|
assert(type(time_interval) == 'table' and #time_interval == 2)
|
||||||
|
|
||||||
|
local current_check_time = os.time()
|
||||||
|
local new_memes = internet.find_reddit_memes(subreddits[1], function(t) return 35 <= t.score and time_interval[1] < t.created and t.created <= time_interval[2] end)
|
||||||
|
if #new_memes == 0 then return end
|
||||||
|
|
||||||
|
-- Find best meme
|
||||||
|
local best_meme = new_memes[1]
|
||||||
|
for i = 2, #new_memes do
|
||||||
|
if best_meme.score < new_memes[i].score then best_meme = new_memes[i] end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Return message to IRC
|
||||||
|
return string.format('Fugtig migmig fra %s: %s: %s', subreddits[1], best_meme.title, best_meme.url)
|
||||||
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
return memes
|
return memes
|
||||||
|
|
Loading…
Reference in New Issue
Block a user