Пример #1
0
def main():
    user_list = helpers.load_data("user_list")
    reddit = helpers.initialize_reddit()
    stats = helpers.load_data("stats")

    if user_list and ("--ignore-active-community"
                      not in sys.argv):  # checks if the user-list is non-empty
        msg = "Userlist is non-empty. Exiting. Call with --ignore-active-community to run anyway"
        helpers.write_log_trash("Failed {}".format(helpers.date_string()), msg)
        raise ActiveCommunity(msg)

    new_users, new_user_urls = helpers.load_data("potential_adds")
    helpers.write_log_trash("New users {}".format(helpers.date_string()),
                            new_users)

    post_text_items = [daddy.build_new_text(new_users, 1), "\n"]

    if config.entry_comments:
        post_text_items.append("[Comments for entry]({})".format(
            daddy.build_and_post_gist(new_users, new_user_urls)))
    if config.stats_section:
        post_text_items.append("# Info:\n\n")
        post_text_items.append("- {} users added".format(len(new_users)))
        diff = len(new_users)
        change = "+{}".format(diff) if diff >= 0 else str(diff)
        post_text_items.append("- Membercap: {} ({})".format(
            len(new_users), change))

    post_text = "\n".join(post_text_items)

    title = config.main_log_title
    if config.title_date:
        title = helpers.date_string() + " - " + title
    if config.title_number:
        stats["log_count"] += 1
        title += " #{}".format(stats["log_count"])

    daddy.make_post(title, post_text, reddit)

    if config.change_title:
        updates.change_title()

    daddy.add_users(new_users, reddit)
    daddy.flair_users(new_users, reddit, config.flair_new)

    if config.update_sidebar:
        updates.update_sidebar(new_users)

    stats["last_full_run"] = time.time()
    helpers.write_data("stats", stats)
    helpers.write_data("user_list", new_users)
    helpers.write_data("participated", [])
Пример #2
0
def main(new_users):
    reddit = helpers.initialize_reddit()

    user_list = helpers.load_data("user_list")

    new_users = [user for user in new_users if user not in user_list]

    if not new_users:
        helpers.write_log_trash(
            "All re-adds already on the memberlist {}".format(helpers.date_string()),
            "",
        )
        sys.exit(1)

    daddy.add_users(new_users, reddit)
    daddy.flair_users(
        new_users, reddit, config.flair_normal, number_adjustment=len(user_list)
    )

    insert_users_to_userlist(new_users)
    user_list = helpers.load_data("user_list")

    title, body = build_post(new_users, len(user_list) - len(new_users) + 1)
    daddy.make_post(title, body, reddit, distinguish=True, sticky=False)

    if config.update_sidebar:
        updates.update_sidebar(user_list)
Пример #3
0
def build_post(new_users, number):
    title = "User re-add"
    if config.title_date:
        title = helpers.date_string() + " - " + title
    if config.title_number:
        stats = helpers.load_data("stats")
        stats["re-add count"] += 1
        readd_count = stats["re-add count"]
        helpers.write_data("stats", stats)
        title += " #{}".format(readd_count)

    lines = []
    for user in new_users:
        lines.append(r"- \#{} /u/{}".format(number, user))
        number += 1

    if config.stats_section:
        cap = number - 1
        diff = len(new_users)
        lines.append(
            "\n# Info:\n\n- 0 users kicked\n- {} users added\n- Membercap: {} (+{})".format(
                diff, cap, diff
            )
        )

    body = "  \n".join(lines)

    return title, body
Пример #4
0
def add_users(users, reddit):
    for user in users:
        if not config.testing:
            try:
                reddit.subreddit(config.target_subreddit).contributor.add(user)
            except (praw.exceptions.PRAWException, prawcore.PrawcoreException):
                error = traceback.format_exc() + "\n"
                helpers.write_log_trash(
                    "Exception: add_users() {}".format(helpers.date_string()),
                    error)
        else:
            print("Testing: added {}.".format(user))
Пример #5
0
def make_post(title,
              text,
              reddit,
              *,
              distinguish=config.distinguish_log,
              sticky=config.sticky_log):
    if not config.testing:
        try:
            new_post = reddit.subreddit(config.target_subreddit).submit(
                title, selftext=text, send_replies=False)
        except (praw.exceptions.PRAWException, prawcore.PrawcoreException):
            error = traceback.format_exc() + "\n"
            helpers.write_log_trash(
                "Exception: make_post(), submit {}".format(
                    helpers.date_string()), error)
            return
        if distinguish:
            try:
                reddit.submission(id=new_post.id).mod.distinguish(how="yes")
            except (praw.exceptions.PRAWException, prawcore.PrawcoreException):
                error = traceback.format_exc() + "\n"
                helpers.write_log_trash(
                    "Exception: make_post(), distinguish {}".format(
                        helpers.date_string()),
                    error,
                )
        if sticky:
            try:
                reddit.submission(id=new_post.id).mod.sticky()
            except (praw.exceptions.PRAWException, prawcore.PrawcoreException):
                error = traceback.format_exc() + "\n"
                helpers.write_log_trash(
                    "Exception: make_post(), sticky {}".format(
                        helpers.date_string()),
                    error,
                )
    else:
        print("Testing: submitted {}:\n\n{}".format(title, text))
