Search code examples
pythonredditpraw

Python Praw skipping sticky in subreddits


I am trying to loop through subreddits, but want to ignore the sticky posts at the top. I am able to print the first 5 posts, unfortunately including the stickies. Various pythonic methods of trying to skip these have failed. Two different examples of my code below.

            subreddit = reddit.subreddit(sub)
            for submission in subreddit.hot(limit=5):

                # If we haven't replied to this post before
                if submission.id not in posts_replied_to:
                    ##FOOD

                    if subreddit == 'food':

                        if 'pLEASE SEE' in submission.title:
                            pass
                        if "please vote" in submission.title:
                            pass
                        else:
                            print(submission.title)
                        if re.search("please vote", submission.title, re.IGNORECASE):
                            pass
                        else:

                            print(submission.title)

I noticed a sticky tag in the documents but not sure exactly how to use it. Any help is appreciated.


Solution

  • It looks like you can get the id of a stickied post based on docs. So perhaps you could get the id(s) of the stickied post(s) (note that with the 'number' parameter of the sticky method you can say give me the first, or second, or third, stickied post; use this to your advantage to get all of the stickied posts) and for each submission that you are going to pull, first check its id against the stickied ids.

    Example:

    # assuming there are no more than three stickies...
    stickies = [reddit.subreddit("chicago").sticky(i).id for i in range(1,4)]
    

    and then when you want to make sure a given post isn't stickied, use:

    if post.id not in stickies:
        do something
    

    It looks like, were there fewer than three, this would give you a list with duplicate ids, which won't be a problem.