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)
|
||||
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)
|
||||
if not body then error(code) end
|
||||
local data = json.decode(body)
|
||||
|
@ -262,6 +262,37 @@ function internet.search_images (topics)
|
|||
return topic_to_image_url
|
||||
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
|
||||
|
||||
|
|
16
main.lua
16
main.lua
|
@ -23,6 +23,8 @@ if CONFIG.LUA_EXTRA_CPATH then package.cpath = package.cpath .. CONFIG.LUA_EXTR
|
|||
local FARVEL_INTERVAL = 120
|
||||
local MEME_INTERVAL = 30
|
||||
local MEME_CHANCE = 0.3
|
||||
local MEME_DELAY, MEME_CHECKING_INTERVAL = 4*60*60, 30*60
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Make sure all required modules can be loaded
|
||||
|
@ -377,12 +379,24 @@ local function init_memebot ()
|
|||
io.write ' Done\n'
|
||||
end)
|
||||
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
|
||||
|
||||
local NEXT_TIME_TO_CHECK_FOR_MEMES = 0
|
||||
|
||||
-- Think loop
|
||||
while BOT_SHOULD_CONTINUE_RUNNING do
|
||||
-- Bot thinking
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
|
21
memes.lua
21
memes.lua
|
@ -760,6 +760,27 @@ function memes.generate_for_message (user, message)
|
|||
return img_link, 'COMPAR'
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user