Make adding images into the toot configurable.

Add an option "include_images" into the config file.
This commit is contained in:
Hartmut Goebel
2019-04-16 13:57:07 +02:00
parent 2d45df57f1
commit 0b65eb8e21

View File

@ -49,7 +49,8 @@ def main():
) )
for feed in config['feeds']: for feed in config['feeds']:
for entry in get_feed(feed['url'], config['updated']): for entry in get_feed(feed['url'], config['updated'],
config['include_images']):
media_ids = [] media_ids = []
for img in entry.get("images", []): for img in entry.get("images", []):
media = masto.media_post(img, img.headers['content-type']) media = masto.media_post(img, img.headers['content-type'])
@ -71,6 +72,7 @@ def save_config(config, config_file, toot_old_posts=False):
def read_config(config_file): def read_config(config_file):
config = { config = {
'updated': datetime(MINYEAR, 1, 1, 0, 0, 0, 0, timezone.utc), 'updated': datetime(MINYEAR, 1, 1, 0, 0, 0, 0, timezone.utc),
'include_images': False,
} }
with open(config_file) as fh: with open(config_file) as fh:
cfg = yaml.load(fh, yaml.SafeLoader) cfg = yaml.load(fh, yaml.SafeLoader)
@ -87,7 +89,7 @@ def detect_generator(feed):
return "wordpress" return "wordpress"
return None return None
def get_feed(feed_url, last_update): def get_feed(feed_url, last_update, include_images):
new_entries = 0 new_entries = 0
feed = feedparser.parse(feed_url) feed = feedparser.parse(feed_url)
if last_update: if last_update:
@ -99,7 +101,7 @@ def get_feed(feed_url, last_update):
generator = detect_generator(feed) generator = detect_generator(feed)
for entry in entries: for entry in entries:
new_entries += 1 new_entries += 1
yield get_entry(entry, generator) yield get_entry(entry, include_images, generator)
return new_entries return new_entries
def collect_images(entry, generator=None): def collect_images(entry, generator=None):
@ -145,7 +147,7 @@ def collect_images(entry, generator=None):
return images return images
def get_entry(entry, generator=None): def get_entry(entry, include_images, generator=None):
hashtags = [] hashtags = []
for tag in entry.get('tags', []): for tag in entry.get('tags', []):
for t in tag['term'].split(): for t in tag['term'].split():
@ -166,7 +168,7 @@ def get_entry(entry, generator=None):
'content': BeautifulSoup(summary, 'html.parser').get_text(), 'content': BeautifulSoup(summary, 'html.parser').get_text(),
'hashtags': ' '.join(hashtags), 'hashtags': ' '.join(hashtags),
'updated': dateutil.parser.parse(entry['updated']), 'updated': dateutil.parser.parse(entry['updated']),
'images': collect_images(entry, generator), 'images': collect_images(entry, generator) if include_images else [],
} }
def setup(config_file): def setup(config_file):
@ -198,12 +200,14 @@ def setup(config_file):
feed_url = input('RSS/Atom feed URL to watch: ') feed_url = input('RSS/Atom feed URL to watch: ')
old_posts = yes_no('Shall already existing entries be tooted, too?') old_posts = yes_no('Shall already existing entries be tooted, too?')
include_images = yes_no('Shall images be included in the toot?')
config = { config = {
'name': name, 'name': name,
'url': url, 'url': url,
'client_id': client_id, 'client_id': client_id,
'client_secret': client_secret, 'client_secret': client_secret,
'access_token': access_token, 'access_token': access_token,
'include_images': include_images,
'feeds': [ 'feeds': [
{'url': feed_url, 'template': '{title} {url}'} {'url': feed_url, 'template': '{title} {url}'}
] ]