2016-04-11 06:04:47 +02:00
|
|
|
local reddit = {}
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
|
|
|
local URL = require('socket.url')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
|
|
|
reddit.command = 'reddit [r/subreddit | query]'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function reddit:init(config)
|
|
|
|
reddit.triggers = utilities.triggers(self.info.username, config.cmd_pat, {'^/r/'}):t('reddit', true):t('r', true):t('r/', true).table
|
|
|
|
reddit.doc = [[```
|
|
|
|
]]..config.cmd_pat..[[reddit [r/subreddit | query]
|
2016-05-25 15:01:54 +02:00
|
|
|
Returns the top posts or results for a given subreddit or query. If no argument is given, returns the top posts from r/all. Querying specific subreddits is not supported.
|
2016-05-27 05:28:44 +02:00
|
|
|
Aliases: ]]..config.cmd_pat..[[r, /r/subreddit
|
2016-01-08 14:44:37 +01:00
|
|
|
```]]
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-25 15:01:54 +02:00
|
|
|
local format_results = function(posts)
|
|
|
|
local output = ''
|
|
|
|
for _,v in ipairs(posts) do
|
|
|
|
local post = v.data
|
|
|
|
local title = post.title:gsub('%[', '('):gsub('%]', ')'):gsub('&', '&')
|
|
|
|
if title:len() > 256 then
|
|
|
|
title = title:sub(1, 253)
|
|
|
|
title = utilities.trim(title) .. '...'
|
|
|
|
end
|
|
|
|
local short_url = 'redd.it/' .. post.id
|
|
|
|
local s = '[' .. title .. '](' .. short_url .. ')'
|
2016-05-26 13:22:20 +02:00
|
|
|
if post.domain and not post.is_self and not post.over_18 then
|
2016-05-25 15:01:54 +02:00
|
|
|
s = '`[`[' .. post.domain .. '](' .. post.url:gsub('%)', '\\)') .. ')`]` ' .. s
|
|
|
|
end
|
|
|
|
output = output .. '• ' .. s .. '\n'
|
2016-02-14 09:46:27 +01:00
|
|
|
end
|
2016-05-25 15:01:54 +02:00
|
|
|
return output
|
|
|
|
end
|
|
|
|
|
|
|
|
reddit.subreddit_url = 'http://www.reddit.com/%s/.json?limit='
|
|
|
|
reddit.search_url = 'http://www.reddit.com/search.json?q=%s&limit='
|
|
|
|
reddit.rall_url = 'http://www.reddit.com/.json?limit='
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function reddit:action(msg, config)
|
2016-05-25 15:01:54 +02:00
|
|
|
-- Eight results in PM, four results elsewhere.
|
2015-11-25 03:22:04 +01:00
|
|
|
local limit = 4
|
2016-05-25 15:01:54 +02:00
|
|
|
if msg.chat.type == 'private' then
|
2015-11-25 03:22:04 +01:00
|
|
|
limit = 8
|
|
|
|
end
|
2016-05-25 15:01:54 +02:00
|
|
|
local text = msg.text_lower
|
|
|
|
if text:match('^/r/.') then
|
|
|
|
-- Normalize input so this hack works easily.
|
2016-05-27 05:28:44 +02:00
|
|
|
text = msg.text_lower:gsub('^/r/', config.cmd_pat..'r r/')
|
2016-05-25 15:01:54 +02:00
|
|
|
end
|
|
|
|
local input = utilities.input(text)
|
|
|
|
local source, url
|
2015-11-25 03:22:04 +01:00
|
|
|
if input then
|
2016-01-08 14:44:37 +01:00
|
|
|
if input:match('^r/.') then
|
2016-05-25 15:01:54 +02:00
|
|
|
input = utilities.get_word(input, 1)
|
|
|
|
url = reddit.subreddit_url:format(input) .. limit
|
|
|
|
source = '*/' .. utilities.md_escape(input) .. '*\n'
|
2015-07-03 00:15:52 +02:00
|
|
|
else
|
2016-05-25 15:01:54 +02:00
|
|
|
input = utilities.input(msg.text)
|
|
|
|
source = '*Results for* _' .. utilities.md_escape(input) .. '_ *:*\n'
|
|
|
|
input = URL.escape(input)
|
|
|
|
url = reddit.search_url:format(input) .. limit
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
else
|
2016-05-25 15:01:54 +02:00
|
|
|
url = reddit.rall_url .. limit
|
2016-01-08 14:44:37 +01:00
|
|
|
source = '*/r/all*\n'
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
local jstr, res = HTTP.request(url)
|
|
|
|
if res ~= 200 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2016-05-25 15:01:54 +02:00
|
|
|
else
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if #jdat.data.children == 0 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2016-05-25 15:01:54 +02:00
|
|
|
else
|
|
|
|
local output = format_results(jdat.data.children)
|
|
|
|
output = source .. output
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, true)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return reddit
|