Merge pull request #428 from tushdante/tushdante-video-upload

added media_category and support for STATUS calls
This commit is contained in:
Mike Helmick 2016-09-30 07:28:38 -04:00 committed by GitHub
commit 866fb0202a

View file

@ -17,6 +17,7 @@ https://dev.twitter.com/docs/api/1.1
import os import os
import warnings import warnings
from io import BytesIO from io import BytesIO
from time import sleep
#try: #try:
#from StringIO import StringIO #from StringIO import StringIO
#except ImportError: #except ImportError:
@ -149,7 +150,7 @@ class EndpointsMixin(object):
return self.post('https://upload.twitter.com/1.1/media/upload.json', params=params) return self.post('https://upload.twitter.com/1.1/media/upload.json', params=params)
def upload_video(self, media, media_type, size=None): def upload_video(self, media, media_type, media_category=None, size=None, check_progress=False):
"""Uploads video file to Twitter servers in chunks. The file will be available to be attached """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 to a status for 60 minutes. To attach to a update, pass a list of returned media ids
to the 'update_status' method using the 'media_ids' param. to the 'update_status' method using the 'media_ids' param.
@ -174,7 +175,8 @@ class EndpointsMixin(object):
params = { params = {
'command': 'INIT', 'command': 'INIT',
'media_type': media_type, 'media_type': media_type,
'total_bytes': size 'total_bytes': size,
'media_category': media_category
} }
response_init = self.post(upload_url, params=params) response_init = self.post(upload_url, params=params)
media_id = response_init['media_id'] media_id = response_init['media_id']
@ -203,7 +205,38 @@ class EndpointsMixin(object):
'command': 'FINALIZE', 'command': 'FINALIZE',
'media_id': media_id 'media_id': media_id
} }
return self.post(upload_url, params=params)
response = self.post(upload_url, params=params)
# Only get the status if explicity asked to
# Default to False
if check_progress:
# Stage 4: STATUS call if still processing
params = {
'command': 'STATUS',
'media_id': media_id
}
# added code to handle if media_category is NOT set and check_progress=True
# the API will return a NoneType object in this case
try:
processing_state = response.get('processing_info').get('state')
except AttributeError:
return response
if processing_state:
while (processing_state == 'pending' or processing_state == 'in_progress') :
# get the secs to wait
check_after_secs = response.get('processing_info').get('check_after_secs')
if check_after_secs:
sleep(check_after_secs)
response = self.get(upload_url, params=params)
# get new state after waiting
processing_state = response.get('processing_info').get('state')
return response
def get_oembed_tweet(self, **params): def get_oembed_tweet(self, **params):
"""Returns information allowing the creation of an embedded """Returns information allowing the creation of an embedded