From 748d28cc71f05a67407e36aa31c5de429d8ce8db Mon Sep 17 00:00:00 2001 From: Jose Manuel Delicado Date: Sat, 7 Oct 2017 18:26:21 +0200 Subject: [PATCH 1/3] twython/api.py: JSON error is not raised if the response content is empty and the status code is not 204. If params is not a dictionary, _transparent_params is not called --- twython/api.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/twython/api.py b/twython/api.py index 2782168..577d6a6 100644 --- a/twython/api.py +++ b/twython/api.py @@ -140,7 +140,11 @@ class Twython(EndpointsMixin, object): params = params or {} func = getattr(self.client, method) - params, files = _transparent_params(params) + if type(params) is dict: + params, files = _transparent_params(params) + else: + params = params + files = list() requests_args = {} for k, v in self.client_args.items(): @@ -192,15 +196,16 @@ class Twython(EndpointsMixin, object): error_message, error_code=response.status_code, retry_after=response.headers.get('X-Rate-Limit-Reset')) - + content="" try: if response.status_code == 204: content = response.content else: content = response.json() except ValueError: - raise TwythonError('Response was not valid JSON. \ - Unable to decode.') + if response.content!="": + raise TwythonError('Response was not valid JSON. \ + Unable to decode.') return content From 6fc7b9e0386110eb05ed897d561004115407c508 Mon Sep 17 00:00:00 2001 From: Jose Manuel Delicado Date: Sat, 7 Oct 2017 18:29:19 +0200 Subject: [PATCH 2/3] Added create_metadata endpoint --- twython/endpoints.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/twython/endpoints.py b/twython/endpoints.py index 95e33d2..1c1d80c 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -14,6 +14,7 @@ This map is organized the order functions are documented at: https://dev.twitter.com/docs/api/1.1 """ +import json import os import warnings from io import BytesIO @@ -150,6 +151,13 @@ class EndpointsMixin(object): return self.post('https://upload.twitter.com/1.1/media/upload.json', params=params) + def create_metadata(self, **params): + """ Adds metadata to a media element, such as image descriptions for visually impaired. + Docs: https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-metadata-create + """ + params = json.dumps(params) + return self.post("https://upload.twitter.com/1.1/media/metadata/create.json", params=params) + 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 From 2cb2ed4a31826349ad81e39a5c035a7eed98c6b1 Mon Sep 17 00:00:00 2001 From: Jose Manuel Delicado Date: Mon, 9 Oct 2017 18:11:24 +0200 Subject: [PATCH 3/3] Twython/api.py: suggested changes in review have been made for pull request 460 --- twython/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/twython/api.py b/twython/api.py index 577d6a6..0ee942c 100644 --- a/twython/api.py +++ b/twython/api.py @@ -140,7 +140,7 @@ class Twython(EndpointsMixin, object): params = params or {} func = getattr(self.client, method) - if type(params) is dict: + if isinstance(params, dict): params, files = _transparent_params(params) else: params = params @@ -196,14 +196,14 @@ class Twython(EndpointsMixin, object): error_message, error_code=response.status_code, retry_after=response.headers.get('X-Rate-Limit-Reset')) - content="" + content = '' try: if response.status_code == 204: content = response.content else: content = response.json() except ValueError: - if response.content!="": + if response.content != '': raise TwythonError('Response was not valid JSON. \ Unable to decode.')