Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
replace mimetypes to filetype
Browse files Browse the repository at this point in the history
mimetypes relies on file extension, while filetype relies on file header which is more stable.
  • Loading branch information
BennyThink committed Dec 1, 2020
1 parent acd9472 commit 913644a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests
requests-oauthlib
filetype
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def extract_metaitem(meta):
download_url=extract_metaitem('download_url'),
packages=find_packages(exclude=('tests', 'docs')),
platforms=['Any'],
install_requires=['requests', 'requests-oauthlib'],
install_requires=['requests', 'requests-oauthlib', 'filetype'],
setup_requires=['pytest-runner'],
tests_require=['pytest'],
keywords='twitter api',
Expand Down
7 changes: 2 additions & 5 deletions twitter/twitter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Collection of utilities for use in API calls, functions."""
from __future__ import unicode_literals

import mimetypes
import filetype
import os
import re
import sys
Expand Down Expand Up @@ -274,10 +274,7 @@ def parse_media_file(passed_media, async_upload=False):
except Exception as e:
pass

media_type = mimetypes.guess_type(os.path.basename(filename))[0]
# The .srt extension is not recognised by the mimetypes module.
if os.path.basename(filename).endswith('.srt'):
media_type = 'text/srt'
media_type = filetype.guess_mime(data_file.name)
if media_type is not None:
if media_type in img_formats and file_size > 5 * 1048576:
raise TwitterError({'message': 'Images must be less than 5MB.'})
Expand Down

0 comments on commit 913644a

Please sign in to comment.