add setting to shorten rss urls (rss-shorten)
This commit is contained in:
parent
92511122ff
commit
ad7f9a8024
1 changed files with 21 additions and 19 deletions
|
@ -7,27 +7,32 @@ import feedparser
|
||||||
|
|
||||||
RSS_INTERVAL = 60 # 1 minute
|
RSS_INTERVAL = 60 # 1 minute
|
||||||
|
|
||||||
def _format_entry(feed_title, entry):
|
|
||||||
title = entry["title"]
|
|
||||||
|
|
||||||
author = entry.get("author", None)
|
|
||||||
author = " by %s" % author if author else ""
|
|
||||||
|
|
||||||
link = entry.get("link", None)
|
|
||||||
link = " - %s" % link if link else ""
|
|
||||||
|
|
||||||
feed_title_str = "%s: " % feed_title if feed_title else ""
|
|
||||||
|
|
||||||
return "%s%s%s%s" % (feed_title_str, title, author, link)
|
|
||||||
|
|
||||||
@utils.export("botset", utils.IntSetting("rss-interval",
|
@utils.export("botset", utils.IntSetting("rss-interval",
|
||||||
"Interval (in seconds) between RSS polls", example="120"))
|
"Interval (in seconds) between RSS polls", example="120"))
|
||||||
|
@utils.export("channelset", utils.BoolSetting("rss-shorten",
|
||||||
|
"Whether or not to shorten RSS urls"))
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
_name = "RSS"
|
_name = "RSS"
|
||||||
def on_load(self):
|
def on_load(self):
|
||||||
self.timers.add("rss", self.bot.get_setting("rss-interval",
|
self.timers.add("rss", self.bot.get_setting("rss-interval",
|
||||||
RSS_INTERVAL))
|
RSS_INTERVAL))
|
||||||
|
|
||||||
|
def _format_entry(self, server, feed_title, entry, shorten):
|
||||||
|
title = entry["title"]
|
||||||
|
|
||||||
|
author = entry.get("author", None)
|
||||||
|
author = " by %s" % author if author else ""
|
||||||
|
|
||||||
|
link = entry.get("link", None)
|
||||||
|
if shorten:
|
||||||
|
link = self.exports.get_one("shorturl")(server, link)
|
||||||
|
link = " - %s" % link if link else ""
|
||||||
|
|
||||||
|
feed_title_str = "%s: " % feed_title if feed_title else ""
|
||||||
|
|
||||||
|
return "%s%s%s%s" % (feed_title_str, title, author, link)
|
||||||
|
|
||||||
|
|
||||||
@utils.hook("timer.rss")
|
@utils.hook("timer.rss")
|
||||||
def timer(self, event):
|
def timer(self, event):
|
||||||
start_time = time.monotonic()
|
start_time = time.monotonic()
|
||||||
|
@ -55,7 +60,6 @@ class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
feed = feedparser.parse(pages[url].data)
|
feed = feedparser.parse(pages[url].data)
|
||||||
feed_title = feed["feed"].get("title", None)
|
feed_title = feed["feed"].get("title", None)
|
||||||
entry_formatted = {}
|
|
||||||
|
|
||||||
for server, channel in channels:
|
for server, channel in channels:
|
||||||
seen_ids = channel.get_setting("rss-seen-ids-%s" % url, [])
|
seen_ids = channel.get_setting("rss-seen-ids-%s" % url, [])
|
||||||
|
@ -71,11 +75,9 @@ class Module(ModuleManager.BaseModule):
|
||||||
continue
|
continue
|
||||||
valid += 1
|
valid += 1
|
||||||
|
|
||||||
if not entry_id in entry_formatted:
|
shorten = channel.get_setting("rss-shorten", False)
|
||||||
output = _format_entry(feed_title, entry)
|
output = self._format_entry(server, feed_title, entry,
|
||||||
entry_formatted[entry_id] = output
|
shorten)
|
||||||
else:
|
|
||||||
output = entry_formatted[entry_id]
|
|
||||||
|
|
||||||
self.events.on("send.stdout").call(target=channel,
|
self.events.on("send.stdout").call(target=channel,
|
||||||
module_name="RSS", server=server, message=output)
|
module_name="RSS", server=server, message=output)
|
||||||
|
|
Loading…
Reference in a new issue