summaryrefslogtreecommitdiff
path: root/plaintext.py
blob: 19b17cdcf4323db4c307e7f4c23ca81ffc4156c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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