From 56a89ec4f3b2f45f4891e09da3e026615e9f70cc Mon Sep 17 00:00:00 2001 From: JC Francois Date: Thu, 7 Nov 2019 20:06:15 +0100 Subject: [PATCH] Added tolerance for invalid media links --- twoot.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/twoot.py b/twoot.py index 763e0e5..f0575af 100755 --- a/twoot.py +++ b/twoot.py @@ -308,19 +308,20 @@ def main(argv): # Upload photos media_ids = [] for photo in tweet['photos']: + media = False # Download picture - media = requests.get(photo) + try: + media = requests.get(photo) + except: + pass # Upload picture to Mastodon instance - try: - media_posted = mastodon.media_post(media.content, mime_type=media.headers['content-type']) - media_ids.append(media_posted['id']) - except MastodonAPIError: # Media cannot be uploaded (invalid format, dead link, etc.) - pass - except MastodonIllegalArgumentError: # Could not determine mime type of content - pass - except TypeError: - pass + if media: + try: + media_posted = mastodon.media_post(media.content, mime_type=media.headers['content-type']) + media_ids.append(media_posted['id']) + except (MastodonAPIError, MastodonIllegalArgumentError, TypeError): # Media cannot be uploaded (invalid format, dead link, etc.) + pass # Post toot try: