Example refactor forcompose_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!
This commit is contained in:
Oscar Domingo 2021-10-16 13:47:21 +00:00
pare af5a373bcd
commit e2588a6ecc

Veure arxiu

@ -52,110 +52,89 @@ 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:
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)'
else:
tuit_text1 = tuit_text[:275].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
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'
)
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
if media_upload.processing_info['state'] == 'succeeded':
images_ids.append(media_upload.media_id)
except TweepyException as err:
print('\n')
print('Error while uploading media!\n')
sys.exit(err)
# Compose tuit
# 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?
)
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 ''
)
tweet = api.update_status(
status=tuit_text2,
in_reply_to_status_id=first_tweet.id,
media_ids=images_ids
)
except TweepyException as err:
print('Error while trying to publish split tweet.\n')
sys.exit(err)
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)
return tweet
def get_tweet_id(toot_id):
tweet_id = 0