chiark / gitweb /
Added message length check
authormpaglia0 <mpaglia0@gmail.com>
Thu, 8 Dec 2022 12:54:15 +0000 (13:54 +0100)
committermpaglia0 <mpaglia0@gmail.com>
Thu, 8 Dec 2022 12:54:15 +0000 (13:54 +0100)
README.md
pelican_toot.py

index ec2983b9dc7ab7407799afbcbacef626fb558a4f..eab4ba5a0009f44c02ec4053328e3d56eed0089b 100644 (file)
--- 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).
index 562088ac611885562dab204680570e9357479ef0..cca3573e31fb93fd6c4f8ee1b97c13c87b8860bc 100644 (file)
@@ -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