def _stream(self, url, params, post): continue_streaming = True timeout = params.pop("timeout", None) yield_exceptions = params.pop("ignore_exceptions", False) while continue_streaming == True: try: if post: request = self._connection.post( self._prepare_url(params), data=params, stream = True, timeout = timeout) else: request = self._connection.post( self._prepare_url(params), params=params, stream = True, timeout = timeout) TwitterException.raise_for_response(request) for line in request.iter_lines(): result = self._process_result(line) if result is not None: yield result except Exception as e: if yield_exceptions: yield e else: raise
def __call__(self, default = None, **params): """ Make the specified call to the Twitter API. Raises requests.exceptions.RequestException if a request was failed. Usually you should retry. Raises a twitterwrapper.TwitterException if Twitter doesn't want to talk to us. """ # Options that will be used during execution spec = self._spec if spec.url is None: raise TypeError("API method is not callable") else: # Place the default option into params properly. if default and spec.default_param: params[spec.default_param] = default # Allow an ID to be specified by the containing class. if self._container and spec.container_id: params[spec.container_id] = self._container.id # Do something different if we're streaming try: if spec.streaming: return self._stream(self._prepare_url(params), params, spec.post) else: if spec.post: result = self._connection.post( self._prepare_url(params), data=params) else: result = self._connection.get( self._prepare_url(params), params=params) TwitterException.raise_for_response(result) # Raises an exception if needed. return self._process_result(result) except OpenSSL.SSL.SysCallError as e: raise requests.exceptions.SSLError(e)