Added suggested refactor from Minkiu

This commit is contained in:
spla 2021-10-19 15:51:21 +02:00
pare d344117604
commit 3ebc539285

Veure arxiu

@ -51,40 +51,54 @@ def get_toot(title):
return tuit_text
def compose_tweet(tuit_text, with_images, is_reply):
images_ids = []
images_id_lst = []
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...
kind = filetype.guess('images/' + image)
is_video = True if kind.mime == 'video/mp4' else False
if is_video:
try:
ffmpeg_probe = ffmpeg.probe(f'images/{image}')
probe = ffmpeg.probe(f'images/{image}')
video_duration = float(
ffmpeg_probe['streams'][0]['duration']
probe['streams'][0]['duration']
)
except Exception as e:
print(f'Error while trying to probe {image}\n{e}')
sys.exit(e)
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 is_video:
if media_upload.processing_info['state'] == 'succeeded':
images_ids.append(media_upload.media_id)
images_id_lst.append(media_upload.media_id)
else:
images_id_lst.append(media_upload.media_id)
except TweepyException as err:
print('Error while uploading media!\n')
sys.exit(err)
@ -92,43 +106,47 @@ def compose_tweet(tuit_text, with_images, is_reply):
tuit_text2 = ''
if len(tuit_text) > 280:
tuit_max_length = 250 if with_images else 275
tuit_text = '{0} (1/2)'.format(
tuit_text1 = '{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?
tuit_text[len(tuit_text1) - 6:]
)
try:
first_tweet = api.update_status(
status=tuit_text,
# No idea where `tweet_id` is defined/coming from
status=tuit_text1,
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
media_ids=images_id_lst
)
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
media_ids=images_id_lst
)
except TweepyException as err:
print('Errror while trying to publish tweet.\n')
print('Error while trying to publish tweet.\n')
sys.exit(err)
return tweet