if arg is None:
continue
if k in self.session.params:
- raise TweepError('Multiple values for parameter %s supplied!' % k)
+ raise TweepError(f'Multiple values for parameter {k} supplied!')
- self.session.params[k] = convert_to_utf8_str(arg)
+ self.session.params[k] = str(arg)
log.debug("PARAMS: %r", self.session.params)
# or maximum number of retries is reached.
retries_performed = 0
while retries_performed < self.retry_count + 1:
- # handle running out of api calls
- if self.wait_on_rate_limit:
- if self._reset_time is not None:
- if self._remaining_calls is not None:
- if self._remaining_calls < 1:
- sleep_time = self._reset_time - int(time.time())
- if sleep_time > 0:
- if self.wait_on_rate_limit_notify:
- log.warning(f"Rate limit reached. Sleeping for: {sleep_time}")
- time.sleep(sleep_time + 5) # sleep for few extra sec
-
- # if self.wait_on_rate_limit and self._reset_time is not None and \
- # self._remaining_calls is not None and self._remaining_calls < 1:
- # sleep_time = self._reset_time - int(time.time())
- # if sleep_time > 0:
- # if self.wait_on_rate_limit_notify:
- # log.warning(f"Rate limit reached. Sleeping for: {sleep_time}")
- # time.sleep(sleep_time + 5) # sleep for few extra sec
+ if (self.wait_on_rate_limit and self._reset_time is not None
+ and self._remaining_calls is not None
+ and self._remaining_calls < 1):
+ # Handle running out of API calls
+ sleep_time = self._reset_time - int(time.time())
+ if sleep_time > 0:
+ if self.wait_on_rate_limit_notify:
- log.warning("Rate limit reached. Sleeping for: %d" % sleep_time)
++ log.warning(f"Rate limit reached. Sleeping for: {sleep_time}")
+ time.sleep(sleep_time + 1) # Sleep for extra sec
# Apply authentication
auth = None
auth=auth,
proxies=self.api.proxy)
except Exception as e:
- raise TweepError('Failed to send request: %s' % e).with_traceback(sys.exc_info()[2])
+ raise TweepError(f'Failed to send request: {e}').with_traceback(sys.exc_info()[2])
- rem_calls = resp.headers.get('x-rate-limit-remaining')
+ if resp.status_code in (200, 204):
+ break
+ rem_calls = resp.headers.get('x-rate-limit-remaining')
if rem_calls is not None:
self._remaining_calls = int(rem_calls)
- elif isinstance(self._remaining_calls, int):
+ elif self._remaining_calls is not None:
self._remaining_calls -= 1
+
reset_time = resp.headers.get('x-rate-limit-reset')
if reset_time is not None:
self._reset_time = int(reset_time)
return results
def __repr__(self):
- state = ['%s=%s' % (k, repr(v)) for (k, v) in vars(self).items()]
- return '%s(%s)' % (self.__class__.__name__, ', '.join(state))
+ state = [f'{k}={v!r}' for (k, v) in vars(self).items()]
+ return f'{self.__class__.__name__}({", ".join(state)})'
- class Status(Model):
+ class Status(Model, Hashable):
@classmethod
def parse(cls, api, json):