Merge pull request #150 from jfmcbrayer/mime-magic
Use python-magic to determine mime types from contents
This commit is contained in:
commit
ae9640e95d
S'han modificat 3 arxius amb 21 adicions i 5 eliminacions
4
.gitignore
vendido
4
.gitignore
vendido
|
@ -91,4 +91,6 @@ ENV/
|
||||||
# Secret files (for credentials used in testing)
|
# Secret files (for credentials used in testing)
|
||||||
*.secret
|
*.secret
|
||||||
pytooter_clientcred.txt
|
pytooter_clientcred.txt
|
||||||
pytooter_usercred.txt
|
pytooter_usercred.txtPipfile
|
||||||
|
Pipfile.lock
|
||||||
|
Pipfile
|
||||||
|
|
|
@ -31,6 +31,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
|
||||||
|
try:
|
||||||
|
import magic
|
||||||
|
except ImportError:
|
||||||
|
magic = None
|
||||||
|
|
||||||
###
|
###
|
||||||
# Version check functions, including decorator and parser
|
# Version check functions, including decorator and parser
|
||||||
###
|
###
|
||||||
|
@ -1491,7 +1496,7 @@ class Mastodon:
|
||||||
# Load avatar, if specified
|
# Load avatar, if specified
|
||||||
if not avatar is None:
|
if not avatar is None:
|
||||||
if avatar_mime_type is None and os.path.isfile(avatar):
|
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')
|
avatar = open(avatar, 'rb')
|
||||||
|
|
||||||
if avatar_mime_type is None:
|
if avatar_mime_type is None:
|
||||||
|
@ -1500,7 +1505,7 @@ class Mastodon:
|
||||||
# Load header, if specified
|
# Load header, if specified
|
||||||
if not header is None:
|
if not header is None:
|
||||||
if header_mime_type is None and os.path.isfile(header):
|
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')
|
header = open(header, 'rb')
|
||||||
|
|
||||||
if header_mime_type is None:
|
if header_mime_type is None:
|
||||||
|
@ -1720,7 +1725,7 @@ class Mastodon:
|
||||||
status_post to attach the media file to a toot.
|
status_post to attach the media file to a toot.
|
||||||
"""
|
"""
|
||||||
if mime_type is None and os.path.isfile(media_file):
|
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')
|
media_file = open(media_file, 'rb')
|
||||||
elif mime_type and os.path.isfile(media_file):
|
elif mime_type and os.path.isfile(media_file):
|
||||||
media_file = open(media_file, 'rb')
|
media_file = open(media_file, 'rb')
|
||||||
|
@ -2531,3 +2536,11 @@ class MastodonRatelimitError(MastodonError):
|
||||||
class MastodonMalformedEventError(MastodonError):
|
class MastodonMalformedEventError(MastodonError):
|
||||||
"""Raised when the server-sent event stream is malformed"""
|
"""Raised when the server-sent event stream is malformed"""
|
||||||
pass
|
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
|
||||||
|
|
3
setup.py
3
setup.py
|
@ -13,7 +13,8 @@ setup(name='Mastodon.py',
|
||||||
'requests',
|
'requests',
|
||||||
'python-dateutil',
|
'python-dateutil',
|
||||||
'six',
|
'six',
|
||||||
'pytz',
|
'pytz',
|
||||||
|
'python-magic',
|
||||||
'decorator>=4.0.0',
|
'decorator>=4.0.0',
|
||||||
'http_ece>=1.0.5',
|
'http_ece>=1.0.5',
|
||||||
'cryptography>=1.6.0'
|
'cryptography>=1.6.0'
|
||||||
|
|
Loading…
Referencia en una nova incidència