diff --git a/README.md b/README.md index fe01146..e40fd6b 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,6 @@ Within Python Virtual Environment: 5. Use your favourite scheduling method to set `python mastotuit.py` to run every minute. 29.9.2021 **New Feature** Added support to media files! mastotuit now gets all media files from Mastodon's post (if any) and publish them to Twitter together with your status update. -7.10.2021 **New Feature** Added thread support! If you create a thread in Mastodon mastotuit will create the same thread on Twitter. +7.10.2021 **New Feature** Added thread support! If you create a thread in Mastodon mastotuit will create the same thread on Twitter. +13.10.2021 Upgraded Tweepy library to v4.1.0 +13.10.2021 **New Feature** Added video upload support! If video properties are according Twitter rules it will be uploaded. diff --git a/mastotuit.py b/mastotuit.py index 5160843..9f44f4f 100644 --- a/mastotuit.py +++ b/mastotuit.py @@ -8,8 +8,10 @@ import time import requests import shutil import tweepy -from tweepy import TweepError +from tweepy import TweepyException import logging +import filetype +import ffmpeg import pdb logger = logging.getLogger() @@ -98,8 +100,7 @@ def create_api(): auth = tweepy.OAuthHandler(api_key, api_key_secret) auth.set_access_token(access_token, access_token_secret) - api = tweepy.API(auth, wait_on_rate_limit=True, - wait_on_rate_limit_notify=True) + api = tweepy.API(auth) try: api.verify_credentials() logged_in = True @@ -278,8 +279,40 @@ if __name__ == '__main__': i = 0 while i < len(images_list): - media = api.media_upload('images/' + images_list[i]) - images_id_lst.append(media.media_id) + 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: @@ -300,7 +333,7 @@ if __name__ == '__main__': tweet = api.update_status(tuit_text) - except TweepError as err: + except TweepyException as err: print('\n') sys.exit(err) @@ -326,8 +359,34 @@ if __name__ == '__main__': i = 0 while i < len(images_list): - media = api.media_upload('images/' + images_list[i]) + 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: @@ -352,7 +411,7 @@ if __name__ == '__main__': tweet = api.update_status(tuit_text2, in_reply_to_status_id=first_tweet.id) - except TweepError as err: + except TweepException as err: print('\n') sys.exit(err) diff --git a/requirements.txt b/requirements.txt index c3aa8ae..511d650 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,6 @@ psycopg2>=2.9.1 feedparser>=6.0.8 bs4>=0.0.1 Mastodon.py>=1.5.1 -tweepy==3.10.0 +tweepy==4.1.0 +filetype>=1.0.8 +ffmpeg-python>=0.2.0