Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subreddit.random() throws BadRequest if not supported #2039

Open
1 task done
Levtastic opened this issue Dec 10, 2024 · 1 comment
Open
1 task done

subreddit.random() throws BadRequest if not supported #2039

Levtastic opened this issue Dec 10, 2024 · 1 comment

Comments

@Levtastic
Copy link

Describe the Bug

Previously, if you called subreddit.random() on a subreddit which didn't support it, praw would simply return a None, as mentioned in the function docstring here:

def random(self) -> praw.models.Submission | None:
"""Return a random :class:`.Submission`.
Returns ``None`` on subreddits that do not support the random feature. One
example, at the time of writing, is r/wallpapers.

But the reddit api seems to have changed and now praw will throw a prawcore.BadRequest instead.

Desired Result

Either restore previous functionality of the function returning None, or update the documentation to reflect the new behaviour.

Code to reproduce the bug

reddit = Reddit(**secret_credentials)

subreddit = reddit.subreddit('politecats')
random_post = subreddit.random()  # throws a BadRequest

My code does not include sensitive credentials

  • Yes, I have removed sensitive credentials from my code.

Relevant Logs

Traceback (most recent call last):                                                                                                                                          
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\flask\app.py", line 1498, in __call__                                                             
    return self.wsgi_app(environ, start_response)                                                                                                                           
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                           
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\flask\app.py", line 1476, in wsgi_app                                                             
    response = self.handle_exception(e)                                                                                                                                     
               ^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                     
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\flask\app.py", line 1473, in wsgi_app                                                             
    response = self.full_dispatch_request()                                                                                                                                 
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                 
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request                                                 
    rv = self.handle_user_exception(e)                                                                                                                                      
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                      
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request                                                 
    rv = self.dispatch_request()                                                                                                                                            
         ^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                            
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\flask\app.py", line 865, in dispatch_request                                                      
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]                                                                 
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                 
  File "C:\Users\Lev\Documents\Mine\Code\Websites\Flask\Main Website\site-docker\website\blueprints\random_reddit_images\views.py", line 19, in get_random_image            
    post = get_random_image_post(subreddit)                                                                                                                                 
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                 
  File "C:\Users\Lev\Documents\Mine\Code\Websites\Flask\Main Website\site-docker\website\blueprints\random_reddit_images\views.py", line 25, in get_random_image_post       
    post = subreddit.random()                                                                                                                                               
           ^^^^^^^^^^^^^^^^^^                                                                                                                                               
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\praw\models\reddit\subreddit.py", line 3230, in random                                            
    self._reddit.get(url, params={"unique": self._reddit._next_unique})                                                                                                     
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\praw\util\deprecate_args.py", line 46, in wrapped                                                 
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                                                     
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                     
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\praw\reddit.py", line 731, in get                                                                 
    return self._objectify_request(method="GET", params=params, path=path)                                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                  
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\praw\reddit.py", line 514, in _objectify_request                                                  
    self.request(                                                                                                                                                           
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\praw\util\deprecate_args.py", line 46, in wrapped                                                 
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                                                     
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                     
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\praw\reddit.py", line 963, in request                                                             
    return self._core.request(                                                                                                                                              
           ^^^^^^^^^^^^^^^^^^^                                                                                                                                              
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\prawcore\sessions.py", line 328, in request                                                       
    return self._request_with_retries(                                                                                                                                      
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                      
  File "C:\Users\Lev\.virtualenvs\Main_Website-TkBe3dLq\Lib\site-packages\prawcore\sessions.py", line 267, in _request_with_retries                                         
    raise self.STATUS_EXCEPTIONS[response.status_code](response)                                                                                                            
prawcore.exceptions.BadRequest: received 400 HTTP response

This code has previously worked as intended

Yes

Operating System/Environment

Windows 10 and Linux raspberrypi 6.1.21

Python Version

3.12.4

PRAW Version

7.8.1

Links, references, and/or additional comments?

Subjectively, I also feel like subreddits that used to support .random() no longer do - it's possible that the reddit api has dropped the feature entirely (which would explain why the endpoint is now return a 400) but I have not checked this.

@LilSpazJoekp
Copy link
Member

Unfortunately, Reddit has indeed dropped support for random(). This will be removed from PRAW soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants