diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index e890b6d..fc585ba 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 | ### |
@@ -895,8 +900,8 @@ class Mastodon: | |||
895 | if "status" in filter_object: | 900 | if "status" in filter_object: |
896 | filter_status = filter_object["status"] | 901 | filter_status = filter_object["status"] |
897 | filter_text = filter_status["content"] | 902 | filter_text = filter_status["content"] |
898 | filter_text = re.sub("<.*?>", " ", filter_text) | 903 | filter_text = re.sub(r"<.*?>", " ", filter_text) |
899 | filter_text = re.sub('\s+', ' ', filter_text).strip() | 904 | filter_text = re.sub(r"\s+", " ", filter_text).strip() |
900 | if not filter_re.search(filter_text): | 905 | if not filter_re.search(filter_text): |
901 | filter_results.append(filter_object) | 906 | filter_results.append(filter_object) |
902 | return filter_results | 907 | return filter_results |
@@ -1500,8 +1505,8 @@ class Mastodon: | |||
1500 | 1505 | ||
1501 | # Load avatar, if specified | 1506 | # Load avatar, if specified |
1502 | if not avatar is None: | 1507 | if not avatar is None: |
1503 | if avatar_mime_type is None and os.path.isfile(avatar): | 1508 | if avatar_mime_type is None and (isinstance(avatar, str) and os.path.isfile(avatar)): |
1504 | avatar_mime_type = mimetypes.guess_type(avatar)[0] | 1509 | avatar_mime_type = guess_type(avatar) |
1505 | avatar = open(avatar, 'rb') | 1510 | avatar = open(avatar, 'rb') |
1506 | 1511 | ||
1507 | if avatar_mime_type is None: | 1512 | if avatar_mime_type is None: |
@@ -1509,8 +1514,8 @@ class Mastodon: | |||
1509 | 1514 | ||
1510 | # Load header, if specified | 1515 | # Load header, if specified |
1511 | if not header is None: | 1516 | if not header is None: |
1512 | if header_mime_type is None and os.path.isfile(header): | 1517 | if header_mime_type is None and (isinstance(avatar, str) and os.path.isfile(header)): |
1513 | header_mime_type = mimetypes.guess_type(header)[0] | 1518 | header_mime_type = guess_type(header) |
1514 | header = open(header, 'rb') | 1519 | header = open(header, 'rb') |
1515 | 1520 | ||
1516 | if header_mime_type is None: | 1521 | if header_mime_type is None: |
@@ -1729,8 +1734,10 @@ class Mastodon: | |||
1729 | Returns a `media dict`_. This contains the id that can be used in | 1734 | Returns a `media dict`_. This contains the id that can be used in |
1730 | status_post to attach the media file to a toot. | 1735 | status_post to attach the media file to a toot. |
1731 | """ | 1736 | """ |
1732 | if mime_type is None and os.path.isfile(media_file): | 1737 | if mime_type is None and (isinstance(media_file, str) and os.path.isfile(media_file)): |
1733 | mime_type = mimetypes.guess_type(media_file)[0] | 1738 | mime_type = guess_type(media_file) |
1739 | media_file = open(media_file, 'rb') | ||
1740 | elif isinstance(media_file, str) and os.path.isfile(media_file): | ||
1734 | media_file = open(media_file, 'rb') | 1741 | media_file = open(media_file, 'rb') |
1735 | 1742 | ||
1736 | if mime_type is None: | 1743 | if mime_type is None: |
@@ -2539,3 +2546,11 @@ class MastodonRatelimitError(MastodonError): | |||
2539 | class MastodonMalformedEventError(MastodonError): | 2546 | class MastodonMalformedEventError(MastodonError): |
2540 | """Raised when the server-sent event stream is malformed""" | 2547 | """Raised when the server-sent event stream is malformed""" |
2541 | pass | 2548 | pass |
2549 | |||
2550 | def guess_type(media_file): | ||
2551 | mime_type = None | ||
2552 | if magic: | ||
2553 | mime_type = magic.from_file(media_file, mime=True) | ||
2554 | else: | ||
2555 | mime_type = mimetypes.guess_type(media_file)[0] | ||
2556 | return mime_type | ||