Python Reddit API Wrapper - simple access to Reddit's API
—
PRAW provides comprehensive subreddit management capabilities including posting content, searching, subscribing, moderation, and accessing subreddit-specific features like flairs, widgets, and stylesheets.
Access subreddits and perform basic operations like subscribing and messaging.
class Subreddit:
def __init__(self, reddit, display_name: str): ...
def subscribe(self):
"""Subscribe to the subreddit."""
def unsubscribe(self):
"""Unsubscribe from the subreddit."""
def message(self, subject: str, message: str, *, from_subreddit = None):
"""
Send a message to the subreddit moderators.
Parameters:
- subject: Message subject
- message: Message body (markdown supported)
- from_subreddit: Send from subreddit instead of user
"""Submit various types of content to subreddits including text posts, links, images, and media.
def submit(
self,
title: str,
*,
flair_id: str = None,
flair_text: str = None,
nsfw: bool = False,
spoiler: bool = False,
**kwargs
):
"""
Submit content to the subreddit.
Parameters:
- title: Submission title
- selftext: Text content (for text posts)
- url: URL (for link posts)
- flair_id: Flair template ID
- flair_text: Custom flair text
- nsfw: Mark as NSFW
- spoiler: Mark as spoiler
- send_replies: Enable inbox replies (default: True)
- resubmit: Allow resubmitting same URL
- draft: Save as draft instead of posting
Returns:
Submission instance
"""
def submit_image(
self,
title: str,
image_path: str,
*,
flair_id: str = None,
flair_text: str = None,
nsfw: bool = False,
spoiler: bool = False,
timeout: int = 10,
**kwargs
):
"""
Submit an image to the subreddit.
Parameters:
- title: Submission title
- image_path: Path to image file
- flair_id: Flair template ID
- flair_text: Custom flair text
- nsfw: Mark as NSFW
- spoiler: Mark as spoiler
- timeout: Upload timeout in seconds
Returns:
Submission instance
"""
def submit_video(
self,
title: str,
video_path: str,
*,
thumbnail_path: str = None,
flair_id: str = None,
flair_text: str = None,
nsfw: bool = False,
spoiler: bool = False,
timeout: int = 10,
**kwargs
):
"""
Submit a video to the subreddit.
Parameters:
- title: Submission title
- video_path: Path to video file
- thumbnail_path: Path to thumbnail image
- flair_id: Flair template ID
- flair_text: Custom flair text
- nsfw: Mark as NSFW
- spoiler: Mark as spoiler
- timeout: Upload timeout in seconds
Returns:
Submission instance
"""Access different content listings and search within subreddits.
def hot(self, **kwargs):
"""
Get hot submissions from the subreddit.
Parameters:
- limit: Number of submissions (default: 25, max: 100)
- params: Additional query parameters
Returns:
ListingGenerator of Submission instances
"""
def new(self, **kwargs):
"""
Get new submissions from the subreddit.
Parameters:
- limit: Number of submissions (default: 25, max: 100)
- params: Additional query parameters
Returns:
ListingGenerator of Submission instances
"""
def top(self, time_filter: str = "all", **kwargs):
"""
Get top submissions from the subreddit.
Parameters:
- time_filter: Time period ("all", "day", "week", "month", "year")
- limit: Number of submissions (default: 25, max: 100)
- params: Additional query parameters
Returns:
ListingGenerator of Submission instances
"""
def controversial(self, time_filter: str = "all", **kwargs):
"""
Get controversial submissions from the subreddit.
Parameters:
- time_filter: Time period ("all", "day", "week", "month", "year")
- limit: Number of submissions (default: 25, max: 100)
- params: Additional query parameters
Returns:
ListingGenerator of Submission instances
"""
def rising(self, **kwargs):
"""
Get rising submissions from the subreddit.
Parameters:
- limit: Number of submissions (default: 25, max: 100)
- params: Additional query parameters
Returns:
ListingGenerator of Submission instances
"""
def gilded(self, **kwargs):
"""
Get gilded submissions from the subreddit.
Parameters:
- limit: Number of submissions (default: 25, max: 100)
- params: Additional query parameters
Returns:
ListingGenerator of Submission instances
"""
def search(
self,
query: str,
*,
sort: str = "relevance",
syntax: str = "plain",
time_filter: str = "all",
**kwargs
):
"""
Search within the subreddit.
Parameters:
- query: Search query
- sort: Sort order ("relevance", "hot", "top", "new", "comments")
- syntax: Search syntax ("plain", "lucene", "cloudsearch")
- time_filter: Time period ("all", "day", "week", "month", "year")
- limit: Number of results (default: 25, max: 100)
Returns:
ListingGenerator of Submission instances
"""Access subreddit information and configuration.
# Basic properties
display_name: str # Subreddit name (without r/ prefix)
id: str # Subreddit ID
title: str # Subreddit title
description: str # Full description
public_description: str # Short description
description_html: str # Description as HTML
public_description_html: str # Short description as HTML
# Statistics
subscribers: int # Subscriber count
accounts_active: int # Currently active users
active_user_count: int # Active users (may be None)
# Status and settings
over18: bool # Whether NSFW
quarantine: bool # Whether quarantined
user_is_subscriber: bool # Whether current user is subscribed
user_is_moderator: bool # Whether current user is moderator
user_is_contributor: bool # Whether current user is contributor
user_is_banned: bool # Whether current user is banned
user_can_flair_in_sr: bool # Whether user can set flair
# Timestamps
created_utc: float # Creation timestamp (UTC)
# Colors and styling
primary_color: str # Primary theme color
key_color: str # Key theme color
banner_background_color: str # Banner background color
banner_background_image: str # Banner image URL
header_img: str # Header image URL
icon_img: str # Icon image URL
# Configuration
submission_type: str # Allowed submission types
subreddit_type: str # Subreddit type (public, restricted, private)
content_options: str # Content options
allow_images: bool # Whether images allowed
allow_videos: bool # Whether videos allowed
allow_polls: bool # Whether polls allowed
wiki_enabled: bool # Whether wiki enabled
show_media: bool # Whether to show media
show_media_preview: bool # Whether to show media previewsManage user and submission flairs within the subreddit.
class SubredditFlair:
"""Subreddit flair management."""
def choices(self):
"""
Get available flair choices for current user.
Returns:
List of flair choice dictionaries
"""
def link_templates(self):
"""
Get link flair templates.
Returns:
List of flair template dictionaries
"""
def user_templates(self):
"""
Get user flair templates.
Returns:
List of flair template dictionaries
"""
def set(
self,
redditor,
text: str = None,
css_class: str = None,
flair_template_id: str = None
):
"""
Set user flair.
Parameters:
- redditor: Redditor instance or username
- text: Flair text
- css_class: CSS class
- flair_template_id: Template ID
"""
def delete(self, redditor):
"""
Remove user flair.
Parameters:
- redditor: Redditor instance or username
"""
def update(
self,
flair_list: list,
text: str = "",
css_class: str = ""
):
"""
Update multiple user flairs.
Parameters:
- flair_list: List of flair updates
- text: Default flair text
- css_class: Default CSS class
"""
flair: SubredditFlair # Flair management instanceAccess comprehensive moderation tools for the subreddit.
class SubredditModeration:
"""Subreddit moderation capabilities."""
def approve(self, thing):
"""Approve a submission or comment."""
def remove(self, thing, *, mod_note: str = None, spam: bool = None):
"""Remove a submission or comment."""
def spam(self, thing):
"""Mark as spam."""
def notes(self, **kwargs):
"""Get moderator notes."""
def log(self, **kwargs):
"""Get moderation log."""
def queue(self, **kwargs):
"""Get moderation queue."""
def reports(self, **kwargs):
"""Get reported items."""
def spam_queue(self, **kwargs):
"""Get spam queue."""
def modmail_conversations(self, **kwargs):
"""Get modmail conversations."""
def settings(self):
"""Get subreddit settings."""
def update(self, **settings):
"""Update subreddit settings."""
mod: SubredditModeration # Moderation instanceManage subreddit widgets for new Reddit design.
class SubredditWidgets:
"""Subreddit widget management."""
def __init__(self, subreddit): ...
def sidebar(self):
"""Get sidebar widgets."""
def topbar(self):
"""Get topbar widgets."""
def id_card(self):
"""Get ID card widget."""
def moderators_widget(self):
"""Get moderators widget."""
def progressive_images(self):
"""Upload progressive images for widgets."""
widgets: SubredditWidgets # Widget management instanceAccess and manage subreddit wiki pages.
class SubredditWiki:
"""Subreddit wiki management."""
def __init__(self, subreddit): ...
def __getitem__(self, page_name: str):
"""Get wiki page by name."""
def create(
self,
name: str,
content: str,
reason: str = None,
**kwargs
):
"""
Create wiki page.
Parameters:
- name: Page name
- content: Page content (markdown)
- reason: Edit reason
"""
wiki: SubredditWiki # Wiki management instanceManage subreddit stylesheets for old Reddit design.
class SubredditStylesheet:
"""Subreddit stylesheet management."""
def __call__(self):
"""Get current stylesheet."""
def update(
self,
stylesheet: str,
reason: str = None
):
"""
Update stylesheet.
Parameters:
- stylesheet: CSS content
- reason: Update reason
"""
def upload(
self,
name: str,
image_path: str
):
"""
Upload stylesheet image.
Parameters:
- name: Image name
- image_path: Path to image file
"""
def delete_image(self, name: str):
"""Delete stylesheet image."""
stylesheet: SubredditStylesheet # Stylesheet management instanceStream new submissions and comments from the subreddit.
class SubredditStream:
"""Real-time subreddit content streaming."""
def submissions(self, **kwargs):
"""
Stream new submissions.
Parameters:
- pause_after: Pause after this many requests
- skip_existing: Skip submissions created before stream start
Yields:
Submission instances
"""
def comments(self, **kwargs):
"""
Stream new comments.
Parameters:
- pause_after: Pause after this many requests
- skip_existing: Skip comments created before stream start
Yields:
Comment instances
"""
stream: SubredditStream # Streaming instanceManage content filters for the subreddit.
class SubredditFilters:
"""Subreddit content filters."""
def __init__(self, subreddit): ...
def __iter__(self):
"""Iterate over filters."""
def add(self, filterName: str):
"""Add content filter."""
def remove(self, filterName: str):
"""Remove content filter."""
filters: SubredditFilters # Filters management instanceManage post collections within the subreddit.
class SubredditCollections:
"""Subreddit collections management."""
def __call__(self, **kwargs):
"""Get collections."""
collections: SubredditCollections # Collections management instance# Access subreddit
subreddit = reddit.subreddit("python")
# Subscribe/unsubscribe
subreddit.subscribe()
subreddit.unsubscribe()
# Get subreddit info
print(f"r/{subreddit.display_name}")
print(f"Subscribers: {subreddit.subscribers}")
print(f"Description: {subreddit.public_description}")# Text post
submission = subreddit.submit(
title="My Python Question",
selftext="I need help with...",
flair_text="Help"
)
# Link post
submission = subreddit.submit(
title="Interesting Article",
url="https://example.com/article"
)
# Image post
submission = subreddit.submit_image(
title="My Screenshot",
image_path="/path/to/image.png",
nsfw=False
)# Get hot posts
for submission in subreddit.hot(limit=10):
print(f"{submission.title} - Score: {submission.score}")
# Search within subreddit
for submission in subreddit.search("python tutorial", limit=5):
print(f"Found: {submission.title}")
# Get top posts from this week
for submission in subreddit.top("week", limit=10):
print(f"{submission.title} - {submission.upvote_ratio}")# Get available flairs
flair_choices = subreddit.flair.choices()
for choice in flair_choices:
print(f"Flair: {choice['flair_text']}")
# Set user flair
subreddit.flair.set(
redditor="username",
text="Python Developer",
css_class="developer"
)# Stream new submissions
for submission in subreddit.stream.submissions():
print(f"New post: {submission.title}")
# Process new submission
# Stream new comments
for comment in subreddit.stream.comments():
print(f"New comment by {comment.author}: {comment.body[:50]}...")
# Process new commentclass SubredditHelper:
"""Helper for creating subreddits."""
def create(
self,
name: str,
title: str = None,
link_type: str = "any",
subreddit_type: str = "public",
wikimode: str = "disabled",
**kwargs
):
"""Create a new subreddit."""
class SubredditModmail:
"""Subreddit modmail management."""
def __call__(self, id: str = None):
"""Get modmail conversation."""
def conversations(self, **kwargs):
"""Get modmail conversations."""
def create(self, subject: str, body: str, recipient: str, **kwargs):
"""Create modmail conversation."""
modmail: SubredditModmail # Modmail management instanceInstall with Tessl CLI
npx tessl i tessl/pypi-praw