Skip to content

Instantly share code, notes, and snippets.

@cgivre
Created June 16, 2023 13: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 cgivre/348ab1137861e38d47e202063bd6568e to your computer and use it in GitHub Desktop.
Save cgivre/348ab1137861e38d47e202063bd6568e to your computer and use it in GitHub Desktop.
Get BlackHat Registration Updates

If you teach at BlackHat, and you're like me and are constantly checking your registration numbers, you can use this handy script to pull your numbers and message you on Slack every time you get a new registration.

from io import StringIO
import logging
import datetime
import pandas as pd
import requests
import slack

FILENAME = "<A CSV FILE>"
SLACK_BOT_TOKEN = "<YOUR SLACK BOT TOKEN>"

# Add Logging
logging.basicConfig(
    filename="<YOUR LOG FILE>",
    filemode="a",
    level=logging.INFO,
    format="%(asctime)s:%(levelname)s:%(message)s",
    datefmt="%Y-%m-%d %I:%M:%S%p",
)


def write_to_slack(class_name: str, new_count: int) -> None:
    """
    Writes the counts to Slack.
    :param class_name: The name of the class in question
    :param new_count: The number of new attendees
    """
    slack_client = slack.WebClient(SLACK_BOT_TOKEN)
    claps = ":clap:" * new_count

    slack_client.chat_postMessage(
        channel='#blackhat',
        text=f"We have a new registration for {class_name}!! There are now {new_count} students registered!! {claps}",
        user="blackhat_updater")


def main():
    username = "<USERNAME>"
    password = "<PASSWORD>"
    req = requests.get(f"https://{username}:{password}@<BLACKHAT URL>stats.json", timeout=50)

    df = pd.read_json(StringIO(req.text))
    current_class_info = df[(df['name'] == '<YOUR COURSE TITLE>') |
                            (df['name'] == '<YOUR OTHER COURSE TITLE')]

    d = datetime.datetime.today()
    last_data = pd.read_csv(FILENAME)

    old_count = int(last_data['filled'][0])
    new_count = int(current_class_info['filled'].iloc[0])
    class_name = "Blackhat 2023 (Weekday)"

    if new_count != old_count:
        current_class_info.to_csv(FILENAME)
        logging.info(f"New registrations on {d} for {class_name}")
        write_to_slack(class_name, new_count)
    else:
        logging.info(f"No new registrations on {d} for {class_name}")

    old_count = int(last_data['filled'][1])
    new_count = int(current_class_info['filled'].iloc[1])
    class_name = "Blackhat 2023 (Weekend)"
    if new_count != old_count:
        current_class_info.to_csv(FILENAME)
        logging.info(f"New registrations on {d} for {class_name}")
        write_to_slack(class_name, new_count)

    else:
        logging.info(f"No new registrations on {d} for {class_name}")


if __name__ == "__main__":
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment