diff options
-rw-r--r-- | API/main.py | 48 | ||||
-rw-r--r-- | AllAboutData/getData.py | 9 |
2 files changed, 49 insertions, 8 deletions
diff --git a/API/main.py b/API/main.py index 382cf69..3816fab 100644 --- a/API/main.py +++ b/API/main.py @@ -1,15 +1,28 @@ from fastapi import FastAPI import pandas as pd -import os +import numpy as np -# Making relative paths for the data +app = FastAPI() + +# Making relative paths for the data, on windows slashes would have to be turned around. +# It is probably not the best way. relPathTeams = "../AllAboutData/Data/NBAteams.csv" relPathPlayers = "../AllAboutData/Data/Players/" -app = FastAPI() +# func which return dict with team names as keys and full team names as values +def getTeamNames(): + teamsDf = pd.read_csv(relPathTeams).to_dict() + team_names = {} + for el in range(len(teamsDf["name"])): + team_names.update({teamsDf["name"][el] : teamsDf["full_name"][el]}) + return team_names + + + + @app.get("/teams") -def teamsIntoDict(): +def getTeams(): # Getting the data from csv files and then converting into dict to be send out on get request teamsDf = pd.read_csv(relPathTeams).to_dict() @@ -24,3 +37,30 @@ def teamsIntoDict(): "Conference" : teamsDf["conference"][el], "Division" : teamsDf["division"][el]} }) return teams_dict + + +@app.get("/players/{team_name}") +def getPlayers(team_name: str): + + teamNames = getTeamNames() + team_name = team_name.capitalize() # Capitalizing in case url is given as lowercase + if team_name in teamNames.keys(): + + playersDf = pd.read_csv(relPathPlayers+teamNames[team_name]+".csv") + print(playersDf) + playersDf = playersDf.replace({np.nan:None}) # For Json Nan must be replaced + + players_dict = {} + for el in range(len(playersDf["first_name"])): + players_dict.update({el+1 : + {"first_name" : playersDf["first_name"][el], + "last_name" : playersDf["last_name"][el], + "position" : playersDf["position"][el], + "height_feet" : playersDf["height_feet"][el], + "height_inches" : playersDf["height_inches"][el]} }) + + + return players_dict + else: + return {"Error 404" : "Team name Not Found"} + diff --git a/AllAboutData/getData.py b/AllAboutData/getData.py index 7c64b30..4cea9af 100644 --- a/AllAboutData/getData.py +++ b/AllAboutData/getData.py @@ -84,10 +84,12 @@ def getPlayerData(url, headers): "height_feet" : player["height_feet"], "height_inches" : player["height_inches"]}) - #add player to dataframe + # Add player to dataframe playerDf.loc[len(playerDf)] = playerSeries - #add dataframe to File - playerDf.to_csv(playersDir+teamName+".csv", mode='a', index=False, header=False) + + # Add dataframe to File + hdr = False if os.path.isfile(playersDir+teamName+".csv") else True + playerDf.to_csv(playersDir+teamName+".csv", mode='a', index=False, header=hdr) print("Page "+str(el)+" read.") print("All done, check \"Data\" Dir.") @@ -95,7 +97,6 @@ def getPlayerData(url, headers): -# Requesting and storing data if __name__ == "__main__": getTeamsData(url, headers) getPlayerData(url, headers) |