2016-04-10 21:04:47 -07:00
|
|
|
local hackernews = {}
|
|
|
|
|
|
|
|
local HTTPS = require('ssl.https')
|
2016-04-15 19:07:23 +00:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 00:31:34 -04:00
|
|
|
local bindings = require('otouto.bindings')
|
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-10 21:04:47 -07:00
|
|
|
|
|
|
|
hackernews.command = 'hackernews'
|
2016-05-26 20:28:44 -07:00
|
|
|
|
|
|
|
function hackernews:init(config)
|
|
|
|
hackernews.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hackernews', true):t('hn', true).table
|
|
|
|
hackernews.doc = [[```
|
2016-01-08 08:44:37 -05:00
|
|
|
Returns four (if group) or eight (if private message) top stories from Hacker News.
|
2016-05-26 20:28:44 -07:00
|
|
|
Alias: ]]..config.cmd_pat..[[hn
|
2016-01-08 08:44:37 -05:00
|
|
|
```]]
|
2016-04-11 20:55:46 -07:00
|
|
|
end
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-05-26 17:26:30 -07:00
|
|
|
function hackernews:action(msg, config)
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-05-29 13:08:39 -04:00
|
|
|
bindings.sendChatAction(self, { chat_id = msg.chat.id, action = 'typing' } )
|
2015-12-14 14:48:17 -05:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
local jstr, res = HTTPS.request('https://hacker-news.firebaseio.com/v0/topstories.json')
|
|
|
|
if res ~= 200 then
|
2016-05-26 17:26:30 -07:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-24 21:22:04 -05:00
|
|
|
return
|
|
|
|
end
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
|
|
|
local res_count = 4
|
2015-07-02 18:15:52 -04:00
|
|
|
if msg.chat.id == msg.from.id then
|
2015-11-24 21:22:04 -05:00
|
|
|
res_count = 8
|
2015-07-02 18:15:52 -04:00
|
|
|
end
|
|
|
|
|
2016-01-08 08:44:37 -05:00
|
|
|
local output = '*Hacker News:*\n'
|
2015-11-24 21:22:04 -05:00
|
|
|
for i = 1, res_count do
|
|
|
|
local res_url = 'https://hacker-news.firebaseio.com/v0/item/' .. jdat[i] .. '.json'
|
|
|
|
jstr, res = HTTPS.request(res_url)
|
|
|
|
if res ~= 200 then
|
2016-05-26 17:26:30 -07:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-24 21:22:04 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local res_jdat = JSON.decode(jstr)
|
2016-01-08 08:44:37 -05:00
|
|
|
local title = res_jdat.title:gsub('%[.+%]', ''):gsub('%(.+%)', ''):gsub('&', '&')
|
|
|
|
if title:len() > 48 then
|
|
|
|
title = title:sub(1, 45) .. '...'
|
|
|
|
end
|
|
|
|
local url = res_jdat.url
|
2016-03-22 06:16:26 -04:00
|
|
|
if not url then
|
2016-05-26 17:26:30 -07:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2016-03-22 06:16:26 -04:00
|
|
|
return
|
|
|
|
end
|
2016-01-08 08:44:37 -05:00
|
|
|
if url:find('%(') then
|
|
|
|
output = output .. '• ' .. title .. '\n' .. url:gsub('_', '\\_') .. '\n'
|
|
|
|
else
|
|
|
|
output = output .. '• [' .. title .. '](' .. url .. ')\n'
|
|
|
|
end
|
|
|
|
|
2015-07-02 18:15:52 -04:00
|
|
|
end
|
|
|
|
|
2016-05-29 13:08:39 -04:00
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, true)
|
2015-07-02 18:15:52 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-10 21:04:47 -07:00
|
|
|
return hackernews
|