python-twitter icon indicating copy to clipboard operation
python-twitter copied to clipboard

Unable to specify media type with UploadMedia*()

Open jd opened this issue 9 years ago • 4 comments
trafficstars

parse_media_files uses mimetypes.guess_type so it can't work if your image does not ends up with a correct extension on the external server.

Maybe using the returned Content-Type would be useful too.

jd avatar Oct 14 '16 12:10 jd

So like http://example.com/hello.jpe gets rejected or is this something else?

jeremylow avatar Oct 21 '16 13:10 jeremylow

@jeremylow Yes.

jd avatar Oct 21 '16 14:10 jd

Yes, mimetypes.guess_type relies on file extension. Why don't we use python-magic or filetype?

BennyThink avatar Dec 01 '20 04:12 BennyThink

filetype is really simple to use, and it's totally compatible


import filetype

def main():
    kind = filetype.guess('tests/fixtures/sample.jpg')
    if kind is None:
        print('Cannot guess file type!')
        return

    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime)

if __name__ == '__main__':
    main()

BennyThink avatar Dec 01 '20 05:12 BennyThink