Skip to content

Commit

Permalink
Merge pull request #1044 from praw-dev/submission_random
Browse files Browse the repository at this point in the history
Return None from subreddit.random when necessary
  • Loading branch information
bboe committed Mar 9, 2019
2 parents 2d4e895 + 07c1041 commit dba778a
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Expand Up @@ -22,6 +22,12 @@ Unreleased
parameter ``timeout``. Default timeout has been raised from 2 seconds to
10 seconds.

**Fixed**

* :meth:`.Subreddit.random` returns ``None`` instead of raising
:class:`.ClientException` when the subreddit does not support generating
random submissions.

**Other**

* Bumped minimum prawcore version to 1.0.1.
Expand Down
15 changes: 11 additions & 4 deletions praw/models/reddit/subreddit.py
Expand Up @@ -527,15 +527,22 @@ def _upload_media(self, media_path):
return upload_url + "/" + upload_data["key"]

def random(self):
"""Return a random Submission."""
"""Return a random Submission.
Returns ``None`` on subreddits that do not support the random feature.
One example, at the time of writing, is /r/wallpapers.
"""
url = API_PATH["subreddit_random"].format(subreddit=self)
try:
self._reddit.get(url, params={"unique": self._reddit._next_unique})
except Redirect as redirect:
path = redirect.path
return self._submission_class(
self._reddit, url=urljoin(self._reddit.config.reddit_url, path)
)
try:
return self._submission_class(
self._reddit, url=urljoin(self._reddit.config.reddit_url, path)
)
except ClientException:
return None

