From b2f95e241271b7f3692d1f27265830a0ee575a7e Mon Sep 17 00:00:00 2001 From: ssdaniel24 Date: Sat, 7 Dec 2024 07:07:31 +0300 Subject: [PATCH] Added --random flag for rss command --- plugins/RSS/plugin.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/plugins/RSS/plugin.py b/plugins/RSS/plugin.py index 29e0edd0e..797ba1d67 100644 --- a/plugins/RSS/plugin.py +++ b/plugins/RSS/plugin.py @@ -36,6 +36,7 @@ import json import time import types import string +import random import socket import threading import feedparser @@ -170,6 +171,8 @@ def sort_feed_items(items, order): """Return feed items, sorted according to sortFeedItems.""" if order == 'asInFeed': return items + elif order == 'random': + return random.shuffle(items) (key, reverse) = _sort_arguments(order) try: sitems = sorted(items, key=key, reverse=reverse) @@ -695,11 +698,12 @@ class RSS(callbacks.Plugin): channels = wrap(channels, ['feedName']) @internationalizeDocstring - def rss(self, irc, msg, args, url, n): - """ [] + def rss(self, irc, msg, args, optlist, url, n): + """[--random] [] Gets the title components of the given RSS feed. If is given, return only that many headlines. + Use --random flag for random sorting of entries. """ self.log.debug('Fetching %u', url) try: @@ -719,17 +723,22 @@ class RSS(callbacks.Plugin): s += str(feed.last_exception) irc.error(s) return - n = n or self.registryValue('defaultNumberOfHeadlines', channel, irc.network) entries = list(filter(lambda e:self.should_send_entry(irc.network, channel, e), feed.entries)) - entries = sort_feed_items(entries, 'newestFirst') + upper_sort = 'newestFirst' + if [y for x,y in optlist if x == 'random']: + upper_sort = 'random' + entries = sort_feed_items(entries, upper_sort) + n = n or self.registryValue('defaultNumberOfHeadlines', channel, irc.network) entries = entries[:n] entries = sort_feed_items(entries, self.registryValue('sortFeedItems')) headlines = map(lambda e:self.format_entry(irc.network, channel, feed, e, False), entries) sep = self.registryValue('headlineSeparator', channel, irc.network) irc.replies(headlines, joiner=sep) - rss = wrap(rss, [first('url', 'feedName'), additional('int')]) + rss = wrap(rss, [getopts({ 'random': '', }), + first('url', 'feedName'), + additional('int')]) @internationalizeDocstring def info(self, irc, msg, args, url):