Skip to content

Commit

Permalink
Merge pull request #2205 from radarhere/master
Browse files Browse the repository at this point in the history
Fallback to mimetypes if imghdr is not available
  • Loading branch information
joshthecoder authored Jan 15, 2025
2 parents 91a41c6 + 336bb53 commit f6e7e23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
24 changes: 15 additions & 9 deletions tweepy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import contextlib
import functools
import imghdr
import logging
import mimetypes
from platform import python_version
Expand Down Expand Up @@ -3468,15 +3467,22 @@ def media_upload(self, filename, *, file=None, chunked=False,
----------
https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/overview
"""
h = None
if file is not None:
location = file.tell()
h = file.read(32)
file.seek(location)
file_type = imghdr.what(filename, h=h)
if file_type is not None:
file_type = 'image/' + file_type
file_type = None
try:
import imghdr
except ModuleNotFoundError:
# imghdr was removed in Python 3.13
pass
else:
h = None
if file is not None:
location = file.tell()
h = file.read(32)
file.seek(location)
file_type = imghdr.what(filename, h=h)
if file_type is not None:
file_type = 'image/' + file_type
if file_type is None:
file_type = mimetypes.guess_type(filename)[0]

if chunked or file_type.startswith('video/'):
Expand Down

0 comments on commit f6e7e23

Please sign in to comment.