Rekognize is a Python Twitter REST API client for accessing Twitter data.
Install rekognize
from PyPI.
pip install rekognize
Before you get started, you will need to authorize Rekognize Twitter application to obtain your
ACCESS_TOKEN
andACCESS_TOKEN_SECRET
.
Import client and initialize it:
from rekognize.twitter import UserClient
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
ACCESS_TOKEN_SECRET = 'YOUR_ACCESS_TOKEN_SECRET'
client = UserClient(
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET
)
Then you can access Twitter REST API endpoints as follows:
# trends/available
>>> client.api.trends.available.get()
# users/search
>>> client.api.users.search.get(
q='#TwitterAPI',
count=100,
)
# users/lookup
>>> r = client.api.users.lookup.post(
screen_name='twitter',
)
>>> client.api.users.show.get(screen_name='twitter')
Alternatively, calls can be written in the following syntax:
>>> response = client.api['users/show'].get(screen_name='twitter')
rekognize
is a read-only API. You can use it to access every GET
endpoint of the Twitter REST API. Twitter API conditions such as the request parameter syntax and rate limits apply.
Calls to REST API return an ApiResponse
object, which in addition to returned data, also gives you access to the remaining number of calls to the same endpoint (reset every 15 mins), response headers and the resource URL.
response.data # decoded JSON data
response.resource_url # resource URL
response.remaining # remaining number of calls you can make to the same API endpoint; reset every 15 mins
response.headers # dictionary containing response HTTP headers
There are 4 types of exceptions.
TwitterClientError
raised for connection and access token retrieval errorsTwitterApiError
raised when Twitter returns an errorTwitterAuthError
raised when authentication fails,
TwitterApiError
subclassTwitterRateLimitError
raised when rate limit for resource is reached, TwitterApiError
subclass