42 lines
1.2 KiB
Lua
42 lines
1.2 KiB
Lua
|
|
-- Fetch one stored feed and speak top 3 titles.
|
|||
|
|
-- If multiple feeds, picks the first one.
|
|||
|
|
local all = jarvis.memory.all()
|
|||
|
|
local feed_url = nil
|
|||
|
|
local feed_name = nil
|
|||
|
|
for _, rec in ipairs(all) do
|
|||
|
|
if rec.key:sub(1, 4) == "rss." then
|
|||
|
|
feed_url = rec.value
|
|||
|
|
feed_name = rec.key:sub(5)
|
|||
|
|
break
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if not feed_url then
|
|||
|
|
return jarvis.cmd.not_found("Лент не добавлено. Скажите 'добавь rss и URL'.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local r = jarvis.http.get(feed_url)
|
|||
|
|
if not r or not r.ok then
|
|||
|
|
return jarvis.cmd.error("Не получилось загрузить ленту.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local body = r.body or ""
|
|||
|
|
-- Crude regex parser. Works for most RSS 2.0 + Atom feeds.
|
|||
|
|
local titles = {}
|
|||
|
|
for t in body:gmatch("<title[^>]*>([^<]+)</title>") do
|
|||
|
|
-- Skip the channel/feed title (always first), keep item titles
|
|||
|
|
table.insert(titles, t)
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if #titles < 2 then
|
|||
|
|
return jarvis.cmd.error("В ленте нет заголовков.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
-- titles[1] is channel title; items start at [2]
|
|||
|
|
local sample = math.min(#titles - 1, 3)
|
|||
|
|
local line = "Из " .. feed_name .. ". "
|
|||
|
|
for i = 2, 1 + sample do
|
|||
|
|
line = line .. titles[i] .. ". "
|
|||
|
|
end
|
|||
|
|
return jarvis.cmd.ok(line)
|