Merge branch 'mr-origin-1' into 'master'

Example refactor  for`compose_tweet` method.

See merge request spla/mastotuit!2
This commit is contained in:
spla 2021-10-19 13:42:12 +00:00
commit d344117604

Veure arxiu

@ -51,110 +51,87 @@ def get_toot(title):
return tuit_text return tuit_text
def compose_tweet(tuit_text, with_images, is_reply): def compose_tweet(tuit_text, with_images, is_reply):
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)
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 media_upload.processing_info['state'] == 'succeeded':
images_ids.append(media_upload.media_id)
except TweepyException as err:
print('Error while uploading media!\n')
sys.exit(err)
# Compose tuit
tuit_text2 = ''
if len(tuit_text) > 280: if len(tuit_text) > 280:
tuit_max_length = 250 if with_images else 275
if with_images: 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?
)
tuit_text1 = tuit_text[:250].rsplit(' ', 1)[0] + ' (1/2)' try:
tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' 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 ''
)
else: tweet = api.update_status(
status=tuit_text2,
in_reply_to_status_id=first_tweet.id,
media_ids=images_ids
)
tuit_text1 = tuit_text[:275].rsplit(' ', 1)[0] + ' (1/2)' except TweepyException as err:
tuit_text2 = tuit_text[int(len(tuit_text1)-6):] + ' (2/2)' print('Error while trying to publish split tweet.\n')
sys.exit(err)
try:
if with_images: 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)
images_id_lst = [] return tweet
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): def get_tweet_id(toot_id):