From 7e2259e3ee5984bbcc27c4810cad301f96106e12 Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Mon, 12 Nov 2018 09:44:57 -0500 Subject: Add requirement for python-magic, and add wrappers to Mastodon.py --- .gitignore | 4 +++- mastodon/Mastodon.py | 13 +++++++++++++ setup.py | 3 ++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e146528..66273be 100644 --- a/.gitignore +++ b/.gitignore @@ -91,4 +91,6 @@ ENV/ # Secret files (for credentials used in testing) *.secret pytooter_clientcred.txt -pytooter_usercred.txt \ No newline at end of file +pytooter_usercred.txtPipfile +Pipfile.lock +Pipfile diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index e6d8268..a2035c9 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -31,6 +31,11 @@ try: except ImportError: from urlparse import urlparse +try: + import magic +except ImportError: + magic = None + ### # Version check functions, including decorator and parser ### @@ -2529,3 +2534,11 @@ class MastodonRatelimitError(MastodonError): class MastodonMalformedEventError(MastodonError): """Raised when the server-sent event stream is malformed""" pass + +def guess_type(media_file): + mime_type = None + if magic: + mime_type = magic.from_file(media_file, mime=True) + else: + mime_type = mimetypes.guess_type(media_file)[0] + return mime_type diff --git a/setup.py b/setup.py index e49c26c..009063d 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,8 @@ setup(name='Mastodon.py', 'requests', 'python-dateutil', 'six', - 'pytz', + 'pytz', + 'python-magic', 'decorator>=4.0.0', 'http_ece>=1.0.5', 'cryptography>=1.6.0' -- cgit v1.2.3 From 4b7bbe72cd58487612543543fec6dba6c6fe0566 Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Mon, 12 Nov 2018 10:04:15 -0500 Subject: Use magic for guessing mime type if available Replace uses of mimetypes.guess_type with guess_type wrapper that uses magic if available, and falls back to mimetypes if not. --- mastodon/Mastodon.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index a2035c9..c8dc6e4 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1496,7 +1496,7 @@ class Mastodon: # Load avatar, if specified if not avatar is None: if avatar_mime_type is None and os.path.isfile(avatar): - avatar_mime_type = mimetypes.guess_type(avatar)[0] + avatar_mime_type = guess_type(avatar) avatar = open(avatar, 'rb') if avatar_mime_type is None: @@ -1505,7 +1505,7 @@ class Mastodon: # Load header, if specified if not header is None: if header_mime_type is None and os.path.isfile(header): - header_mime_type = mimetypes.guess_type(header)[0] + header_mime_type = guess_type(header) header = open(header, 'rb') if header_mime_type is None: @@ -1725,7 +1725,7 @@ class Mastodon: status_post to attach the media file to a toot. """ if mime_type is None and os.path.isfile(media_file): - mime_type = mimetypes.guess_type(media_file)[0] + mime_type = guess_type(media_file) media_file = open(media_file, 'rb') if mime_type is None: -- cgit v1.2.3