Skip to content

Instantly share code, notes, and snippets.

@13steinj
Last active December 16, 2018 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13steinj/b6a71febdcb50f0c3be91366c894b20c to your computer and use it in GitHub Desktop.
Save 13steinj/b6a71febdcb50f0c3be91366c894b20c to your computer and use it in GitHub Desktop.
Script to toggle a subreddit setting it any sticky matches a given regexp
#!/usr/bin/env python
"""Script to toggle a subreddit setting it any sticky matches a given regexp"""
from argparse import ArgumentParser
from praw import Reddit
from re import compile as re_compile
praw_config = {
'user_agent': __doc__, # change this to be nice to reddit's API guides
}
epilog = """Requires PRAW ^6.0.0.
Note: the parameters to be passed into the reddit instance should be set
in the dictionary above this string in the script file.
Under the chance you have these set in a config file that PRAW will
automatically read, you can leave it as an empty dictionary instead."""
settings_patch = {
'allow_top': 'default_set',
'lang': 'language',
'link_type': 'content_options',
}
sentinel = object()
def main(argv):
reddit = Reddit(**praw_config)
subreddit = reddit.subreddit(argv.subreddit_name)
settings = subreddit.mod.settings()
setting = settings.get(argv.setting_name, sentinel)
if setting is sentinel:
setting = settings[settings_patch[argv.setting_name]]
to_update = {argv.setting_name: not setting}
# 3 because 2 stickies and cache glitches might move one to spot 3
stickies = [post for post in subreddit.hot(limit=3)
if post.stickied and argv.sticky_regexp.match(post.title)]
if stickies and not setting:
subreddit.mod.update(**to_update)
elif not stickies and setting:
subreddit.mod.update(**to_update})
if __name__ == '__main__':
parser = ArgumentParser(description=__doc__, epilog=epilog)
parser.add_argument('subreddit_name')
parser.add_argument('setting_name',
help="See https://praw.readthedocs.io/en/v6.0.0/"
"code_overview/other/subredditmoderation.html"
"#praw.models.reddit.subreddit.Subreddit"
"Moderation.update for a list of valid "
"settings. Should be a boolean setting.")
parser.add_argument('sticky_regexp', type=re_compile)
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment