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-04-11 06:04:47 +02:00
|
|
|
local bindings = require('bindings')
|
|
|
|
local utilities = require('utilities')
|
|
|
|
|
|
|
|
reddit.command = 'reddit [r/subreddit | query]'
|
|
|
|
reddit.doc = [[```
|
2016-01-08 14:44:37 +01:00
|
|
|
/reddit [r/subreddit | query]
|
|
|
|
Returns the four (if group) or eight (if private message) top posts for the given subreddit or query, or from the frontpage.
|
|
|
|
Aliases: /r, /r/[subreddit]
|
|
|
|
```]]
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function reddit:init()
|
2016-04-26 21:04:42 +02:00
|
|
|
reddit.triggers = utilities.triggers(self.info.username, {'^/r/'}):t('reddit', true):t('r', true):t('r/', true).table
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function reddit:action(msg)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
msg.text_lower = msg.text_lower:gsub('/r/', '/r r/')
|
2016-04-11 06:04:47 +02:00
|
|
|
local input
|
2016-02-14 09:46:27 +01:00
|
|
|
if msg.text_lower:match('^/r/') then
|
|
|
|
msg.text_lower = msg.text_lower:gsub('/r/', '/r r/')
|
2016-04-11 06:04:47 +02:00
|
|
|
input = utilities.get_word(msg.text_lower, 1)
|
2016-02-14 09:46:27 +01:00
|
|
|
else
|
2016-04-08 23:12:02 +02:00
|
|
|
input = utilities.input(msg.text_lower)
|
2016-02-14 09:46:27 +01:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
local url
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local limit = 4
|
|
|
|
if msg.chat.id == msg.from.id then
|
|
|
|
limit = 8
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
local source
|
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-02-14 09:46:27 +01:00
|
|
|
url = 'http://www.reddit.com/' .. URL.escape(input) .. '/.json?limit=' .. limit
|
2016-01-08 14:44:37 +01:00
|
|
|
source = '*/r/' .. input:match('^r/(.+)') .. '*\n'
|
2015-07-03 00:15:52 +02:00
|
|
|
else
|
2016-02-14 09:46:27 +01:00
|
|
|
url = 'http://www.reddit.com/search.json?q=' .. URL.escape(input) .. '&limit=' .. limit
|
2016-01-12 11:22:28 +01:00
|
|
|
source = '*reddit results for* _' .. input .. '_ *:*\n'
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
else
|
2015-11-25 03:22:04 +01:00
|
|
|
url = 'http://www.reddit.com/.json?limit=' .. 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-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if #jdat.data.children == 0 then
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, msg, self.config.errors.results)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
local output = ''
|
2016-04-11 06:04:47 +02:00
|
|
|
for _,v in ipairs(jdat.data.children) do
|
2016-02-14 09:46:27 +01:00
|
|
|
local title = v.data.title:gsub('%[', '('):gsub('%]', ')'):gsub('&', '&')
|
2016-01-08 14:44:37 +01:00
|
|
|
if title:len() > 48 then
|
|
|
|
title = title:sub(1,45) .. '...'
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
if v.data.over_18 then
|
2016-01-08 14:44:37 +01:00
|
|
|
v.data.is_self = true
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-01-08 14:44:37 +01:00
|
|
|
local short_url = 'redd.it/' .. v.data.id
|
|
|
|
output = output .. '• [' .. title .. '](' .. short_url .. ')\n'
|
2015-11-25 03:22:04 +01:00
|
|
|
if not v.data.is_self then
|
2016-01-08 14:44:37 +01:00
|
|
|
output = output .. v.data.url:gsub('_', '\\_') .. '\n'
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
output = source .. output
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendMessage(self, msg.chat.id, output, true, nil, true)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return reddit
|