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
Reddit: restrict nsfw subreddit(s) #1157
Conversation
… be over 18). Changed the return format a little bit for the fetch_posts() function, instead of returning an empty list, it returns a list with a dict holding the error message.
I believe filtering posts and only sending the NSFW error message when no posts pass the filter would be a better option here, currently if the first fetched post happens to be NSFW while none of the others are, the user will get no output. |
I think a better approach will be to check if any 1 of the posts does not pass the filter, then send nothing. |
…nd send the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request!
- Regarding NSFW subreddits: Subreddits marked as over 18 will have all the posts marked over 18, so it shouldn't be an issue.
- Rather than telling the user about the subreddit being out of scope, the error message when no posts are found should just be
... couldn't find any SFW posts from that subreddit...
. That way it is clearer and easier to handle in the code. - Error handling should be in
get_top_posts
rather thanfetch_posts
, which would basically be what it is originally.
bot/exts/info/reddit.py
Outdated
@@ -140,12 +140,32 @@ async def fetch_posts(self, route: str, *, amount: int = 25, params: dict = None | |||
# Got appropriate response - process and return. | |||
content = await response.json() | |||
posts = content["data"]["children"] | |||
|
|||
for post in posts: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't mutate a list while you're looping over that same list. The loop is currently skipping posts because of it.
Either create a new list like filtered_posts
or use a list comprehension to assign the filtered list to posts
.
…using list comprehension.
@@ -163,12 +166,11 @@ async def get_top_posts(self, subreddit: Subreddit, time: str = "all", amount: i | |||
amount=amount, | |||
params={"t": time} | |||
) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for the removal of the newline here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well in testing now, awesomesauce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works great, thanks for the fix. There is a good opportunity to test against r/test's top weekly posts:
Also works against NSFW-only subs.
Since this is somewhat critical I decided to merge it even with an unresolved comment from @Numerlor. It only applies to code style and can be addressed separately if necessary. Feel free to continue the conversation. |
closes #1153
Changes made to Reddit Cog:
Check if the subreddit is NSFW or similar by using the
over_18
boolean value got with the API response.if True, then send an error message.
if False, the fetched data is sent to the user.
A small change to the return format of the
fetch_posts()
method, instead of returning an emptylist
in case of an error, it now returns a list, with adict
containing the error message. This is done in order to distinguish between the NSFW restriction error message and network/connection/failed to connect to API error.Pending
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/451
Edit: this should be working.