diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 19 |
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: | |||
31 | except ImportError: | 31 | except ImportError: |
32 | from urlparse import urlparse | 32 | from urlparse import urlparse |
33 | 33 | ||
34 | try: | ||
35 | import magic | ||
36 | except 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): | |||
2531 | class MastodonMalformedEventError(MastodonError): | 2536 | class 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 | |||
2540 | def 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 | ||