From 2c02b622a2cf8db5a2f060b3cb5672ff4e88d189 Mon Sep 17 00:00:00 2001 From: Tushar Bhushan Date: Wed, 14 Sep 2016 15:35:51 -0700 Subject: [PATCH 1/4] added media_category and support for STATUS calls --- twython/endpoints.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/twython/endpoints.py b/twython/endpoints.py index 9fe7734..8814fee 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -199,7 +199,28 @@ class EndpointsMixin(object): 'command': 'FINALIZE', 'media_id': media_id } - return self.post(upload_url, params=params) + response_finalize = self.post(upload_url, params=params) + + # Stage 4: STATUS call if still processing + params = { + 'command': 'STATUS', + 'media_id': media_id + } + + processing_state = response_finalize['processing_info'].get('state', None) + + if processing_state is not None: + while (processing_state == "pending" or processing_state == "in_progress") : + # get the secs to wait + check_after_secs = response_finalize['processing_info'].get('check_after_secs', None) + + if check_after_secs is not None: + time.sleep(check_after_secs) + response_finalize = self.get(upload_url, params=params) + # get new state after waiting + processing_state = response_finalize['processing_info'].get('state') + + return response_finalize def get_oembed_tweet(self, **params): """Returns information allowing the creation of an embedded From 7401adfb640e50021cab7db2041f13a21ce6bad2 Mon Sep 17 00:00:00 2001 From: Tushar Bhushan Date: Sun, 25 Sep 2016 18:03:26 -0700 Subject: [PATCH 2/4] - Double -> single quotes - Removed default value from .get() - Added a check_progress param to upload_video to allow users to decide when to check 'STATUS' calls --- twython/endpoints.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/twython/endpoints.py b/twython/endpoints.py index 8814fee..33d4b00 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -145,7 +145,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, 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. @@ -199,28 +199,33 @@ class EndpointsMixin(object): 'command': 'FINALIZE', 'media_id': media_id } - response_finalize = self.post(upload_url, params=params) - # Stage 4: STATUS call if still processing - params = { - 'command': 'STATUS', - 'media_id': media_id - } - - processing_state = response_finalize['processing_info'].get('state', None) + # Only get the status if explicity asked to + # Default to False + if check_progress == True: - if processing_state is not None: - while (processing_state == "pending" or processing_state == "in_progress") : - # get the secs to wait - check_after_secs = response_finalize['processing_info'].get('check_after_secs', None) + response_finalize = self.post(upload_url, params=params) - if check_after_secs is not None: - time.sleep(check_after_secs) - response_finalize = self.get(upload_url, params=params) - # get new state after waiting - processing_state = response_finalize['processing_info'].get('state') + # Stage 4: STATUS call if still processing + params = { + 'command': 'STATUS', + 'media_id': media_id + } + + processing_state = response_finalize['processing_info'].get('state') - return response_finalize + if processing_state is not None: + while (processing_state == 'pending' or processing_state == 'in_progress') : + # get the secs to wait + check_after_secs = response_finalize['processing_info'].get('check_after_secs', None) + + if check_after_secs is not None: + time.sleep(check_after_secs) + response_finalize = self.get(upload_url, params=params) + # get new state after waiting + processing_state = response_finalize['processing_info'].get('state') + + return self.post(upload_url, param=params) def get_oembed_tweet(self, **params): """Returns information allowing the creation of an embedded From e76a290166884cebfffaca30bb847b00b1b2b29e Mon Sep 17 00:00:00 2001 From: Tushar Bhushan Date: Mon, 26 Sep 2016 12:51:05 -0700 Subject: [PATCH 3/4] fixed styling issues --- twython/endpoints.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/twython/endpoints.py b/twython/endpoints.py index 33d4b00..24ddcb9 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -200,11 +200,11 @@ class EndpointsMixin(object): 'media_id': media_id } + response = self.post(upload_url, params=params) + # Only get the status if explicity asked to # Default to False - if check_progress == True: - - response_finalize = self.post(upload_url, params=params) + if check_progress: # Stage 4: STATUS call if still processing params = { @@ -212,20 +212,20 @@ class EndpointsMixin(object): 'media_id': media_id } - processing_state = response_finalize['processing_info'].get('state') + processing_state = response['processing_info'].get('state') if processing_state is not None: while (processing_state == 'pending' or processing_state == 'in_progress') : # get the secs to wait - check_after_secs = response_finalize['processing_info'].get('check_after_secs', None) + check_after_secs = response['processing_info'].get('check_after_secs') if check_after_secs is not None: time.sleep(check_after_secs) - response_finalize = self.get(upload_url, params=params) + response = self.get(upload_url, params=params) # get new state after waiting - processing_state = response_finalize['processing_info'].get('state') + processing_state = response['processing_info'].get('state') - return self.post(upload_url, param=params) + return response def get_oembed_tweet(self, **params): """Returns information allowing the creation of an embedded From 469432bcf84793db1137aaa6c5b3a4f70f386ad9 Mon Sep 17 00:00:00 2001 From: Tushar Bhushan Date: Mon, 26 Sep 2016 14:22:19 -0700 Subject: [PATCH 4/4] - added support for media_category param - added code to handle empty responses for STATUS calls --- twython/endpoints.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/twython/endpoints.py b/twython/endpoints.py index 24ddcb9..8919a71 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -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, check_progress=False): + 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'] @@ -211,19 +213,24 @@ class EndpointsMixin(object): 'command': 'STATUS', 'media_id': media_id } - - processing_state = response['processing_info'].get('state') - if processing_state is not None: + # 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['processing_info'].get('check_after_secs') + check_after_secs = response.get('processing_info').get('check_after_secs') - if check_after_secs is not None: - time.sleep(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['processing_info'].get('state') + processing_state = response.get('processing_info').get('state') return response