Code optimization, removed 40 lines

This commit is contained in:
spla 2021-10-15 13:15:52 +02:00
pare ddda1fbee9
commit 979d24984b

Veure arxiu

@ -50,6 +50,112 @@ def get_toot(title):
return tuit_text 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): def get_tweet_id(toot_id):
tweet_id = 0 tweet_id = 0
@ -177,7 +283,6 @@ def get_parameter( parameter, file_path ):
if __name__ == '__main__': if __name__ == '__main__':
#######################################################################
mastodon, mastodon_hostname = mastodon() mastodon, mastodon_hostname = mastodon()
feeds_db, feeds_db_user, feeds_url = db_config() feeds_db, feeds_db_user, feeds_url = db_config()
@ -268,153 +373,7 @@ if __name__ == '__main__':
api, logged_in = create_api() api, logged_in = create_api()
if len(tuit_text) < 280: tweet = compose_tweet(tuit_text, with_images, is_reply)
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)
######################################################### #########################################################