def rules(self):
"""Return rules for the subreddit.
Expand Down
2 changes: 1 addition & 1 deletion pre_push.py
Expand Up @@ -31,7 +31,7 @@ def main():
"""Entry point to pre_push.py."""
success = True
success &= do_process(["black *.py docs praw tests"], shell=True)
success &= do_process(["flake8", "--exclude=.eggs,docs,.tox"])
success &= do_process(["flake8", "--exclude=.eggs,build,dist,docs,.tox"])
success &= do_process(["pydocstyle", "praw"])
# success &= do_process(["pylint", "--rcfile=.pylintrc", "praw"])

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
@@ -1,2 +1,3 @@
[tool.black]
exclude = '/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/'
line-length = 79
@@ -0,0 +1,220 @@
{
"http_interactions": [
{
"recorded_at": "2019-03-09T18:26:01",
"request": {
"body": {
"encoding": "utf-8",
"string": "grant_type=client_credentials"
},
"headers": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"identity"
],
"Authorization": [
"Basic <BASIC_AUTH>"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"29"
],
"Content-Type": [
"application/x-www-form-urlencoded"
],
"User-Agent": [
"<USER_AGENT> PRAW/6.1.2.dev0 prawcore/1.0.1"
]
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"headers": {
"Accept-Ranges": [
"bytes"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"106"
],
"Content-Type": [
"application/json; charset=UTF-8"
],
"Date": [
"Sat, 09 Mar 2019 18:26:03 GMT"
],
"Server": [
"snooserv"
],
"Set-Cookie": [
"edgebucket=GwXc0wbGP8qmtmR16B; Domain=reddit.com; Max-Age=63071999; Path=/; secure"
],
"Strict-Transport-Security": [
"max-age=15552000; includeSubDomains; preload"
],
"Via": [
"1.1 varnish"
],
"X-Cache": [
"MISS"
],
"X-Cache-Hits": [
"0"
],
"X-Moose": [
"majestic"
],
"X-Served-By": [
"cache-bur17520-BUR"
],
"X-Timer": [
"S1552155963.947519,VS0,VE334"
],
"cache-control": [
"max-age=0, must-revalidate"
],
"x-content-type-options": [
"nosniff"
],
"x-frame-options": [
"SAMEORIGIN"
],
"x-reddit-loid": [
"00000000003dkbrpbb.2.1552155963235.Z0FBQUFBQmNoQVU3UElPYXZKVU8wRWVPN0w0RXVzV3hWNWRTRFpIRnBuZm9jeFEtWlZ5V25BZEt5d3EtQXoxSkZScnJhRHVjS2RHcVhURjJVMTF5ZVVVYms0RlpSVEFnUjZ5QU5aWUdsUHE0QW9kSXRDVEFtOUhlVC02NVFLbGN4Qk94RkdmakxScl8"
],
"x-xss-protection": [
"1; mode=block"
]
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
},
{
"recorded_at": "2019-03-09T18:26:01",
"request": {
"body": {
"encoding": "utf-8",
"string": ""
},
"headers": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"identity"
],
"Authorization": [
"bearer <ACCESS_TOKEN>"
],
"Connection": [
"keep-alive"
],
"Cookie": [
"edgebucket=GwXc0wbGP8qmtmR16B"
],
"User-Agent": [
"<USER_AGENT> PRAW/6.1.2.dev0 prawcore/1.0.1"
]
},
"method": "GET",
"uri": "https://oauth.reddit.com/r/wallpapers/random/?unique=0&raw_json=1"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": ""
},
"headers": {
"Accept-Ranges": [
"bytes"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"0"
],
"Content-Type": [
"application/json; charset=UTF-8"
],
"Date": [
"Sat, 09 Mar 2019 18:26:03 GMT"
],
"Server": [
"snooserv"
],
"Strict-Transport-Security": [
"max-age=15552000; includeSubDomains; preload"
],
"Via": [
"1.1 varnish"
],
"X-Cache": [
"MISS"
],
"X-Cache-Hits": [
"0"
],
"X-Moose": [
"majestic"
],
"X-Served-By": [
"cache-bur17528-BUR"
],
"X-Timer": [
"S1552155963.475956,VS0,VE211"
],
"access-control-allow-origin": [
"*"
],
"access-control-expose-headers": [
"X-Moose"
],
"cache-control": [
"max-age=0, must-revalidate"
],
"location": [
"https://www.reddit.com/r/wallpapers/.json?utm_campaign=redirect&utm_medium=desktop&utm_source=reddit&utm_name=random_link"
],
"set-cookie": [
"loid=00000000003dkbrpbb.2.1552155963235.Z0FBQUFBQmNoQVU3RzBWcURXYXBNTzdCU0FEelAyaXc0OHpmTm5vdFh2dUNyYmVHNU4yV0U0WHJSY2pxMTRfT09RMEstT1lRRS1mWmkzVE42aXFEU00xSTdaYTE3X3hDZEttSlh1TnJrVzFnRUdfc0pYcjhVdnB1T3hkYjFTYmJtN0h6ZGc1YnFLUUY; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Mon, 08-Mar-2021 18:26:03 GMT; secure",
"session_tracker=ES9DTLwanxM1RT5ULY.0.1552155963543.Z0FBQUFBQmNoQVU3WWwyUXpHMDZqTW5XUG9hdDR1aGJhcmplVkNyT18xQ0F2WlNydFZWeVlmNjdVYUhfSHBuem52eVAzZ3RWcURRMnpHNC1uODhYN1RmeTZSXzRlNFFlQmNHUG1HOVhna2FjZWNVQjZsdW1tRVd2UUFZZjF0ZjJYRVpFcWJlY0FQVGI; Domain=reddit.com; Max-Age=7199; Path=/; expires=Sat, 09-Mar-2019 20:26:03 GMT; secure"
],
"x-content-type-options": [
"nosniff"
],
"x-frame-options": [
"SAMEORIGIN"
],
"x-ua-compatible": [
"IE=edge"
],
"x-xss-protection": [
"1; mode=block"
]
},
"status": {
"code": 302,
"message": "Found"
},
"url": "https://oauth.reddit.com/r/wallpapers/random/?unique=0&raw_json=1"
}
}
],
"recorded_with": "betamax/0.8.1"
}
9 changes: 8 additions & 1 deletion tests/integration/models/reddit/test_subreddit.py
Expand Up @@ -133,7 +133,14 @@ def test_random(self):
subreddit.random(),
subreddit.random(),
]
assert len(submissions) == len(set(submissions))
assert len(submissions) == len(set(submissions))

def test_random__returns_none(self):
with self.recorder.use_cassette(
"TestSubreddit.test_random__returns_none"
):
subreddit = self.reddit.subreddit("wallpapers")
assert subreddit.random() is None

def test_rules(self):
with self.recorder.use_cassette("TestSubreddit.test_rules"):
Expand Down

0 comments on commit dba778a

Please sign in to comment.