Beispiel #1
0
    if len(opts) != 0:
        for o, a in opts:
            if o in ("-d", "--dry"):
                dry_run = True
            elif o in ("-p", "--poll"):
                now = datetime.datetime.utcnow()
                now_minus_10 = now + datetime.timedelta(minutes=-10)
                float_now = time.mktime(now_minus_10.timetuple())
                print("Checking for new posts")
                posts = [p for p in subreddit.get_new(limit=post_limit) if p.created_utc > float_now]
                if len(posts) is 0:
                    print("Nothing new")
                else:
                    print("Length is %s" % len(posts))
                    for post in sorted(posts, key=lambda p: p.created_utc):
                        if not dry_run:
                            notify_slack(post)
                        print("Notified")
            elif o in ("-u", "--unsticky"):
                if a in weekly_threads and datetime.datetime.now().strftime("%A") != weekly_threads[a]['day']:
                    print("%s threads should will only be removed on %ss" % (
                        weekly_threads[a]['name'].capitalize(), weekly_threads[a]['day']))
                    break
                sub = get_most_recent_thread(r, a)
                if not dry_run:
                    sub.unsticky()
                print("Unstickied %s" % sub.url)
            else:
                sys.exit('No valid args specified')
Beispiel #2
0
def process_command(text):
    print("Text is %s" % text)
    argv = shlex.split(text)
    print("Tokenized text is %s" % str(argv))

    if len(argv) < 2:
        return 'usage: postbot %s' % standard_commands

    command = argv[1]
    if command not in standard_commands or command == 'help':
        return 'usage: postbot %s' % standard_commands
    elif command == 'running':
        return 'banana'

    if len(argv) < 3:
        return 'usage: postbot %s' % standard_commands

    target = argv[2]
    args = []
    if len(argv) > 2:
        args = argv[3:]

    if command == 'rm':
        # target is the post id
        try:
            opts, args = getopt.getopt(args, "sm:c:", ["spam", "message=", "canned="])
        except getopt.GetoptError as err:
            return str(err)

        submission = r.get_submission(submission_id=target)

        if submission.subreddit.display_name != 'androiddev' and submission.subreddit.display_name != 'androiddevtest':
            return target + " is not a valid /r/androiddev ID"

        is_spam = False
        comment_text = None

        for o, a in opts:
            if o in ("-s", "--spam"):
                is_spam = True
            elif o in ("-m", "--message"):
                comment_text = a
                break  # Only take one of message or canned
            elif o in ("-c", "--canned"):
                comment_text = config.cans[a]
                if a == "questions_thread":
                    comment_text = comment_text + "\n\nToday's thread can be found here: " \
                                                + get_most_recent_thread(r, "questions").url
                break  # Only take one of message or canned

        has_comment = comment_text is not None

        # Now delete
        if has_comment:
            comment = submission.add_comment(comment_text)
            comment.distinguish()
        submission.remove(spam=is_spam)
        return "Removed!"
    elif command == 'flair':
        text = args[0]
        submission = r.get_submission(submission_id=target)
        if text in config.flair_mapping:
            submission.set_flair(flair_text=text, flair_css_class=config.flair_mapping[text])
        else:
            submission.set_flair(text)
        return "Flaired!"
    elif command == 'approve':
        submission = r.get_submission(submission_id=target)
        submission.approve()
        return "Approved!"
    elif command == 'ban':
        # # target is the user id
        # try:
        # opts, args = getopt.getopt(args, "tm:n:", ["temp", "message=", "note="])
        # except getopt.GetoptError as err:
        #     return str(err)
        #
        # # TODO ban the user
        # subreddit.add_ban(target)
        return "Unsupported until next version of PRAW"

    return 'Something went wrong...'