diff --git a/internet.lua b/internet.lua index faf46a4..e2bf8e9 100644 --- a/internet.lua +++ b/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 diff --git a/main.lua b/main.lua index a02aaa7..accce12 100644 --- a/main.lua +++ b/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 @@ -351,38 +353,50 @@ local function init_memebot () shutdown_memebot() os.exit(128 + signum) end) - --- - local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' } - local ORIG_TABLES = {} - for _, module_name in ipairs(RELOADABLE_LIBS) do + --- + local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' } + local ORIG_TABLES = {} + for _, module_name in ipairs(RELOADABLE_LIBS) do ORIG_TABLES[module_name] = require(module_name) - end - -- + end + -- signal.signal(signal.SIGUSR1, function (signum) io.write ' !! Received SIGUSR1, will reload modules...\n' for _, module_name in ipairs(RELOADABLE_LIBS) do - io.write(' - '..module_name) + io.write(' - '..module_name) package.loaded[module_name] = nil local new_module = require(module_name) - if type(new_module) == 'table' then - local old_module = ORIG_TABLES[module_name] - for k, v in pairs(new_module) do - old_module[k] = v - end - else + if type(new_module) == 'table' then + local old_module = ORIG_TABLES[module_name] + for k, v in pairs(new_module) do + old_module[k] = v + end + else io.write(' SKIP: Got '..tostring(new_module)..' but expected table.') - end + end io.write '\n' end 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 diff --git a/memes.lua b/memes.lua index f22c003..4559270 100644 --- a/memes.lua +++ b/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