Пример #6
0
def check_permissions(reddit):
    try:
        for m in reddit.subreddit(config.target_subreddit).moderator(
                reddit.user.me()):
            my_permissions = m.mod_permissions

    except (praw.exceptions.PRAWException, prawcore.PrawcoreException):
        err = traceback.format_exc() + "\n"
        helpers.write_log_trash(
            "check_permissions() {}".format(helpers.date_string()), err)
        sys.exit(1)

    # noinspection PyUnboundLocalVariable
    perms = ["access" in my_permissions, "flair" in my_permissions]
    if config.distinguish_log or config.sticky_log:
        perms.append("posts" in my_permissions)
    if config.update_sidebar or config.change_title:
        perms.append("config" in my_permissions)

    if not (all(perms) or "all" in my_permissions):
        msg = "Have: {}\n".format(my_permissions)
        helpers.write_log_trash(
            "Insufficient Permissions {}".format(helpers.date_string()), msg)
        sys.exit(1)
Пример #7
0
def main():
    if config.delay:
        time.sleep(random.randrange(0, config.max_delay * 60))

    daily.main()

    reddit = (helpers.initialize_reddit()
              )  # will exit if Reddit isn't properly initialized
    check_permissions(
        reddit
    )  # will check if bot has all needed permissions; exit on failure
    participated = set(helpers.load_data("participated"))
    stats = helpers.load_data("stats")
    user_list = helpers.load_data("user_list")
    helpers.write_log_trash("User list {}".format(helpers.date_string()),
                            user_list)

    if stats["last_full_run"] + 23 * 60 * 60 > time.time():
        if "--override_time" not in sys.argv:
            msg = 'Less than 23 hours since last run. Exiting. Run with "--override_time" as an option to disregard'
            print(msg)
            helpers.write_log_trash("Failed {}".format(helpers.date_string()),
                                    msg)
            sys.exit(1)

    updated_list, not_participated = segregate_users(user_list, participated)
    helpers.write_log_trash(
        "Not participated {}".format(helpers.date_string()), not_participated)

    flair_and_remove(not_participated, reddit)
    flair_users(updated_list, reddit, config.flair_normal)

    saved_users, saved_urls = check_saved_users()
    valid_users = []
    valid_urls = []
    for i in range(len(saved_users)):
        if (valid_user(saved_users[i], reddit)
                and saved_users[i] not in valid_users
                and saved_users[i] not in updated_list):
            valid_users.append(saved_users[i])
            valid_urls.append(saved_urls[i])

    # helpers.delete_datafile("potential_adds")
    total_needed_users = len(valid_users)
    num_still_needed_users = 0
    new_users, new_user_urls = get_new_users(reddit, num_still_needed_users,
                                             updated_list)
    new_users = valid_users + new_users
    new_user_urls = valid_urls + new_user_urls
    # new_users, new_user_urls = hack_shuffle(new_users, new_user_urls)
    new_users = new_users[:total_needed_users]
    new_user_urls = new_user_urls[:total_needed_users]

    helpers.write_log_trash("New users {}".format(helpers.date_string()),
                            new_users)

    post_text_lines = [
        build_removed_text(user_list, not_participated),
        "\n",
        build_new_text(new_users,
                       len(updated_list) + 1),
        "\n",
    ]

    if config.entry_comments:
        try:
            post_text_lines.append("\n[Comments for entry]({})".format(
                build_and_post_gist(new_users, new_user_urls)))
        except Exception:  # can fail!
            pass  # lol
    if config.stats_section:
        post_text_lines.append("\n# Info:\n")
        post_text_lines.append("- {} users kicked".format(
            len(not_participated)))
        post_text_lines.append("- {} users added".format(len(new_users)))
        diff = len(new_users) - len(not_participated)
        change = "+{}".format(diff) if diff >= 0 else str(diff)
        post_text_lines.append("- Membercap: {} ({})".format(
            (len(updated_list) + len(new_users)), change))

    post_text = "\n".join(post_text_lines)

    title = config.main_log_title
    if config.title_date:
        title = helpers.date_string() + " - " + title
    if config.title_number:
        stats["log_count"] += 1
        title += " #{}".format(stats["log_count"])

    make_post(title, post_text, reddit)

    if config.change_title:
        updates.change_title()

    add_users(new_users, reddit)
    flair_users(new_users,
                reddit,
                config.flair_new,
                number_adjustment=len(updated_list))

    updated_list_copy = updated_list[:]
    updated_list_copy.extend(new_users)
    if config.update_sidebar:
        updates.update_sidebar(updated_list_copy)

    stats["last_full_run"] = time.time()
    helpers.write_data("stats", stats)
    helpers.write_data("user_list", updated_list_copy)
    helpers.write_data("participated", [])
Пример #8
0
    # noinspection PyUnboundLocalVariable
    perms = ["access" in my_permissions, "flair" in my_permissions]
    if config.distinguish_log or config.sticky_log:
        perms.append("posts" in my_permissions)
    if config.update_sidebar or config.change_title:
        perms.append("config" in my_permissions)

    if not (all(perms) or "all" in my_permissions):
        msg = "Have: {}\n".format(my_permissions)
        helpers.write_log_trash(
            "Insufficient Permissions {}".format(helpers.date_string()), msg)
        sys.exit(1)


if __name__ == "__main__":
    # noinspection PyBroadException
    try:
        main()
    except:
        with open(
                os.path.join(
                    helpers.folder_path(),
                    "log_trash",
                    "ERROR {}".format(helpers.date_string()),
                ),
                "a",
        ) as f:
            traceback.print_exc()
            traceback.print_exc(file=f)