diff options
Diffstat (limited to 'API')
-rw-r--r-- | API/main.py | 79 | ||||
-rw-r--r-- | API/routers/get.py | 52 | ||||
-rw-r--r-- | API/utils.py | 18 |
3 files changed, 75 insertions, 74 deletions
diff --git a/API/main.py b/API/main.py index c67c95b..8068243 100644 --- a/API/main.py +++ b/API/main.py @@ -1,82 +1,13 @@ from fastapi import FastAPI +from routers import get import pandas as pd -import numpy as np -import os - -import sys -sys.path.append("--") app = FastAPI() +#CORS STUFF HERE -# 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/" - -# 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.include_router(get.router) -@app.get("/") +@app.get("/", tags=["Index"]) def getIndex(): - return {"Message" : "Hello!"} - - -#@app.get("/sync") -#def syncPlayers(): # Currnetly only works for Unix type systems, which is not good -# os.system("rm " + relPathPlayers + "*") -# getData.getPlayerData(getData.url, getData.headers) - - - -@app.get("/teams") -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() - - teams_dict = {} - for el in range(len(teamsDf["name"])): - teams_dict.update({el+1 : - {"Abbreviation" : teamsDf["abbreviation"][el], - "Name" : teamsDf["name"][el], - "FullName" : teamsDf["full_name"][el], - "City" : teamsDf["city"][el], - "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"} - + return {"Message": "Hello! add /docs for documentation"} diff --git a/API/routers/get.py b/API/routers/get.py new file mode 100644 index 0000000..031b469 --- /dev/null +++ b/API/routers/get.py @@ -0,0 +1,52 @@ +from fastapi import FastAPI, APIRouter +import numpy as np +import pandas as pd +import sys + +sys.path.append("..") +import utils + +router = APIRouter(tags=["Get requests"]) + +@router.get("/teams") +def getTeams(): + + # Getting the data from csv files and then converting into dict to be send out on get request + teamsDf = pd.read_csv(utils.relPathTeams).to_dict() + + teams_dict = {} + for el in range(len(teamsDf["name"])): + teams_dict.update({el+1 : + {"Abbreviation" : teamsDf["abbreviation"][el], + "Name" : teamsDf["name"][el], + "FullName" : teamsDf["full_name"][el], + "City" : teamsDf["city"][el], + "Conference" : teamsDf["conference"][el], + "Division" : teamsDf["division"][el]} }) + return teams_dict + + +@router.get("/players/{team_name}") +def getPlayers(team_name: str): + + teamNames = utils.getTeamNames() + team_name = team_name.capitalize() # Capitalizing in case url is given as lowercase + if team_name in teamNames.keys(): + + playersDf = pd.read_csv(utils.relPathPlayers+teamNames[team_name]+".csv") + 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/API/utils.py b/API/utils.py new file mode 100644 index 0000000..c04bdf1 --- /dev/null +++ b/API/utils.py @@ -0,0 +1,18 @@ +import pandas as pd + +# Making relative paths. On windows slashes would be backwards +# It is probably not the best way. +relPathTeams = "../AllAboutData/Data/NBAteams.csv" +relPathPlayers = "../AllAboutData/Data/Players/" + +def getTeamNames(): + ''' + Returns dictionary with fetched teams' names as keys + and full_names as values. + ''' + + 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 |