TwitterAPI.TwitterAPI

class TwitterAPI.TwitterAPI.TwitterAPI(consumer_key=None, consumer_secret=None, access_token_key=None, access_token_secret=None, auth_type=OAuthType.OAUTH1, proxy_url=None, api_version='1.1')[source]

Access REST API or Streaming API resources.

Parameters
  • consumer_key – Twitter application consumer key

  • consumer_secret – Twitter application consumer secret

  • access_token_key – Twitter application access token key

  • access_token_secret – Twitter application access token secret

  • auth_type – “oAuth1” (default) or “oAuth2”

  • proxy_url – HTTPS proxy URL string (ex. ‘https://USER:PASSWORD@SERVER:PORT’), or dict of URLs (ex. {‘http’:’http://SERVER’, ‘https’:’https://SERVER’})

CONNECTION_TIMEOUT = 5
REST_TIMEOUT = 5
STREAMING_TIMEOUT = 90
USER_AGENT = 'python-TwitterAPI'
request(resource, params=None, files=None, method_override=None, hydrate_type=HydrateType.NONE)[source]

Request a Twitter REST API or Streaming API resource.

Parameters
  • resource – A Twitter endpoint (ex. “search/tweets”)

  • params – Dictionary with endpoint parameters or None (default)

  • files – Dictionary with multipart-encoded file or None (default)

  • method_override – Request method to override or None (default)

  • hydrate_type – HydrateType or int Do not hydrate (‘includes’ field and all its content will be lost for non-streaming requests) - NONE or 0 (default) Append new field with ‘_hydrate’ suffix with hydrate values - APPEND or 1 Replace current field value with hydrate values - REPLACE or 2

Returns

TwitterResponse

Raises

TwitterConnectionError

class TwitterAPI.TwitterAPI.TwitterResponse(response, options)[source]

Response from either a REST API or Streaming API resource call.

Parameters
  • response – The requests.Response object returned by the API call

  • stream – Boolean connection type (True if a streaming connection)

  • options – Dict containing parsing options

close()[source]

Disconnect stream (blocks with Python 3).

get_iterator()[source]

Get API dependent iterator.

Returns

Iterator for tweets or other message objects in response.

Raises

TwitterConnectionError, TwitterRequestError

get_quota()[source]

Quota information in the REST-only response header.

Returns

Dictionary of ‘remaining’ (count), ‘limit’ (count), ‘reset’ (time)

property headers
Returns

Dictionary of API response header contents.

json(**kwargs)[source]

Get the response as a JSON object.

Parameters

**kwargs – Optional arguments that json.loads takes.

Returns

response as JSON object.

Raises

ValueError

property status_code
Returns

HTTP response status code.

property text
Returns

Raw API response text.