From 4566600f430558ae7d36f413d7ed99d55ffbd45b Mon Sep 17 00:00:00 2001 From: Madhu Kumar Dadi Date: Wed, 8 Mar 2017 16:33:41 +0530 Subject: [PATCH 1/3] Proxy feature added --- twitter/api.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/twitter/api.py b/twitter/api.py index 4a45061c..0b8df294 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -155,7 +155,8 @@ def __init__(self, debugHTTP=False, timeout=None, sleep_on_rate_limit=False, - tweet_mode='compat'): + tweet_mode='compat', + proxies=None): """Instantiate a new twitter.Api object. Args: @@ -229,6 +230,7 @@ def __init__(self, self.rate_limit = RateLimit() self.sleep_on_rate_limit = sleep_on_rate_limit self.tweet_mode = tweet_mode + self.proxies = proxies if base_url is None: self.base_url = 'https://api.twitter.com/1.1' @@ -4872,7 +4874,8 @@ def _RequestChunkedUpload(self, url, headers, data): headers=headers, data=data, auth=self.__auth, - timeout=self._timeout + timeout=self._timeout, + proxies=self.proxies ) except requests.RequestException as e: raise TwitterError(str(e)) @@ -4909,20 +4912,20 @@ def _RequestUrl(self, url, verb, data=None, json=None): if data: if 'media_ids' in data: url = self._BuildUrl(url, extra_params={'media_ids': data['media_ids']}) - resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout) + resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout, proxies=self.proxies) elif 'media' in data: - resp = requests.post(url, files=data, auth=self.__auth, timeout=self._timeout) + resp = requests.post(url, files=data, auth=self.__auth, timeout=self._timeout, proxies=self.proxies) else: - resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout) + resp = requests.post(url, data=data, auth=self.__auth, timeout=self._timeout, proxies=self.proxies) elif json: - resp = requests.post(url, json=json, auth=self.__auth, timeout=self._timeout) + resp = requests.post(url, json=json, auth=self.__auth, timeout=self._timeout, proxies=self.proxies) else: resp = 0 # POST request, but without data or json elif verb == 'GET': data['tweet_mode'] = self.tweet_mode url = self._BuildUrl(url, extra_params=data) - resp = requests.get(url, auth=self.__auth, timeout=self._timeout) + resp = requests.get(url, auth=self.__auth, timeout=self._timeout, proxies=self.proxies) else: resp = 0 # if not a POST or GET request @@ -4954,14 +4957,15 @@ def _RequestStream(self, url, verb, data=None): try: return requests.post(url, data=data, stream=True, auth=self.__auth, - timeout=self._timeout) + timeout=self._timeout, + proxies=self.proxies) except requests.RequestException as e: raise TwitterError(str(e)) if verb == 'GET': url = self._BuildUrl(url, extra_params=data) try: return requests.get(url, stream=True, auth=self.__auth, - timeout=self._timeout) + timeout=self._timeout, proxies=self.proxies) except requests.RequestException as e: raise TwitterError(str(e)) return 0 # if not a POST or GET request From 4679199e7c8f50ce5e8f90d189af2b75e69a3bd4 Mon Sep 17 00:00:00 2001 From: Madhu Kumar Dadi Date: Sun, 12 Mar 2017 22:39:58 +0530 Subject: [PATCH 2/3] Updated Inline documentation for Api.__init__() method --- twitter/api.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/twitter/api.py b/twitter/api.py index 0b8df294..4abe5b3a 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -204,6 +204,9 @@ def __init__(self, tweet_mode (str, optional): Whether to use the new (as of Sept. 2016) extended tweet mode. See docs for details. Choices are ['compatibility', 'extended']. + proxies (dict, optional): + A dictionary of proxies for the request to pass through, if not specified + allows requests lib to use environmental variables for proxy if any. """ # check to see if the library is running on a Google App Engine instance From f6a1a8ca5bbbc38092c13c8947c6f72adfd1d55b Mon Sep 17 00:00:00 2001 From: Madhu Kumar Dadi Date: Mon, 13 Mar 2017 12:49:03 +0530 Subject: [PATCH 3/3] document formatting done --- twitter/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/twitter/api.py b/twitter/api.py index 4abe5b3a..88b2a66c 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -204,9 +204,9 @@ def __init__(self, tweet_mode (str, optional): Whether to use the new (as of Sept. 2016) extended tweet mode. See docs for details. Choices are ['compatibility', 'extended']. - proxies (dict, optional): - A dictionary of proxies for the request to pass through, if not specified - allows requests lib to use environmental variables for proxy if any. + proxies (dict, optional): + A dictionary of proxies for the request to pass through, if not specified + allows requests lib to use environmental variables for proxy if any. """ # check to see if the library is running on a Google App Engine instance