Added pulling memes from subreddits

This commit is contained in:
Jon Michael Aanes 2018-08-08 16:23:52 +02:00
parent 13a505c975
commit b1121a94fa
3 changed files with 82 additions and 16 deletions

View File

@ -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

View File

@ -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
@ -377,12 +379,24 @@ local function init_memebot ()
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

View File

@ -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