From 979d24984b456c32524418b43b5edee7ad21ccd7 Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 15 Oct 2021 13:15:52 +0200 Subject: [PATCH] Code optimization, removed 40 lines --- mastotuit.py | 255 +++++++++++++++++++++------------------------------ 1 file changed, 107 insertions(+), 148 deletions(-) diff --git a/mastotuit.py b/mastotuit.py index 9f44f4f..73f0da9 100644 --- a/mastotuit.py +++ b/mastotuit.py @@ -50,6 +50,112 @@ def get_toot(title): return tuit_text +def compose_tweet(tuit_text, with_images, is_reply): + + if len(tuit_text) > 280: + + if with_images: + + tuit_text1 = tuit_text[:250].rsplit(' ', 1)[0] + ' (1/2)' + tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' + + else: + + tuit_text1 = tuit_text[:275].rsplit(' ', 1)[0] + ' (1/2)' + tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' + + try: + + if with_images: + + images_id_lst = [] + + i = 0 + while i < len(images_list): + + kind = filetype.guess('images/' + images_list[i]) + if kind.mime == 'video/mp4': + + probe = ffmpeg.probe('images/' + images_list[i]) + duration = probe['streams'][0]['duration'] + + if float(duration) > 139: + + print(f'video duration is too large: {duration}') + + 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) + def get_tweet_id(toot_id): tweet_id = 0 @@ -177,7 +283,6 @@ def get_parameter( parameter, file_path ): if __name__ == '__main__': - ####################################################################### mastodon, mastodon_hostname = mastodon() feeds_db, feeds_db_user, feeds_url = db_config() @@ -268,153 +373,7 @@ if __name__ == '__main__': api, logged_in = create_api() - if len(tuit_text) < 280: - - try: - - if with_images: - - images_id_lst = [] - - i = 0 - while i < len(images_list): - - kind = filetype.guess('images/' + images_list[i]) - if kind.mime == 'video/mp4': - - probe = ffmpeg.probe('images/' + images_list[i]) - duration = probe['streams'][0]['duration'] - - if float(duration) > 139: - - print(f'video duration is too large: {duration}') - - 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]) - - if media.processing_info['state'] == 'succeeded': - - images_id_lst.append(media.media_id) - - else: - - print(media.processing_info) - - i += 1 - - 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 is_reply: - - tweet = api.update_status(status=tuit_text, in_reply_to_status_id=tweet_id) - - else: - - tweet = api.update_status(tuit_text) - - except TweepyException as err: - - print('\n') - sys.exit(err) - - else: - - if with_images: - - tuit_text1 = tuit_text[:250].rsplit(' ', 1)[0] + ' (1/2)' - tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' - - else: - - tuit_text1 = tuit_text[:275].rsplit(' ', 1)[0] + ' (1/2)' - tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' - - try: - - if with_images: - - images_id_lst = [] - - i = 0 - while i < len(images_list): - - kind = filetype.guess('images/' + images_list[i]) - if kind.mime == 'video/mp4': - - probe = ffmpeg.probe('images/' + images_list[i]) - duration = probe['streams'][0]['duration'] - - if float(duration) > 139: - - print(f'video duration is too large: {duration}') - - 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 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: - - 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) - - except TweepException as err: - - print('\n') - sys.exit(err) + tweet = compose_tweet(tuit_text, with_images, is_reply) #########################################################