Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This Branch fixes issue with reconnection #490

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions twython/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
Twitter Authentication, and miscellaneous methods that are useful when
dealing with the Twitter API
"""

import warnings
import logging

import requests
from requests.auth import HTTPBasicAuth
Expand All @@ -18,13 +18,13 @@
from . import __version__
from .advisory import TwythonDeprecationWarning
from .compat import json, urlencode, parse_qsl, quote_plus, str, is_py2
from .compat import urlsplit
from .endpoints import EndpointsMixin
from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
from .helpers import _transparent_params

warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 >


class Twython(EndpointsMixin, object):
def __init__(self, app_key=None, app_secret=None, oauth_token=None,
oauth_token_secret=None, access_token=None,
Expand Down Expand Up @@ -162,7 +162,23 @@ def _request(self, url, method='GET', params=None, api_call=None):
try:
response = func(url, **requests_args)
except requests.RequestException as e:
raise TwythonError(str(e))
logging.exception('RequestException encountered - refreshing...')
# Rebuild Session and retry
s2 = requests.Session()
s2.auth = self.client.auth
s2.headers = self.client.headers
s2.cert = self.client.cert
s2.hooks = self.client.hooks
s2.proxies = self.client.proxies
s2.max_redirects = self.client.max_redirects
self.client.close()

self.client = s2
try:
response = func(url, **requests_args)
except requests.RequestException as e:
logging.exception('RequestException encountered after refresh!')
raise TwythonError(str(e))

# create stash for last function intel
self._last_call = {
Expand Down Expand Up @@ -498,19 +514,27 @@ def cursor(self, function, return_pages=False, **params):

try:
if function.iter_mode == 'id':
if 'max_id' not in params:
# Add 1 to the id because since_id and
# max_id are inclusive
if hasattr(function, 'iter_metadata'):
since_id = content[function.iter_metadata].get('since_id_str')
# Set max_id in params to one less than lowest tweet id
if hasattr(function, 'iter_metadata'):
# Get supplied next max_id
metadata = content.get(function.iter_metadata)
if 'next_results' in metadata:
next_results = urlsplit(metadata['next_results'])
params = dict(parse_qsl(next_results.query))
else:
since_id = content[0]['id_str']
params['since_id'] = (int(since_id) - 1)
# No more results
raise StopIteration
else:
# Twitter gives tweets in reverse chronological order:
params['max_id'] = str(int(content[-1]['id_str']) - 1)
elif function.iter_mode == 'cursor':
params['cursor'] = content['next_cursor_str']
except (TypeError, ValueError): # pragma: no cover
raise TwythonError('Unable to generate next page of search \
results, `page` is not a number.')
except (KeyError, AttributeError): #pragma no cover
raise TwythonError('Unable to generate next page of search \
results, content has unexpected structure.')

@staticmethod
def unicode2utf8(text):
Expand Down
4 changes: 2 additions & 2 deletions twython/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@

if is_py2:
from urllib import urlencode, quote_plus
from urlparse import parse_qsl
from urlparse import parse_qsl, urlsplit

str = unicode
basestring = basestring
numeric_types = (int, long, float)


elif is_py3:
from urllib.parse import urlencode, quote_plus, parse_qsl
from urllib.parse import urlencode, quote_plus, parse_qsl, urlsplit

str = str
basestring = (str, bytes)
Expand Down