Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add upload_media_with_file function. #398

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions twython/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def upload_media(self, **params):
"""
return self.post('https://upload.twitter.com/1.1/media/upload.json', params=params)

def upload_media_with_file(self, filename):
with open(filename, 'rb') as f:
return self.upload_media(media=f)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do something like this?

content = None
_file = open(filename, 'rb')
with _file:
    content = self.upload_media(media=_file)

_file.close()
return content

Other versions of Python (not CPython) don't automatically close references.

See http://stackoverflow.com/questions/19039704/is-python-retaining-a-reference-to-a-file-opened-in-a-list


def upload_video(self, media, media_type, size=None):
"""Uploads video file to Twitter servers in chunks. The file will be available to be attached
to a status for 60 minutes. To attach to a update, pass a list of returned media ids
Expand Down