Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Python client for interacting with Twitter V2 API available at https://rapidapi.com/datarise-datarise-default/api/twitter-x
This is a Python client for interacting with the Twitter V2 API via the RapidAPI platform. The library provides synchronous, asynchronous, and multi-threaded capabilities for retrieving Twitter user details and tweets.
pip install -U twitter-client-py
The following example demonstrates how to use the library to get user details and tweets.
from os import getenv
import json
from twitter_client_py import TwitterClient
api_key = getenv('API_KEY')
client = TwitterClient(api_key=api_key, timeout=20, verbose=True)
# Get user details by username
user_details = client.user_details(username='elonmusk')
if user_details.status_code == 200:
print(json.dumps(user_details.json(), indent=4))
# Get user details by id
user_details = client.user_details(user_id='44196397')
if user_details.status_code == 200:
print(json.dumps(user_details.json(), indent=4))
# Get user tweets by username
user_tweets = client.user_tweets(username='elonmusk')
if user_tweets.status_code == 200:
print(json.dumps(user_tweets.json(), indent=4))
# Get user tweets by id
user_tweets = client.user_tweets(user_id='44196397')
if user_tweets.status_code == 200:
print(json.dumps(user_tweets.json(), indent=4))
TwitterClient class is thread-safe, so you can use it in a multi-threaded environment.
from os import getenv
import json
from concurrent.futures import ThreadPoolExecutor
from twitter_client_py import TwitterClient
api_key = getenv('API_KEY')
client = TwitterClient(api_key=api_key, timeout=20, verbose=True)
users = ['elonmusk', 'BillGates', 'JeffBezos', 'tim_cook', 'satyanadella']
def get_user_details(username):
user_details = client.user_details(username=username)
if user_details.status_code == 200:
return user_details.json()
with ThreadPoolExecutor(max_workers=5) as executor:
results = executor.map(get_user_details, users)
for result in results:
print(json.dumps(result, indent=4))
The library also supports asynchronous requests using aiohttp
with asyncio
from os import getenv
import asyncio
import json
from twitter_client_py import AsyncTwitterClient
api_key = getenv('API_KEY') # X-RapidAPI-Key
client = AsyncTwitterClient(api_key=api_key, timeout=20, verbose=True)
users = ['elonmusk', 'BillGates', 'JeffBezos', 'tim_cook', 'satyanadella']
async def get_user_details(username):
user_details = await client.user_details(username=username)
if user_details.status == 200:
return user_details.json()
async def main():
tasks = [get_user_details(username) for username in users]
results = await asyncio.gather(*tasks)
for result in results:
print(json.dumps(await result, indent=4))
await main()
You can check the rate limit of the API using the rate_limit
method. TwitterClient and AsyncTwitterClient have a rate_limit
attribute that returns the rate limit details. It's updated after each request.
rate_limit = client.rate_limit
print(f"Limit: {rate_limit.limit}")
print(f"Remaining requests: {rate_limit.remaining}")
print(f"Reset time: {rate_limit.reset}")
This project is licensed under the MIT License - see the LICENSE file for details.
If you have any questions or feedback, feel free to reach out to us at contact [at] datarise [dot] ma.
FAQs
Python client for interacting with Twitter V2 API available at https://rapidapi.com/datarise-datarise-default/api/twitter-x
We found that twitter-client-py demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.