diff options
Diffstat (limited to 'alfred/feed.py')
| -rw-r--r-- | alfred/feed.py | 131 |
1 files changed, 131 insertions, 0 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) |
