diff options
| author | Matthew Fennell <matthew@fennell.dev> | 2026-08-23 22:39:35 +0100 |
|---|---|---|
| committer | Matthew Fennell <matthew@fennell.dev> | 2026-08-23 22:39:35 +0100 |
| commit | 58b762a53a5e64d6a6ea6999009d1899f7b6a4da (patch) | |
| tree | 950612344353c79d069b2f2185c8613c14ce7179 | |
| parent | 77078787b2568d791a0f180737d14070be46f23c (diff) | |
Add RSS feed plugins
These are used to save each feed entry to a file.
| -rw-r--r-- | html2text2.py | 29 | ||||
| -rw-r--r-- | plaintext.py | 52 |
2 files changed, 81 insertions, 0 deletions
diff --git a/html2text2.py b/html2text2.py new file mode 100644 index 0000000..cd6e635 --- /dev/null +++ b/html2text2.py @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: 2026 Matthew Fennell <matthew@fennell.dev> +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from __future__ import absolute_import, division, print_function + +import html2text + +class filter(object): + + def __init__(self, *args, feed=None, item=None, **kwargs): + if item.get('content'): + item['content_plain'] = ''.join([self.parse(x.value) + for x in item.get('content')]) + elif item.get('description') and item.get('description').strip(): + item['content_plain'] = self.parse(item.get('description')) + + @staticmethod + def parse(html=None): + if html is None: + return None + text_maker = html2text.HTML2Text() + text_maker.inline_links = False + text_maker.images_to_alt = True + text_maker.unicode_snob = True + text_maker.links_each_paragraph = True + text_maker.protect_links = True + text_maker.wrap_links = False + return text_maker.handle(html) diff --git a/plaintext.py b/plaintext.py new file mode 100644 index 0000000..19b17cd --- /dev/null +++ b/plaintext.py @@ -0,0 +1,52 @@ +# SPDX-FileCopyrightText: 2026 Matthew Fennell <matthew@fennell.dev> +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from feed2exec.utils import slug + +import logging +import os +import uuid + +def output(*args, feed=None, item=None, session=None, **kwargs): + + logging.debug(f'args: {args}') + logging.debug(f'feed: {feed}') + logging.debug(f'item: {item}') + logging.debug(f'session: {session}') + + title = item.get('title', str(uuid.uuid4())) + + if not args or len(args) != 1: + logging.error(f'Expected args to be a path, but got {args}') + return False + + if not item.get('link'): + logging.error(f'Link not given') + return False + + if not item.get('content_plain'): + logging.error(f'content_plain not given') + return False + + if not feed.get('name'): + logging.error(f'Feed name not given') + return False + + feed_dir = args[0] + + if not os.path.exists(feed_dir): + logging.error(f'Path {feed_dir} does not exist') + + if feed.get('catchup'): + return True + + path = '-'.join([slug(feed.get('name')), slug(title)]) + path = os.path.join(feed_dir, path) + + with open(path, 'w') as content: + logging.info(f'Writing {item.title} to {path}') + content.write(item.get('content_plain')) + content.write(item.get('link') + os.linesep) + + return True |
