import requests
from connect_to_db import create_connection, close_and_commit_connection, execute_bulk_insert_sql, \
    execute_sql_with_return
from config import get_config

if __name__ == "__main__":
    config = get_config()
    connection = create_connection()
    existing_games = execute_sql_with_return(
        connection, "SELECT row_to_json(games.*)"
        "  FROM games")
    existing_players = execute_sql_with_return(
        connection, "SELECT row_to_json(players.*)"
        "  FROM players")
    existing_player_ids = list(map(lambda x: x[0]['id'], existing_players))
    sql_bulk_insert_scripts = []
    for existing_game in existing_games:
        game_data = requests.get(config["api_url"] +
                                 f"/game/{existing_game[0]['id']}/feed/live")
        if game_data.status_code == 200:
            game_data = game_data.json()
            for goalie_id in game_data['liveData']['boxscore']['teams'][
                    'away']['goalies']:
                if str(goalie_id)[0:2] == 'ID':
                    goalie_id = int(str(goalie_id)[2:])
                game_player_data = game_data['liveData']['boxscore']['teams'][
                    'away']['players']['ID' + str(goalie_id)]
                if goalie_id in existing_player_ids and 'goalieStats' in game_player_data[
                        'stats']:
                    sql_bulk_insert_scripts.append(
                        "INSERT INTO game_goalies"
Example #2
0
import requests
from connect_to_db import create_connection, close_and_commit_connection, execute_sql, execute_sql_with_return
from config import get_config

if __name__ == "__main__":
    config = get_config()
    connection = create_connection()
    existing_seasons = execute_sql_with_return(
        connection, "SELECT row_to_json(seasons.*)"
        "  FROM seasons")
    season_ids = list(map(lambda x: x[0]['id'], existing_seasons))
    for season in season_ids:
        season_data = requests.get(config["api_url"] +
                                   f"/teams/10/stats?season={season}")
        if season_data.status_code == 200:
            season_data = season_data.json()['stats'][0]['splits'][0]['stat']
            execute_sql(
                connection, "INSERT INTO season_stats"
                f"    VALUES ({season}"
                f"         , {season_data['gamesPlayed']}"
                f"         , {season_data['wins']}"
                f"         , {season_data['losses']}"
                f"         , {season_data['ot']}"
                f"         , {season_data['pts']}"
                f"         , {season_data['goalsPerGame']}"
                f"         , {season_data['goalsAgainstPerGame']}"
                f"         , {season_data['evGGARatio']}"
                f"         , {season_data['powerPlayPercentage']}"
                f"         , {season_data['powerPlayGoals']}"
                f"         , {season_data['powerPlayGoalsAgainst']}"
                f"         , {season_data['powerPlayOpportunities']}"