summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-03-17 18:04:24 +0200
committerRasmus Luha <rasmus.luha@gmail.com>2022-03-17 18:04:24 +0200
commit6d6a6a8ac331135aa26320c532f47f25872d4243 (patch)
treee83b099a025899bcfa41559a1a4c6588e00f8724
parent98e375682a50f3cdeddc8e6af022d1e8d12f608d (diff)
fix importing modules, add readme
-rw-r--r--README.md20
-rw-r--r--__init__.py0
-rw-r--r--api/main.py2
-rw-r--r--api/routers/get.py37
-rw-r--r--api/utils.py4
5 files changed, 51 insertions, 12 deletions
diff --git a/README.md b/README.md
index e69de29..1aca4e8 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,20 @@
+# Overview
+This project has the functionality to fetch data about NBA and store it in csv format files.
+It also has an API to serve the data. API can be accessed here.
+
+## How to use
+### API
+From the api you can fetch data as follows:
+* For teams, the endpoint is "/teams" and you can fetch data as follows:
+'''
+ https//:{api_url}/teams
+'''
+* For player information, the endpoint is "/players/{team_name}
+ - you can get the team name from fetching the teams data.
+ - Examples: Bulls, Celtics, Hawks, Nets, Hornets.
+ '''
+ https//:{api_url}/players/celtics
+ https//:{api_url}/players/hawks
+ '''
+ - NB: you shouldn't use the "full_name", like "Chicago Bulls".
+
diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/__init__.py
diff --git a/api/main.py b/api/main.py
index 8068243..e4d90f8 100644
--- a/api/main.py
+++ b/api/main.py
@@ -1,5 +1,5 @@
from fastapi import FastAPI
-from routers import get
+from .routers import get
import pandas as pd
app = FastAPI()
diff --git a/api/routers/get.py b/api/routers/get.py
index bbbd26d..7842cc3 100644
--- a/api/routers/get.py
+++ b/api/routers/get.py
@@ -1,18 +1,37 @@
from fastapi import FastAPI, APIRouter
import numpy as np
import pandas as pd
-import sys
-
-#sys.path.append("/home/rasmus/Proge/STACC/api")
-from .. import utils
router = APIRouter(tags=["Get requests"])
+
+# Making relative paths. On windows slashes would be backwards
+# Though, it is probably not a good way. And the API has to be activated
+# from the project dir, for the paths to match.
+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
+
+
+
@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()
+ # Converting the data and converting into dict to send
+ teamsDf = pd.read_csv(relPathTeams).to_dict()
teams_dict = {}
for el in range(len(teamsDf["name"])):
@@ -29,12 +48,12 @@ def getTeams():
@router.get("/players/{team_name}")
def getPlayers(team_name: str):
- teamNames = utils.getTeamNames()
+ 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(utils.relPathPlayers+teamNames[team_name]+".csv")
- playersDf = playersDf.replace({np.nan:None}) # For Json Nan must be replaced
+ playersDf = pd.read_csv(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"])):
diff --git a/api/utils.py b/api/utils.py
index c04bdf1..069f072 100644
--- a/api/utils.py
+++ b/api/utils.py
@@ -2,8 +2,8 @@ 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/"
+relPathTeams = "../../AllAboutData/Data/NBAteams.csv"
+relPathPlayers = "../../AllAboutData/Data/Players/"
def getTeamNames():
'''