From 14673a9c0528c3733c54090802c45326cceda296 Mon Sep 17 00:00:00 2001 From: mpaglia0 Date: Thu, 8 Dec 2022 13:54:15 +0100 Subject: [PATCH] Added message length check --- README.md | 6 ++---- pelican_toot.py | 30 +++++++++++++++++++----------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ec2983b9..eab4ba5a 100644 --- a/README.md +++ b/README.md @@ -26,16 +26,14 @@ This release can publish: Title is taken from `article.title` -Body is taken from `article.summary` with standard Pelican configuartion i.e. length trimmed to 50 words and summary ends with `...` +Body is taken from `article.summary` with standard Pelican configuartion i.e. length trimmed to 50 words and summary ends with `...` OR with parameters you set in `pelicanconf.py` (`SUMMARY_MAX_LENGTH` and `SUMMARY_END_SUFFIX`). -In a future release *Pelican-toot* must have its own configuration parameter for length of text since Mastodon accepts posts with max 500 bytes. +If the total length of the post exceeds MAX length allowed from Mastodon, then *Pelican-toot* will trim `article.summary` accordingly. Hashtag(s) are taken - if any - from `article.tags` and concatenated separating each of them with commas. Pelican can handle tags with whitespaces (for example `#My nice article`) without problems but hashtags in Mastodon are all written without. For this reason all whitespaces from Pelican hashtags will be removed before publishing (`#Mynicearticle`). -Now if your post exceeds this limit you simply will receive the errore message `your toot exceeds Mastodon max limit...` - ## Mastodon APIs This plugin depends on [Mastodon.py](https://github.com/halcy/Mastodon.py). diff --git a/pelican_toot.py b/pelican_toot.py index 562088ac..cca3573e 100644 --- a/pelican_toot.py +++ b/pelican_toot.py @@ -92,6 +92,8 @@ def post_updates(generator, writer): api_base_url = mt_base_url ) # Actually build the post structure + # First set a maximum length for the final post + toot_maxlength = 490 # Actually 500 but let's keep a safety gap for miscalculation... for article in new_posts: articleslist.append(article.url) titlehtmltext = article.title @@ -100,27 +102,33 @@ def post_updates(generator, writer): articlehtmltext = article.summary articlecleantext = html.fromstring(articlehtmltext) summary_to_publish = articlecleantext.text_content().strip() + '\n' - read_more = 'Read more: ' + article.get_siteurl() + '/' + article.url + '\n\n' + read_more = 'Leggi tutto: ' + article.get_siteurl() + '/' + article.url + '\n\n' if hasattr(article, 'tags'): taglist = article.tags new_taglist = [] for i in taglist: new_taglist.append('#' + str(i)) tags_to_publish = ', '.join(str(x).replace(" ", "") for x in new_taglist) - mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish + toot_length = len(title_to_publish) + len(summary_to_publish) + len(read_more) + len(tags_to_publish) + if toot_length > toot_maxlength: + truncate = toot_maxlength - len(title_to_publish) - len(tags_to_publish) - len(read_more) - 4 + summary_to_publish = summary_to_publish[:truncate] + ' ...' + '\n' + mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish + else: + mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish else: - mastodon_toot = title_to_publish + summary_to_publish + read_more - # check the length of the final post - toot_maxlength = 490 # Actually 500 but let's keep a safety gap... - if len(mastodon_toot) >= toot_maxlength: - logger.warning('Pelican_toot: your toot exceeds Mastodon max limit... ') - sys.exit(9) - else: - mastodon.toot(mastodon_toot) + toot_length = len(title_to_publish) + len(summary_to_publish) + len(read_more) + if toot_length > toot_maxlength: + truncate = toot_maxlength - len(title_to_publish) - len(read_more) - 4 + summary_to_publish = summary_to_publish[:truncate] + ' ...' + '\n' + mastodon_toot = title_to_publish + summary_to_publish + read_more + else: + mastodon_toot = title_to_publish + summary_to_publish + read_more + mastodon.toot(mastodon_toot) write_articleslist(articleslist) def register(): try: signals.article_writer_finalized.connect(post_updates) except AttributeError: - pass + pass \ No newline at end of file -- 2.30.2