From e2588a6ecc20e68d4d063e2911cdb52fff0eccef Mon Sep 17 00:00:00 2001 From: Oscar Domingo Date: Sat, 16 Oct 2021 13:47:21 +0000 Subject: [PATCH 1/2] Example refactor for`compose_tweet` method. This commit was made after seeing the following toot: https://mastodon.social/web/statuses/107105301232594830 And wondering if it could be improved, this is my take on it, feel free to discard if not wanted/needed! --- mastotuit.py | 159 ++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 90 deletions(-) diff --git a/mastotuit.py b/mastotuit.py index 98e8cce..3a03ed5 100644 --- a/mastotuit.py +++ b/mastotuit.py @@ -52,109 +52,88 @@ def get_toot(title): def compose_tweet(tuit_text, with_images, is_reply): - if len(tuit_text) > 280: + tuit_max_length = 250 if len(tuit_text) > 280 else 275 + tuit_text2 = '' + images_ids = [] - if with_images: + if with_images: + for image in images_list: + file_ = filetype.guess('images/' + image) + is_video = True if file_.mime == 'video/mp4' else False + # It's a video, let's do some processing... + if is_video: + try: + ffmpeg_probe = ffmpeg.probe(f'images/{image}') + video_duration = float( + ffmpeg_probe['streams'][0]['duration'] + ) + except Exception as e: + print(f'Error while trying to probe {image}\n{e}') + sys.exit(e) - tuit_text1 = tuit_text[:250].rsplit(' ', 1)[0] + ' (1/2)' - tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' + if video_duration > 139: + print(f'video duration is too large: {video_duration}') + # We could potentially use ffmpeg to shorten the video + # We skip to the next image + continue - else: + try: + # Now let's uplaod the media... + media_upload = api.media_upload( + f'images/{image}', + media_category='tweet_video' if is_video else'tweet_image' + ) - tuit_text1 = tuit_text[:275].rsplit(' ', 1)[0] + ' (1/2)' - tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' + if media_upload.processing_info['state'] == 'succeeded': + images_ids.append(media_upload.media_id) - try: + except TweepyException as err: + print('Error while uploading media!\n') + sys.exit(err) - if with_images: + # Compose tuit - images_id_lst = [] + # This means that the OG tweet is bigger than 280 + # Thus we need to break it down in two + if tuit_max_length == 250: + tuit_text = '{0} (1/2)'.format( + tuit_text[:tuit_max_length].rsplit(' ', 1)[0] + ) + tuit_text2 = '{0} (2/2)'.format( + tuit_text[len(tuit_text) - 6:] # Why minus 6? + ) - i = 0 - while i < len(images_list): + try: + first_tweet = api.update_status( + status=tuit_text, + # No idea where `tweet_id` is defined/coming from + in_reply_to_status_id=tweet_id if is_reply else '' + ) - kind = filetype.guess('images/' + images_list[i]) - if kind.mime == 'video/mp4': + tweet = api.update_status( + status=tuit_text2, + in_reply_to_status_id=first_tweet.id, + media_ids=images_ids + ) - probe = ffmpeg.probe('images/' + images_list[i]) - duration = probe['streams'][0]['duration'] + except TweepyException as err: + print('Error while trying to publish split tweet.\n') + sys.exit(err) - if float(duration) > 139: - print(f'video duration is too large: {duration}') + else: + try: + tweet = api.update_status( + status=tuit_text, + # No idea where `tweet_id` is defined/coming from + in_reply_to_status_id=tweet_id if is_reply else '', + media_ids=images_ids # defaults to empty list + ) + except TweepyException as err: + print('Errror while trying to publish tweet.\n') + sys.exit(err) - else: - - media = api.media_upload('images/' + images_list[i], media_category='tweet_video') - - if media.processing_info['state'] == 'succeeded': - - images_id_lst.append(media.media_id) - - else: - - print(media.processing_info) - - else: - - media = api.media_upload('images/' + images_list[i]) - - images_id_lst.append(media.media_id) - - i += 1 - - if len(tuit_text) > 280: - - if is_reply: - - first_tweet = api.update_status(status=tuit_text1, in_reply_to_status_id=tweet_id) - - else: - - first_tweet = api.update_status(status=tuit_text1) - - tweet = api.update_status(status=tuit_text2, in_reply_to_status_id=first_tweet.id, media_ids=images_id_lst) - - else: - - if is_reply: - - tweet = api.update_status(status=tuit_text, in_reply_to_status_id=tweet_id, media_ids=images_id_lst) - - else: - - tweet = api.update_status(status=tuit_text, media_ids=images_id_lst) - - else: - - if len(tuit_text) > 280: - - if is_reply: - - first_tweet = api.update_status(tuit_text1, in_reply_to_status_id=tweet_id) - - else: - - first_tweet = api.update_status(tuit_text1) - - tweet = api.update_status(tuit_text2, in_reply_to_status_id=first_tweet.id) - - else: - - if is_reply: - - tweet = api.update_status(status=tuit_text, in_reply_to_status_id=tweet_id) - - else: - - tweet = api.update_status(tuit_text) - - return tweet - - except TweepyException as err: - - print('\n') - sys.exit(err) + return tweet def get_tweet_id(toot_id): From 1d8fb197310ed12c0c5ccab6c889e224a5bd079b Mon Sep 17 00:00:00 2001 From: Oscar Domingo Date: Sat, 16 Oct 2021 13:58:41 +0000 Subject: [PATCH 2/2] Fixup Fixed slightly wrong logic. --- mastotuit.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mastotuit.py b/mastotuit.py index 3a03ed5..b036e08 100644 --- a/mastotuit.py +++ b/mastotuit.py @@ -51,9 +51,6 @@ def get_toot(title): return tuit_text def compose_tweet(tuit_text, with_images, is_reply): - - tuit_max_length = 250 if len(tuit_text) > 280 else 275 - tuit_text2 = '' images_ids = [] if with_images: @@ -92,10 +89,11 @@ def compose_tweet(tuit_text, with_images, is_reply): sys.exit(err) # Compose tuit + tuit_text2 = '' + + if len(tuit_text) > 280: + tuit_max_length = 250 if with_images else 275 - # This means that the OG tweet is bigger than 280 - # Thus we need to break it down in two - if tuit_max_length == 250: tuit_text = '{0} (1/2)'.format( tuit_text[:tuit_max_length].rsplit(' ', 1)[0] )