redditwarp - Reddit API Wrapper

1.3.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Reddit using the password login flow and fetch the top posts from a subreddit. It emphasizes using environment variables for credentials and includes basic error handling for common API issues like rate limits. Remember to replace placeholder values or set environment variables.

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

view raw JSON →