Python client for interacting with Twitter V2 API available at https://rapidapi.com/datarise-datarise-default/api/twitter-x
Project description
Unofficial Twitter Python Client for Twitter/X Rapid API
Overview
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.
Installation
pip install -U twitter-client-py
Usage
Simple Example
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))
Advanced Example
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))
Asynchronous Example
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')
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(result, indent=4))
asyncio.run(main())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for twitter_client_py-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 954f3969c347ac301925fdad657aa286de32fcb19a479616d3bf3bd4f750bc6f |
|
MD5 | 5e5fc0ba1b651653e685780d88dc080e |
|
BLAKE2b-256 | 230846b6e4c7edae571698fa5853cc4635105a71ba324a177e5246736634a909 |