From bda141858158595b82ab02e2425a4109e8ac78e0 Mon Sep 17 00:00:00 2001 From: Matt Donders Date: Thu, 29 Aug 2019 00:54:59 -0400 Subject: [PATCH] Better event handling and documentation. --- README.md | 13 +++++++++++-- function.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 858bde5..ef94fb0 100644 --- a/README.md +++ b/README.md @@ -42,10 +42,19 @@ $ aws lambda update-function-code --function-name --s3-bu ## Testing / Running the Function In order to make this is as *dynamic* as possible, most of the values that can be changed are stored in Environment Variables within the function itself. The following need to be set for this function to properly run. +**Required** - debug_twtr_access_secret - debug_twtr_access_token - debug_twtr_consumer_key - debug_twtr_consumer_secret -- GAMEID (Valid NHL Game ID, ex: 2018020020) - S3_BUCKET (the S3 bucket that holds necessary objects) -- BLANK_SHOTMAP (the key of the blank shotmap image) \ No newline at end of file +- BLANK_SHOTMAP (the key of the blank shotmap image) + +**Optional** +- GAMEID (Valid NHL Game ID, ex: 2018020020) + +The NHL Game ID should be passed in via the event parameter into the handler in a dictionary with key `game_id` as per the below sample. +```python +> print(event) +{'game_id': '2018020020'} +``` \ No newline at end of file diff --git a/function.py b/function.py index a191ec6..1c20514 100644 --- a/function.py +++ b/function.py @@ -1,5 +1,6 @@ import os import tempfile +from datetime import datetime import boto3 import matplotlib.image as mpimg @@ -13,6 +14,42 @@ # These are the only events we want to get coordinates for MAPPED_EVENTS = ('SHOT', 'MISSED_SHOT', 'GOAL') +def get_game_id(event: dict): + """ Takes the event & tries to determine & validate the Game ID. + + Args: + event: event passed into the AWS Lambda + + Returns: + dict: {status, game_id, msg} + """ + + # Get the Game ID from the event parameter passed into the handler or via environment variable + game_id = event.get('game_id') + game_id = game_id if game_id is not None else os.environ.get('GAMEID') + if not game_id or game_id is None: + return {"status": False, "msg": "An NHL Game ID is required for this script to run."} + + # Validate Game ID meets all criteria to actually run this script + # In-Season game validation will happen at the API endpoint + game_id = str(game_id) + season = game_id[0:4] + game_type = game_id[4:6] + game_number = game_id[6:10] + + if int(season) > datetime.now().year: + return {"status": False, "msg": "Invalid season detected in the specified Game ID."} + + if int(game_type) > 4: + return {"status": False, "msg": "Invalid game type detected in the specified Game ID."} + + if int(game_number) > 1271: + return {"status": False, "msg": "Invalid game number detected in the specified Game ID."} + + # If all validations pass, return our game_id + return {"status": True, "game_id": game_id} + + def all_plays_parser(home_team: str, away_team: str, all_plays: dict): """ Takes the JSON object of all game events and generates a pandas dataframe. @@ -181,15 +218,27 @@ def send_shotmap_tweet(completed_path: str, tweet_text: str): def lambda_handler(event, context): + game_id_dict = get_game_id(event) + game_id_status = game_id_dict['status'] + + if not game_id_status: + return {"status": False, "msg": game_id_dict['msg']} + + # If status is True, set the game_id variable + game_id = game_id_dict['game_id'] + # Until we have a trigger for this function, just go get the live feed of a particular game. - game_id = os.environ.get('GAMEID') feed = requests.get(f'https://statsapi.web.nhl.com/api/v1/game/{game_id}/feed/live').json() home_team = feed['gameData']['teams']['home']['name'] away_team = feed['gameData']['teams']['away']['name'] all_plays = feed['liveData']['plays']['allPlays'] - home_df, away_df = all_plays_parser(home_team, away_team, all_plays) + # If all_plays is empty, return a message & exit the script + if not all_plays: + return {"status": False, "msg": "Cannot generate a shotmap or a game that has not yet happened."} + + home_df, away_df = all_plays_parser(home_team, away_team, all_plays) home_df_json = home_df.to_json() away_df_json = away_df.to_json()