2016-04-11 06:04:47 +02:00
|
|
|
local HTTPS = require('ssl.https')
|
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-08-14 04:26:44 +02:00
|
|
|
local bindings = require('otouto.bindings')
|
|
|
|
|
|
|
|
local hackernews = {}
|
2016-04-11 06:04:47 +02:00
|
|
|
|
|
|
|
hackernews.command = 'hackernews'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
local function get_hackernews_results()
|
2016-08-14 04:46:18 +02:00
|
|
|
local results = {}
|
|
|
|
local jstr, code = HTTPS.request(hackernews.topstories_url)
|
|
|
|
if code ~= 200 then return end
|
|
|
|
local data = JSON.decode(jstr)
|
|
|
|
for i = 1, 8 do
|
|
|
|
local ijstr, icode = HTTPS.request(hackernews.res_url:format(data[i]))
|
|
|
|
if icode ~= 200 then return end
|
|
|
|
local idata = JSON.decode(ijstr)
|
|
|
|
local result
|
|
|
|
if idata.url then
|
|
|
|
result = string.format(
|
|
|
|
'\n• <code>[</code><a href="%s">%s</a><code>]</code> <a href="%s">%s</a>',
|
|
|
|
utilities.html_escape(hackernews.art_url:format(idata.id)),
|
|
|
|
idata.id,
|
|
|
|
utilities.html_escape(idata.url),
|
|
|
|
utilities.html_escape(idata.title)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
result = string.format(
|
|
|
|
'\n• <code>[</code><a href="%s">%s</a><code>]</code> %s',
|
|
|
|
utilities.html_escape(hackernews.art_url:format(idata.id)),
|
|
|
|
idata.id,
|
|
|
|
utilities.html_escape(idata.title)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
table.insert(results, result)
|
|
|
|
end
|
|
|
|
return results
|
2016-08-14 04:26:44 +02:00
|
|
|
end
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function hackernews:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
hackernews.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hackernews', true):t('hn', true).table
|
|
|
|
hackernews.doc = [[Returns four (if group) or eight (if private message) top stories from Hacker News.
|
2016-07-25 11:03:35 +02:00
|
|
|
Alias: ]] .. config.cmd_pat .. 'hn'
|
2016-08-14 04:46:18 +02:00
|
|
|
hackernews.topstories_url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
|
|
|
|
hackernews.res_url = 'https://hacker-news.firebaseio.com/v0/item/%s.json'
|
|
|
|
hackernews.art_url = 'https://news.ycombinator.com/item?id=%s'
|
|
|
|
hackernews.last_update = 0
|
|
|
|
if config.hackernews_onstart == true then
|
|
|
|
hackernews.results = get_hackernews_results()
|
|
|
|
if hackernews.results then hackernews.last_update = os.time() / 60 end
|
|
|
|
end
|
2016-04-12 05:55:46 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function hackernews:action(msg, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
local now = os.time() / 60
|
|
|
|
if not hackernews.results or hackernews.last_update + config.hackernews_interval < now then
|
2016-08-23 06:16:32 +02:00
|
|
|
bindings.sendChatAction{ chat_id = msg.chat.id, action = 'typing' }
|
2016-08-14 04:46:18 +02:00
|
|
|
hackernews.results = get_hackernews_results()
|
|
|
|
if not hackernews.results then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
hackernews.last_update = now
|
|
|
|
end
|
|
|
|
-- Four results in a group, eight in private.
|
|
|
|
local res_count = msg.chat.id == msg.from.id and 8 or 4
|
|
|
|
local output = '<b>Top Stories from Hacker News:</b>'
|
|
|
|
for i = 1, res_count do
|
|
|
|
output = output .. hackernews.results[i]
|
|
|
|
end
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_message(msg.chat.id, output, true, nil, 'html')
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return hackernews
|