added media_category and support for STATUS calls #428

Merged
tushdante merged 4 commits from tushdante-video-upload into master 2016-09-30 04:28:38 -07:00

View file

@ -17,6 +17,7 @@ https://dev.twitter.com/docs/api/1.1
import os
import warnings
from io import BytesIO
from time import sleep
#try:
#from StringIO import StringIO
#except ImportError:
@ -145,7 +146,7 @@ class EndpointsMixin(object):
"""
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
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.
@ -170,7 +171,8 @@ class EndpointsMixin(object):
params = {
'command': 'INIT',
'media_type': media_type,
'total_bytes': size
'total_bytes': size,
'media_category': media_category
}
response_init = self.post(upload_url, params=params)
media_id = response_init['media_id']
@ -199,7 +201,38 @@ class EndpointsMixin(object):
'command': 'FINALIZE',
'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:
michaelhelmick commented 2016-09-23 06:23:26 -07:00 (Migrated from github.com)

Instead of changing the return here, can you add a keyword argument to upload_video like check_progress=False and if they pass check_progress=True, it will execute the code you added, otherwise it will just return the response.

Instead of changing the return here, can you add a keyword argument to `upload_video` like `check_progress=False` and if they pass `check_progress=True`, it will execute the code you added, otherwise it will just return the response.
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):
"""Returns information allowing the creation of an embedded