2016-06-12 17:35:32 +02:00
|
|
|
local rss = {}
|
|
|
|
|
|
|
|
local feedparser = require("feedparser")
|
|
|
|
|
|
|
|
rss.command = 'rss <sub/del>'
|
|
|
|
|
|
|
|
function rss:init(config)
|
2016-07-17 13:22:27 +02:00
|
|
|
rss.triggers = {
|
|
|
|
"^/(rss) @(.*)$",
|
|
|
|
"^/rss$",
|
|
|
|
"^/rss (sub) (https?://[%w-_%.%?%.:/%+=&%~]+) @(.*)$",
|
|
|
|
"^/rss (sub) (https?://[%w-_%.%?%.:/%+=&%~]+)$",
|
|
|
|
"^/rss (del) (%d+) @(.*)$",
|
|
|
|
"^/rss (del) (%d+)$",
|
2016-08-15 23:14:28 +02:00
|
|
|
"^/rss (del)$",
|
2016-07-17 13:22:27 +02:00
|
|
|
"^/rss (sync)$"
|
|
|
|
}
|
2016-06-12 17:35:32 +02:00
|
|
|
rss.doc = [[*
|
2016-07-17 13:22:27 +02:00
|
|
|
]]..config.cmd_pat..[[rss* _@[Kanalname]_: Feed-Abonnements anzeigen
|
|
|
|
*]]..config.cmd_pat..[[rss* _sub_ _<URL>_ _@[Kanalname]_: Diesen Feed abonnieren
|
|
|
|
*]]..config.cmd_pat..[[rss* _del_ _<#>_ _@[Kanalname]_: Diesen Feed deabonnieren
|
|
|
|
*]]..config.cmd_pat..[[rss* _sync_: Feeds syncen (nur Superuser)
|
|
|
|
Der Kanalname ist optional]]
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function tail(n, k)
|
|
|
|
local u, r=''
|
|
|
|
for i=1,k do
|
|
|
|
n,r = math.floor(n/0x40), n%0x40
|
|
|
|
u = string.char(r+0x80) .. u
|
|
|
|
end
|
|
|
|
return u, n
|
|
|
|
end
|
|
|
|
|
|
|
|
function to_utf8(a)
|
|
|
|
local n, r, u = tonumber(a)
|
|
|
|
if n<0x80 then -- 1 byte
|
|
|
|
return string.char(n)
|
|
|
|
elseif n<0x800 then -- 2 byte
|
|
|
|
u, n = tail(n, 1)
|
|
|
|
return string.char(n+0xc0) .. u
|
|
|
|
elseif n<0x10000 then -- 3 byte
|
|
|
|
u, n = tail(n, 2)
|
|
|
|
return string.char(n+0xe0) .. u
|
|
|
|
elseif n<0x200000 then -- 4 byte
|
|
|
|
u, n = tail(n, 3)
|
|
|
|
return string.char(n+0xf0) .. u
|
|
|
|
elseif n<0x4000000 then -- 5 byte
|
|
|
|
u, n = tail(n, 4)
|
|
|
|
return string.char(n+0xf8) .. u
|
|
|
|
else -- 6 byte
|
|
|
|
u, n = tail(n, 5)
|
|
|
|
return string.char(n+0xfc) .. u
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-20 21:01:08 +02:00
|
|
|
function cleanRSS(str)
|
2016-08-22 22:29:30 +02:00
|
|
|
str = string.gsub(str, '%[%…%]', '')
|
2016-07-20 21:01:08 +02:00
|
|
|
str = string.gsub(str, '%[mehr%]', '')
|
|
|
|
str = string.gsub(str, '%[video%]', '')
|
|
|
|
str = string.gsub(str, '...%[more%]', '')
|
|
|
|
str = string.gsub(str, '%[more%]', '')
|
2016-08-21 19:59:36 +02:00
|
|
|
str = string.gsub(str, '%[liveticker%]', '')
|
|
|
|
str = string.gsub(str, '%[livestream%]', '')
|
|
|
|
str = string.gsub(str, '%[multimedia%]', '')
|
2016-07-20 21:01:08 +02:00
|
|
|
str = string.gsub(str, '%[phoenix%]', '')
|
2016-07-22 21:53:41 +02:00
|
|
|
str = string.gsub(str, '%[swr%]', '')
|
2016-08-21 19:59:36 +02:00
|
|
|
str = string.gsub(str, '%[ndr%]', '')
|
|
|
|
str = string.gsub(str, '%[mdr%]', '')
|
|
|
|
str = string.gsub(str, '%[wdr%]', '')
|
|
|
|
str = string.gsub(str, '%[hr%]', '')
|
|
|
|
str = string.gsub(str, '%[br%]', '')
|
2016-07-20 21:01:08 +02:00
|
|
|
str = string.gsub(str, 'Click for full.', '')
|
|
|
|
str = string.gsub(str, 'Read more »', '')
|
|
|
|
str = string.gsub(str, 'Read more', '')
|
2016-07-21 13:13:26 +02:00
|
|
|
str = string.gsub(str, '%(more…%)', '')
|
2016-07-21 14:04:27 +02:00
|
|
|
str = string.gsub(str, 'View On WordPress', '')
|
2016-07-22 21:53:41 +02:00
|
|
|
str = string.gsub(str, '%(RSS generated with FetchRss%)', '')
|
2016-07-21 13:13:26 +02:00
|
|
|
str = string.gsub(str, 'Meldung bei www%.tagesschau%.de lesen', '')
|
2016-08-22 13:50:02 +02:00
|
|
|
str = string.gsub(str, 'The post.*appeared first on Sugoi! Anime Blog%.', '')
|
|
|
|
str = string.gsub(str, 'Der Beitrag.*erschien zuerst auf MAnime%.de%.', '')
|
2016-08-21 19:59:36 +02:00
|
|
|
str = string.gsub(str, 'http://www%.serienjunkies%.de/.*%.html', '')
|
2016-07-20 21:01:08 +02:00
|
|
|
return str
|
2016-07-18 18:37:29 +02:00
|
|
|
end
|
|
|
|
|
2016-06-12 17:35:32 +02:00
|
|
|
function get_base_redis(id, option, extra)
|
|
|
|
local ex = ''
|
|
|
|
if option ~= nil then
|
|
|
|
ex = ex .. ':' .. option
|
|
|
|
if extra ~= nil then
|
|
|
|
ex = ex .. ':' .. extra
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 'rss:' .. id .. ex
|
|
|
|
end
|
|
|
|
|
|
|
|
function prot_url(url)
|
|
|
|
local url, h = string.gsub(url, "http://", "")
|
|
|
|
local url, hs = string.gsub(url, "https://", "")
|
|
|
|
local protocol = "http"
|
|
|
|
if hs == 1 then
|
|
|
|
protocol = "https"
|
|
|
|
end
|
|
|
|
return url, protocol
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_rss(url, prot)
|
|
|
|
local res, code = nil, 0
|
|
|
|
if prot == "http" then
|
|
|
|
res, code = http.request(url)
|
|
|
|
elseif prot == "https" then
|
|
|
|
res, code = https.request(url)
|
|
|
|
end
|
|
|
|
if code ~= 200 then
|
|
|
|
return nil, "Fehler beim Erreichen von " .. url
|
|
|
|
end
|
|
|
|
local parsed = feedparser.parse(res)
|
|
|
|
if parsed == nil then
|
|
|
|
return nil, "Fehler beim Dekodieren des Feeds.\nBist du sicher, dass "..url.." ein Feed ist?"
|
|
|
|
end
|
|
|
|
return parsed, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_new_entries(last, nentries)
|
|
|
|
local entries = {}
|
|
|
|
for k,v in pairs(nentries) do
|
|
|
|
if v.id == last then
|
|
|
|
return entries
|
|
|
|
else
|
|
|
|
table.insert(entries, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return entries
|
|
|
|
end
|
|
|
|
|
|
|
|
function rss:subscribe(id, url)
|
|
|
|
local baseurl, protocol = prot_url(url)
|
|
|
|
|
|
|
|
local prothash = get_base_redis(baseurl, "protocol")
|
|
|
|
local lasthash = get_base_redis(baseurl, "last_entry")
|
|
|
|
local lhash = get_base_redis(baseurl, "subs")
|
|
|
|
local uhash = get_base_redis(id)
|
|
|
|
|
|
|
|
if redis:sismember(uhash, baseurl) then
|
2016-08-04 13:44:56 +02:00
|
|
|
return "Du hast <code>"..url.."</code> bereits abonniert."
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local parsed, err = get_rss(url, protocol)
|
|
|
|
if err ~= nil then
|
|
|
|
return err
|
|
|
|
end
|
|
|
|
|
|
|
|
local last_entry = ""
|
|
|
|
if #parsed.entries > 0 then
|
|
|
|
last_entry = parsed.entries[1].id
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = parsed.feed.title
|
|
|
|
|
|
|
|
redis:set(prothash, protocol)
|
|
|
|
redis:set(lasthash, last_entry)
|
|
|
|
redis:sadd(lhash, id)
|
|
|
|
redis:sadd(uhash, baseurl)
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
return "<i>"..name.."</i> abonniert!"
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function rss:unsubscribe(id, n)
|
|
|
|
if #n > 5 then
|
|
|
|
return "Du kannst nicht mehr als fünf Feeds abonnieren!"
|
|
|
|
end
|
|
|
|
n = tonumber(n)
|
|
|
|
|
|
|
|
local uhash = get_base_redis(id)
|
|
|
|
local subs = redis:smembers(uhash)
|
|
|
|
if n < 1 or n > #subs then
|
|
|
|
return "Abonnement-ID zu hoch!"
|
|
|
|
end
|
|
|
|
local sub = subs[n]
|
|
|
|
local lhash = get_base_redis(sub, "subs")
|
|
|
|
|
|
|
|
redis:srem(uhash, sub)
|
|
|
|
redis:srem(lhash, id)
|
|
|
|
|
|
|
|
local left = redis:smembers(lhash)
|
|
|
|
if #left < 1 then -- no one subscribed, remove it
|
|
|
|
local prothash = get_base_redis(sub, "protocol")
|
|
|
|
local lasthash = get_base_redis(sub, "last_entry")
|
|
|
|
redis:del(prothash)
|
|
|
|
redis:del(lasthash)
|
|
|
|
end
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
return "Du hast <code>"..sub.."</code> deabonniert."
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function rss:print_subs(id, chat_name)
|
|
|
|
local uhash = get_base_redis(id)
|
|
|
|
local subs = redis:smembers(uhash)
|
|
|
|
if not subs[1] then
|
2016-08-04 13:44:56 +02:00
|
|
|
return '<b>Keine Feeds abonniert!</b>'
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
local keyboard = '{"keyboard":[['
|
|
|
|
local keyboard_buttons = ''
|
2016-08-04 13:44:56 +02:00
|
|
|
local text = '<b>'..chat_name..'</b> hat abonniert:\n---------\n'
|
2016-06-12 17:35:32 +02:00
|
|
|
for k,v in pairs(subs) do
|
|
|
|
text = text .. k .. ") " .. v .. '\n'
|
2016-07-17 13:22:27 +02:00
|
|
|
if k == #subs then
|
|
|
|
keyboard_buttons = keyboard_buttons..'{"text":"/rss del '..k..'"}'
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
keyboard_buttons = keyboard_buttons..'{"text":"/rss del '..k..'"},'
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
local keyboard = keyboard..keyboard_buttons..']], "one_time_keyboard":true, "selective":true, "resize_keyboard":true}'
|
|
|
|
return text, keyboard
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
function rss:action(msg, config, matches)
|
2016-06-12 17:35:32 +02:00
|
|
|
local id = "user#id" .. msg.from.id
|
|
|
|
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
|
|
|
|
id = 'chat#id'..msg.chat.id
|
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
-- For channels
|
|
|
|
if matches[1] == 'sub' and matches[2] and matches[3] then
|
|
|
|
if msg.from.id ~= config.admin then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.sudo)
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local id = '@'..matches[3]
|
2016-08-24 15:38:29 +02:00
|
|
|
local result = utilities.get_chat_info(id)
|
2016-07-17 13:22:27 +02:00
|
|
|
if not result then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Diesen Kanal gibt es nicht!')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local output = rss:subscribe(id, matches[2])
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, 'HTML')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
elseif matches[1] == 'del' and matches[2] and matches[3] then
|
|
|
|
if msg.from.id ~= config.admin then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.sudo)
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local id = '@'..matches[3]
|
2016-08-24 15:38:29 +02:00
|
|
|
local result = utilities.get_chat_info(id)
|
2016-07-17 13:22:27 +02:00
|
|
|
if not result then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Diesen Kanal gibt es nicht!')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
local output = rss:unsubscribe(id, matches[2])
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, 'HTML')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
elseif matches[1] == 'rss' and matches[2] then
|
|
|
|
local id = '@'..matches[2]
|
2016-08-24 15:38:29 +02:00
|
|
|
local result = utilities.get_chat_info(id)
|
2016-07-17 13:22:27 +02:00
|
|
|
if not result then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Diesen Kanal gibt es nicht!')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local chat_name = result.result.title
|
2016-06-12 17:35:32 +02:00
|
|
|
local output = rss:print_subs(id, chat_name)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, 'HTML')
|
2016-06-12 17:35:32 +02:00
|
|
|
return
|
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
|
|
|
|
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
|
|
|
|
chat_name = msg.chat.title
|
|
|
|
else
|
|
|
|
chat_name = msg.chat.first_name
|
|
|
|
end
|
|
|
|
|
|
|
|
if matches[1] == 'sub' and matches[2] then
|
2016-06-12 17:35:32 +02:00
|
|
|
if msg.from.id ~= config.admin then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.sudo)
|
2016-06-12 17:35:32 +02:00
|
|
|
return
|
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
local output = rss:subscribe(id, matches[2])
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, 'HTML')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
elseif matches[1] == 'del' and matches[2] then
|
2016-06-12 17:35:32 +02:00
|
|
|
if msg.from.id ~= config.admin then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.sudo)
|
2016-06-12 17:35:32 +02:00
|
|
|
return
|
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
local output = rss:unsubscribe(id, matches[2])
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, 'HTML', '{"hide_keyboard":true}')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
elseif matches[1] == 'del' and not matches[2] then
|
|
|
|
local list_subs, keyboard = rss:print_subs(id, chat_name)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, list_subs, 'HTML', keyboard)
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
elseif matches[1] == 'sync' then
|
2016-06-12 17:35:32 +02:00
|
|
|
if msg.from.id ~= config.admin then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.sudo)
|
2016-06-12 17:35:32 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
rss:cron()
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
local output = rss:print_subs(id, chat_name)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, 'HTML')
|
2016-06-12 17:35:32 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-24 15:38:29 +02:00
|
|
|
function rss:cron()
|
2016-06-12 17:35:32 +02:00
|
|
|
local keys = redis:keys(get_base_redis("*", "subs"))
|
|
|
|
for k,v in pairs(keys) do
|
|
|
|
local base = string.match(v, "rss:(.+):subs") -- Get the URL base
|
2016-08-21 19:59:36 +02:00
|
|
|
print('Checke RSS: '..base)
|
2016-06-12 17:35:32 +02:00
|
|
|
local prot = redis:get(get_base_redis(base, "protocol"))
|
|
|
|
local last = redis:get(get_base_redis(base, "last_entry"))
|
|
|
|
local url = prot .. "://" .. base
|
|
|
|
local parsed, err = get_rss(url, prot)
|
|
|
|
if err ~= nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- local feed_title = parsed.feed.title
|
|
|
|
local newentr = get_new_entries(last, parsed.entries)
|
|
|
|
local subscribers = {}
|
|
|
|
local text = '' -- Send one message per feed with the latest entries
|
|
|
|
for k2, v2 in pairs(newentr) do
|
2016-08-16 13:05:26 +02:00
|
|
|
local title = unescape(v2.title) or 'Kein Titel'
|
2016-06-12 17:35:32 +02:00
|
|
|
local link = v2.link or v2.id or 'Kein Link'
|
2016-07-23 13:33:20 +02:00
|
|
|
if v2.content then
|
|
|
|
content = v2.content:gsub("%b<>", "")
|
2016-08-01 21:07:27 +02:00
|
|
|
if string.len(v2.content) > 300 then
|
2016-08-21 19:59:36 +02:00
|
|
|
content = cleanRSS(content)
|
2016-08-16 13:05:26 +02:00
|
|
|
content = unescape(content)
|
2016-08-01 21:07:27 +02:00
|
|
|
content = string.sub(content, 1, 300)..'...'
|
2016-06-12 17:35:32 +02:00
|
|
|
else
|
2016-08-21 19:59:36 +02:00
|
|
|
content = cleanRSS(content)
|
|
|
|
content = unescape(content)
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
elseif v2.summary then
|
2016-07-23 13:33:20 +02:00
|
|
|
content = v2.summary:gsub("%b<>", "")
|
2016-08-01 21:07:27 +02:00
|
|
|
if string.len(v2.summary) > 300 then
|
2016-08-21 19:59:36 +02:00
|
|
|
content = cleanRSS(content)
|
|
|
|
content = unescape(content)
|
2016-08-01 21:07:27 +02:00
|
|
|
content = string.sub(content, 1, 300)..'...'
|
2016-06-12 17:35:32 +02:00
|
|
|
else
|
2016-08-21 19:59:36 +02:00
|
|
|
content = cleanRSS(content)
|
|
|
|
content = unescape(content)
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
content = ''
|
|
|
|
end
|
2016-08-04 20:37:12 +02:00
|
|
|
text = text..'\n<b>[</b>#RSS<b>] '..title..'</b>\n'..utilities.trim(content)..' <a href="'..link..'">Weiterlesen</a>\n'
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
if text ~= '' then
|
|
|
|
local newlast = newentr[1].id
|
|
|
|
redis:set(get_base_redis(base, "last_entry"), newlast)
|
|
|
|
for k2, receiver in pairs(redis:smembers(v)) do
|
|
|
|
local receiver = string.gsub(receiver, 'chat%#id', '')
|
|
|
|
local receiver = string.gsub(receiver, 'user%#id', '')
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_message(receiver, text, true, nil, 'HTML')
|
2016-06-12 17:35:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
return rss
|