Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Unofficial Twitter Python Client for Twitter/X Rapid API

PyPI Build Status PyPI - Python Version PyPI - Downloads License: MIT

Table of Contents

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') # 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()

Check Rate Limit

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}")

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

If you have any questions or feedback, feel free to reach out to us at contact [at] datarise [dot] ma.

Metadata

Release files for twitter-client-py 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for twitter-client-py 0.3.3
File Size Uploaded
twitter_client_py-0.3.3.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for twitter-client-py 0.3.3
File Interpreter ABI Platform
twitter_client_py-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / twitter_client_py-0.3.3.tar.gz

Download URL twitter_client_py-0.3.3.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fa05037b8605a68112b914778f79988f4386e2a2acce386eaedbed58c737e194
BLAKE2b-256 checksum
How to use checksums
8e841e3498032e1594fdc69fab68a5c60ef0d49e8b1a3c312c44b98bae491dd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3

Release files / twitter_client_py-0.3.3-py3-none-any.whl

Download URL twitter_client_py-0.3.3-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e00ee409e1b3527b51c03cd29dea79d32378c53cd2d1113e014ae815b83d49eb
BLAKE2b-256 checksum
How to use checksums
06095e8efe5baa200eff85413905b807065fa0c073f337907ee9b9ca51c649f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page