def main():
    # make Slack API connection
    slack = Slacker(SLACK_AUTH_TOKEN)

    # get all students and subjects they do
    student_details, all_subjects = get_student_data(STUDENT_FILE)

    # get users from Slack like {email: (id, username, real name)}
    slack_user_details = get_slack_users(slack)

    # get channels like {channel name: (id, [members])}
    channel_details = get_slack_channels(slack)
    # PP.pprint(channel_details)

    # optionally, clear out non-enrolled students
    if REMOVE_OLD_STUDENTS:
        print("Removing all students from subject channels")
        remove_students(slack, channel_details, slack_user_details,
                        student_details, STAFF_FILE)

    # return

    substitutions = create_substitutions()
    missing_students = set()
    missing_channels = set()
    invited_count = 0
    # now we can find students not in their subject channels
    for email, subjects in student_details.items():
        try:
            slack_id = slack_user_details[email][0]
        except KeyError:
            missing_students.add(email)
            continue
        for subject in subjects:
            channel_name = subject_to_channel(subject, substitutions)
            try:
                # print(email, slack_id, channel_name)
                # PP.pprint(channel_details[channel_name])
                if slack_id not in channel_details[channel_name][1]:
                    print("inviting", email, "to", channel_name)
                    invited_count += 1
                    try:
                        slack.channels.invite(channel_details[channel_name][0], slack_id)
                    except Exception as error:
                        print("ERROR inviting ({})\n".format(error))
                        missing_channels.add(channel_name)
            except Exception as error:
                print("ERROR with {} lookup ({})\n".format(channel_name, error))

    print("Invited people {} times".format(invited_count))
    print("\n{} people not in Slack:\n{}".format(len(missing_students),
                                                 "\n".join(missing_students)))
    # output text file with missing students
    # in form ready for bulk Slack invite (comma separated)
    with open(NONSLACKERS_FILE, "w") as f:
        f.write(", ".join(missing_students))
    if missing_channels:
        print("\nProblem (probably missing) channels: {}\n".format(
            "\n".join(missing_channels)))
def check_channels(slack):
    # these = ['CP1406', 'CP1806', 'CP5632', 'CP5046', 'CP5330']
    # students, subjects = get_group_lists()
    channels = get_slack_channels(slack)
    with open("subjects.txt") as f:
        for line in f:
            s = line[:6]
            channel = subject_to_channel(s)
            if channel not in channels:
                print("ERROR", channel)
def check_channels():
    """Check for missing subject channels based on input file."""
    slack = Slacker(SLACK_AUTH_TOKEN)
    # these = ['CP1406', 'CP1806', 'CP5632', 'CP5046', 'CP5330']
    # students, subjects = get_group_lists()
    substitutions = create_substitutions()
    channel_details = get_slack_channels(slack)
    # PP.pprint(channel_details['cp1404'])
    with open("data/subjects.txt") as f:
        for line in f:
            subject_name = line[:6]
            channel = subject_to_channel(subject_name, substitutions)
            if channel not in channel_details:
                print("ERROR", channel)
def main():
    slack = Slacker(SLACK_AUTH_TOKEN)
    pp = PrettyPrinter(indent=4)

    # get all students and subjects they do
    student_details, all_subjects = get_student_data(STUDENT_FILE)
    # pp.pprint(student_details)

    # get users from Slack
    slack_user_details = get_slack_users(slack, pp)

    # get channels (names and ids)
    channel_details = get_slack_channels(slack)
    # pp.pprint(channel_details)

    missing = []
    invited_count = 0
    # now we can find students not in their subject channels
    for email, subjects in student_details.items():
        try:
            slack_id = slack_user_details[email][0]
            # print(slack_id, email)
        except KeyError:
            missing.append(email)
            continue
        for subject in subjects:
            channel = subject_to_channel(subject)
            try:
                if slack_id not in channel_details[channel][1]:
                    print("inviting", email, "to", channel)
                    invited_count += 1
                    try:
                        slack.channels.invite(channel_details[channel][0], slack_id)
                    except:
                        print("ERROR with Slack call, probably missing channel for {}\n".format(channel))
            except:
                print("ERROR with lookup, probably missing channel for {}\n".format(channel))

    print("Invited people {} times".format(invited_count))
    print("\n{} people not in Slack:\n{}".format(len(missing), "\n".join(missing)))
    # output text file with missing students in form ready for bulk Slack invite (comma separated)
    with open("nonslackers.txt", "w") as f:
        f.write(", ".join(missing))