redditwarp - Reddit API Wrapper
redditwarp is a modern, feature-rich Python library designed for interacting with the Reddit API. It provides both synchronous and asynchronous interfaces, robust error handling, and comprehensive model definitions for Reddit objects. Currently at version 1.3.0, the library maintains an active development cycle, releasing updates as needed to track API changes or introduce new features.
Common errors
-
ModuleNotFoundError: No module named 'redditwarp'
cause The `redditwarp` package is not installed in your current Python environment.fixRun `pip install redditwarp` to install the library. -
redditwarp.errors.InvalidCredentials: Invalid credentials. Check your username, password, client ID, and client secret.
cause One or more of the authentication parameters provided to `Auth.password_login` (or other Auth methods) are incorrect.fixVerify all your Reddit API credentials. Ensure the client ID and secret belong to a 'script' type application on Reddit and that the username/password are correct. -
redditwarp.errors.APIError: 429 - Too Many Requests
cause Your application has exceeded Reddit's API rate limits. The server has temporarily blocked further requests.fixImplement a delay mechanism (e.g., `time.sleep()`) after catching `RateLimitError` or `APIError` with status code 429. Consider using a library like `tenacity` for robust retry logic. -
AttributeError: 'Post' object has no attribute 'some_non_existent_field'
cause You are trying to access a property or field on a Reddit model object that does not exist or is not returned by the specific API endpoint called.fixConsult the `redditwarp` documentation for the specific model type (e.g., `Post`, `Comment`) to see its available attributes. Use `dir(object)` in a Python console to explore available fields. Ensure the API call you made fetches the data you expect.
Warnings
- breaking Major API changes occurred between `redditwarp` 0.x.x and 1.x.x. If you are migrating from an older 0.x.x version, expect significant changes in import paths, class names (e.g., `AsyncClient` vs `Client`), and method signatures.
- gotcha Incorrect or missing authentication credentials will lead to `redditwarp.errors.InvalidCredentials` or `redditwarp.errors.APIError` (e.g., HTTP 401 Unauthorized).
- gotcha Reddit has strict rate limits. Making too many requests in a short period will result in `redditwarp.errors.RateLimitError` (HTTP 429 Too Many Requests).
- gotcha Attempting to access attributes or methods on a Reddit object (e.g., a `Post` or `Comment`) that do not exist or have not been loaded yet can result in an `AttributeError`.
- gotcha `redditwarp` supports both synchronous and asynchronous operations. Mixing them without proper `asyncio` context can lead to runtime errors or deadlocks.
Install
-
pip install redditwarp
Imports
- Client
import redditwarp.Client
from redditwarp import Client
- Auth
from redditwarp import Auth
- errors
from redditwarp import errors
Quickstart
import os
from redditwarp import Client, Auth
from redditwarp.errors import APIError, RateLimitError
# It's highly recommended to use environment variables for sensitive credentials.
# Set these in your environment: REDDIT_USERNAME, REDDIT_PASSWORD,
# REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET
username = os.environ.get('REDDIT_USERNAME', 'your_username')
password = os.environ.get('REDDIT_PASSWORD', 'your_password')
client_id = os.environ.get('REDDIT_CLIENT_ID', 'your_client_id')
client_secret = os.environ.get('REDDIT_CLIENT_SECRET', 'your_client_secret')
# For password login flow, a user agent string is also good practice
user_agent = 'my_script_name_v1.0 (by /u/your_reddit_username)'
try:
# Authenticate and create a client instance
auth = Auth.password_login(
username=username,
password=password,
client_id=client_id,
client_secret=client_secret,
user_agent=user_agent
)
client = Client(auth)
# Fetch the top 5 posts from r/python
print('Fetching top 5 posts from r/python...')
subreddit_posts = client.p.subreddit.get_top(subreddit='python', limit=5)
for post in subreddit_posts:
print(f"- {post.title} (Score: {post.score})")
except APIError as e:
print(f"Reddit API Error: {e.code} - {e.message}")
except RateLimitError:
print("Rate limit exceeded. Please wait and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")