Home > Enterprise >  How to get latest post i made in a specific subreddit using Python PRAW
How to get latest post i made in a specific subreddit using Python PRAW

Time:08-15

I need to check the information about the last post i made on specific subreddit using PRAW. I'm still new to Python and PRAW in general so I did my research and unfortunately I only ended up with getting the latest post i made in the latest subreddit i posted on.

This is the current code that i managed to write:

for submission in reddit.user.me().submissions.new(limit=1):
    print(submission.title)
    print(submission.selftext)

CodePudding user response:

reddit.user.me().submissions returns a SubListing object, which doesn't have the filtering capability to restrict to subreddits, so you'd have to do it yourself by checking to see if the submission was in the subreddit you care about, like so.

import praw

# make our reddit instance
# reddit = praw.Reddit(...)

subreddit = "subreddit-name"

for post in reddit.user.me().submissions.new():
    if post.subreddit.display_name.lower() == subreddit:
        print(post.title)
        print(post.selftext)
        break

Alternatively, you can use the search function to find your user's posts within a subreddit. Also see Subreddit.search

import praw

# make our reddit instance
# reddit = praw.Reddit(...)

subreddit_name = "your-subreddit"
username = reddit.user.me().name

# get an instance of a particular subreddit
subreddit = reddit.subreddit(subreddit_name)

# this should return everything in reverse chronological order
# time_filter can be 'all', 'day', 'hour', 'month', 'week'
listing = subreddit.search(f'author:"{username}"', time_filter='day')

if post := next(listing, None):
    print(post.title)
    print(post.self_text)
  • Related