diff options
| -rw-r--r-- | alfred/feed.py | 131 | ||||
| -rw-r--r-- | alfred/helpstrings.py | 13 | ||||
| -rw-r--r-- | scripts/gen_bots.py | 1 |
3 files changed, 143 insertions, 2 deletions
diff --git a/alfred/feed.py b/alfred/feed.py new file mode 100644 index 0000000..b4c1c28 --- /dev/null +++ b/alfred/feed.py @@ -0,0 +1,131 @@ +""" +Sopel plugin: feed.gumx.cc aggregator management + +Commands (owner only): + !feed - show usage + !feed refresh - regenerate feed.gumx.cc now + !feed add <url> [nick] - add a feed and refresh + !feed list - list followed feeds + !feed rm <nick> - remove a feed by nick and refresh +""" + +import subprocess +import os +import sys + +from sopel import plugin + +import os as _os, sys as _sys +_alfred_dir = _os.path.dirname(_os.path.abspath(__file__)) +if _alfred_dir not in _sys.path: + _sys.path.insert(0, _alfred_dir) +import helpstrings as h + +FEEDS_FILE = '/home/ahmed/feeds.txt' +FEED_SCRIPT = '/home/ahmed/scripts/feed-gen.py' + + +def _is_owner(bot, trigger): + return trigger.nick == bot.settings.core.owner + + +def _load_feeds(): + feeds = [] + try: + with open(FEEDS_FILE) as f: + for line in f: + line = line.strip() + if line and not line.startswith('#'): + feeds.append(line) + except FileNotFoundError: + pass + return feeds + + +def _save_feeds(feeds): + with open(FEEDS_FILE, 'w') as f: + for line in feeds: + f.write(line + '\n') + + +def _refresh(bot): + try: + result = subprocess.run( + ['python3', FEED_SCRIPT], + capture_output=True, text=True, timeout=60, + ) + if result.returncode == 0: + last = result.stdout.strip().splitlines()[-1] if result.stdout.strip() else 'done' + bot.say(last) + else: + bot.say(f'error: {result.stderr.strip()[:200]}') + except subprocess.TimeoutExpired: + bot.say('feed-gen timed out') + except Exception as e: + bot.say(f'error: {e}') + + +@plugin.commands('feed') +def cmd_feed(bot, trigger): + if not _is_owner(bot, trigger): + return + + raw = (trigger.group(2) or '').strip() + args = raw.split(None, 2) + sub = args[0].lower() if args else '' + + if not sub: + bot.say(h.FEED_TOPIC) + return + + if sub == 'refresh': + bot.say('refreshing...') + _refresh(bot) + return + + if sub == 'list': + feeds = _load_feeds() + if not feeds: + bot.say('no feeds') + return + for line in feeds: + bot.say(line) + return + + if sub == 'add': + if len(args) < 2: + bot.say('usage: !feed add <url> [nick]') + return + url = args[1] + nick = args[2].strip() if len(args) > 2 else url + feeds = _load_feeds() + if any(line.split()[0] == url for line in feeds): + bot.say(f'already following {url}') + return + feeds.append(f'{url} {nick}') + _save_feeds(feeds) + bot.say(f'added {nick}') + _refresh(bot) + return + + if sub == 'rm': + if len(args) < 2: + bot.say('usage: !feed rm <nick>') + return + target = args[1].strip() + feeds = _load_feeds() + before = len(feeds) + feeds = [ + line for line in feeds + if not (len(line.split(None, 1)) > 1 and line.split(None, 1)[1].strip() == target) + and line.split()[0] != target + ] + if len(feeds) == before: + bot.say(f'not found: {target}') + return + _save_feeds(feeds) + bot.say(f'removed {target}') + _refresh(bot) + return + + bot.say(h.FEED_TOPIC) diff --git a/alfred/helpstrings.py b/alfred/helpstrings.py index 15362be..30fc996 100644 --- a/alfred/helpstrings.py +++ b/alfred/helpstrings.py @@ -1,4 +1,4 @@ -TOPICS = "topics: srv, bot, files, irc, coffee, vpn, list, git, twt. type !help <topic> for details" +TOPICS = "topics: srv, bot, files, irc, coffee, vpn, list, git, twt, feed. type !help <topic> for details" SRV_TOPIC = "srv: uptime disk mem load status net top topmem io conns who logs. type !help srv <cmd> for details" SRV = { @@ -86,6 +86,14 @@ TWF = { "no": "twt yes or no: confirm or cancel a pending delete. usage: !twt yes or !twt no", } +FEED_TOPIC = "feed: refresh add list rm. type !help feed <cmd> for details" +FEED = { + "refresh": "feed refresh: regenerate feed.gumx.cc from all followed feeds. usage: !feed refresh", + "add": "feed add: follow a feed. usage: !feed add <url> [nick]", + "list": "feed list: show all followed feeds. usage: !feed list", + "rm": "feed rm: remove a feed by nick or url. usage: !feed rm <nick>", +} + TOPIC_MAP = { "srv": (SRV_TOPIC, SRV), "bot": (BOT_TOPIC, BOT), @@ -96,9 +104,10 @@ TOPIC_MAP = { "list": (LIST_TOPIC, LIST), "git": (GIT_TOPIC, GIT), "twt": (TWF_TOPIC, TWF), + "feed": (FEED_TOPIC, FEED), } -_ALL = {**SRV, **BOT, **FILES, **IRC, **COFFEE, **VPN, **LIST, **GIT, **TWF} +_ALL = {**SRV, **BOT, **FILES, **IRC, **COFFEE, **VPN, **LIST, **GIT, **TWF, **FEED} def lookup(args): diff --git a/scripts/gen_bots.py b/scripts/gen_bots.py index 59fdb8e..0c7995a 100644 --- a/scripts/gen_bots.py +++ b/scripts/gen_bots.py @@ -42,6 +42,7 @@ FOOTER = """\ <footer> <hr> <a href="https://twt.gumx.cc">twt</a> / +<a href="https://feed.gumx.cc">feed</a> / <a href="https://git.gumx.cc">git</a> / <a href="https://mail.gumx.cc">mail</a> / <a href="https://list.gumx.cc">list</a> / |
