Unable to get a random post from some subreddits with PRAW

前端 未结 1 721
执念已碎
执念已碎 2021-01-22 15:37

i am trying to make a disord bot send images from reddit with this code (just to grab not to send)

def random_post(subreddit):
while True:
    post = reddit.subre         


        
相关标签:
1条回答
  • 2021-01-22 15:49

    This is a known issue on Reddit's end.

    Some subreddits will refuse to give you a random submission. See praw-dev/praw#885 on GitHub.

    On that issue, I went in depth about the cause. My findings were: When a subreddit's moderators uncheck the "allow this subreddit to be exposed to users in /r/all, /r/popular, default, and trending lists" checkbox in their subreddit settings, it prevents the subreddit from being able to provide a random submission. For example, at the time of this writing, https://reddit.com/r/wallpapers/random just redirects to https://www.reddit.com/r/wallpapers/ instead of to some submission.

    In my personal opinion, this is a Reddit bug caused by confusion between /r/random (which redirects to a random subreddit) and /r/{subreddit}/random (which redirects to a random submission within a subreddit). As it stands, the checkbox affects both settings, when in my view it only makes sense for it to affect /r/random.

    In the same GitHub issue, a Reddit developer chimed in to confirm that "the two behaviors are connected" and said that this behavior "isn't a bug per se." That was over two years ago and nothing has changed on Reddit's end since.


    So what does this mean for you?

    Unfortunately there's no good way to get around this. As you've discovered, for some subreddits, subreddit.random() returns None. This is noted in the documentation of the method. In your code, you should check that post is not None. In the case that it is None, it's up to you how to handle that. You could just return None, or perhaps you could use an alternate method to get a random submission, such as (for instance) getting 25 submissions from the hot listing and using random.choice() to select one.

    0 讨论(0)
提交回复
热议问题