aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2018-11-26 11:24:35 +0100
committerGitHub <[email protected]>2018-11-26 11:24:35 +0100
commitae9640e95d2cfa43e1a35d8281fe38a671fdc71d (patch)
tree059a01f007af648aae42188d0b2e5b793f008f52 /mastodon
parent62bf6afd9fbffc1d47c00eb496a03b3598c66ed3 (diff)
parent4b7bbe72cd58487612543543fec6dba6c6fe0566 (diff)
downloadmastodon.py-ae9640e95d2cfa43e1a35d8281fe38a671fdc71d.tar.gz
Merge pull request #150 from jfmcbrayer/mime-magic
Use python-magic to determine mime types from contents
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 7f0f0fd..a5c05b7 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -31,6 +31,11 @@ try:
31except ImportError: 31except ImportError:
32 from urlparse import urlparse 32 from urlparse import urlparse
33 33
34try:
35 import magic
36except ImportError:
37 magic = None
38
34### 39###
35# Version check functions, including decorator and parser 40# Version check functions, including decorator and parser
36### 41###
@@ -1491,7 +1496,7 @@ class Mastodon:
1491 # Load avatar, if specified 1496 # Load avatar, if specified
1492 if not avatar is None: 1497 if not avatar is None:
1493 if avatar_mime_type is None and os.path.isfile(avatar): 1498 if avatar_mime_type is None and os.path.isfile(avatar):
1494 avatar_mime_type = mimetypes.guess_type(avatar)[0] 1499 avatar_mime_type = guess_type(avatar)
1495 avatar = open(avatar, 'rb') 1500 avatar = open(avatar, 'rb')
1496 1501
1497 if avatar_mime_type is None: 1502 if avatar_mime_type is None:
@@ -1500,7 +1505,7 @@ class Mastodon:
1500 # Load header, if specified 1505 # Load header, if specified
1501 if not header is None: 1506 if not header is None:
1502 if header_mime_type is None and os.path.isfile(header): 1507 if header_mime_type is None and os.path.isfile(header):
1503 header_mime_type = mimetypes.guess_type(header)[0] 1508 header_mime_type = guess_type(header)
1504 header = open(header, 'rb') 1509 header = open(header, 'rb')
1505 1510
1506 if header_mime_type is None: 1511 if header_mime_type is None:
@@ -1720,7 +1725,7 @@ class Mastodon:
1720 status_post to attach the media file to a toot. 1725 status_post to attach the media file to a toot.
1721 """ 1726 """
1722 if mime_type is None and os.path.isfile(media_file): 1727 if mime_type is None and os.path.isfile(media_file):
1723 mime_type = mimetypes.guess_type(media_file)[0] 1728 mime_type = guess_type(media_file)
1724 media_file = open(media_file, 'rb') 1729 media_file = open(media_file, 'rb')
1725 elif mime_type and os.path.isfile(media_file): 1730 elif mime_type and os.path.isfile(media_file):
1726 media_file = open(media_file, 'rb') 1731 media_file = open(media_file, 'rb')
@@ -2531,3 +2536,11 @@ class MastodonRatelimitError(MastodonError):
2531class MastodonMalformedEventError(MastodonError): 2536class MastodonMalformedEventError(MastodonError):
2532 """Raised when the server-sent event stream is malformed""" 2537 """Raised when the server-sent event stream is malformed"""
2533 pass 2538 pass
2539
2540def guess_type(media_file):
2541 mime_type = None
2542 if magic:
2543 mime_type = magic.from_file(media_file, mime=True)
2544 else:
2545 mime_type = mimetypes.guess_type(media_file)[0]
2546 return mime_type
Powered by cgit v1.2.3 (git 2.41.0)