diff options
author | Rasmus Luha <rasmus.luha@gmail.com> | 2022-03-17 00:47:22 +0200 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@gmail.com> | 2022-03-17 00:47:22 +0200 |
commit | ab2efcde6a5fac062ace86aa1705dc47ce69f438 (patch) | |
tree | e91491dfb56459866001ff4a6cd4cee547eec1f1 | |
parent | ecb4b4c8c56d9f720ba004fcfefd40acb996cc7e (diff) |
Little Restructuring
-rw-r--r-- | API/main.py | 79 | ||||
-rw-r--r-- | API/routers/get.py | 52 | ||||
-rw-r--r-- | API/utils.py | 18 | ||||
-rw-r--r-- | AllAboutData/getData.py | 32 | ||||
-rw-r--r-- | AllAboutData/utils.py (renamed from AllAboutData/utils/osSpecific.py) | 6 | ||||
-rw-r--r-- | requirements.txt | 119 |
6 files changed, 103 insertions, 203 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 diff --git a/AllAboutData/getData.py b/AllAboutData/getData.py index 7b503fa..fe00c2d 100644 --- a/AllAboutData/getData.py +++ b/AllAboutData/getData.py @@ -13,17 +13,14 @@ Used API: https://rapidapi.com/theapiguy/api/free-nba/ ''' import os +import utils # Some functions to delete and create directories for data import requests import pandas as pd -from utils import osSpecific # Some functions to delete and create directories for data from dotenv import load_dotenv - -# Loading API key from environment variables. load_dotenv() API_KEY = os.getenv("API_KEY") - # API request details url = "https://free-nba.p.rapidapi.com/" headers = { @@ -31,27 +28,21 @@ headers = { "x-rapidapi-key": API_KEY } -# File name variables to store data in -if osSpecific.whichOs() == "windows": + +# Createing file name variables to store data in +if utils.whichOs() == "windows": teamsFile = "Data/NBAteams.csv" playersDir = "Data\Players\\" else: teamsFile = "Data/NBAteams.csv" playersDir = "Data/Players/" -# Createubg new Data dir to avoid duplicates (due appending) -osSpecific.deleteDataDir() -osSpecific.addDataDir() - - ###### Functions ###### def getTeamsData(url, headers): -''' -Requests Data about NBA teams and stores it. -Takes API url as first and its headers as second argument. -''' + '''Requests Data about NBA teams and stores it. + Takes API url as first and its headers as second argument.''' querystring = {"page": "0"} response = requests.request("GET", url+"teams", headers=headers, params=querystring) @@ -64,10 +55,8 @@ Takes API url as first and its headers as second argument. def getPlayerData(url, headers): -''' -Requests Data about NBA players and stores it, based on teams -Takes API url as first and its headers as second argument. -''' + '''Requests Data about NBA players and stores it, based on teams + Takes API url as first and its headers as second argument.''' print("Stared reading players data") @@ -111,6 +100,11 @@ Takes API url as first and its headers as second argument. if __name__ == "__main__": + + # Creating new Data dir to avoid duplicates (due appending) + utils.deleteDataDir() + utils.addDataDir() + getTeamsData(url, headers) getPlayerData(url, headers) diff --git a/AllAboutData/utils/osSpecific.py b/AllAboutData/utils.py index c89e65a..d5f36cf 100644 --- a/AllAboutData/utils/osSpecific.py +++ b/AllAboutData/utils.py @@ -3,7 +3,7 @@ import sys # terminal commands, which are unfortunately os-specific def whichOs(): -''' Returns "windows" if used os is windows. If not, returns "good" ''' + ''' Returns "windows" if used os is windows. If not, returns "good" ''' if sys.platform == "win32": return "windows" @@ -11,7 +11,7 @@ def whichOs(): return "good" def deleteDataDir(): -''' Removes Data directory from working directroy ''' + ''' Removes Data directory from working directroy ''' if whichOs() == "windows": os.system("rmdir \s Data") @@ -20,7 +20,7 @@ def deleteDataDir(): def addDataDir(): -''' Adds data directory from working directroy ''' + ''' Adds data directory from working directroy ''' if whichOs() == "windows": os.system("mkdir Data\Players") diff --git a/requirements.txt b/requirements.txt index a7ee8de..f7722cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,116 +1,21 @@ -alabaster==0.7.12 anyio==3.5.0 -apparmor==3.0.4 -appdirs==1.4.4 -argon2-cffi==21.3.0 -argon2-cffi-bindings==21.2.0 -asttokens==2.0.5 -attrs==21.4.0 -Babel==2.9.1 -backcall==0.2.0 -beautifulsoup4==4.10.0 -bleach==4.1.0 -btrfsutil==5.16.2 -CacheControl==0.12.6 -cffi==1.15.0 -chardet==4.0.0 -colorama==0.4.4 -contextlib2==0.6.0.post1 -cryptography==36.0.1 -debugpy==1.5.1 -decorator==5.1.1 -defusedxml==0.7.1 -distlib==0.3.4 -distro==1.6.0 -docutils==0.16 -entrypoints==0.4 -executing==0.8.3 -html5lib==1.1 +asgiref==3.5.0 +certifi==2021.10.8 +charset-normalizer==2.0.12 +click==8.0.4 +fastapi==0.75.0 +h11==0.13.0 idna==3.3 -imagesize==1.3.0 -ipykernel==6.9.1 -ipython==8.1.1 -ipython-genutils==0.2.0 -ipywidgets==7.6.5 -isc==2.0 -jedi==0.18.1 -Jinja2==3.0.3 -json5==0.9.6 -jsonschema==4.4.0 -jupyter==1.0.0 -jupyter-client==7.1.2 -jupyter-console==6.4.3 -jupyter-core==4.9.2 -jupyter-server==1.13.5 -jupyterlab==3.3.1 -jupyterlab-pygments==0.1.2 -jupyterlab-server==2.10.3 -jupyterlab-widgets==1.0.2 -LibAppArmor==3.0.4 -libtorrent===2.0.5-build-libtorrent-rasterbar-src-libtorrent-rasterbar-2.0.5-bindings-python -MarkupSafe==2.0.1 -matplotlib-inline==0.1.3 -mistune==0.8.4 -more-itertools==8.10.0 -msgpack==1.0.3 -nbclassic==0.3.6 -nbclient==0.5.13 -nbconvert==6.4.4 -nbformat==5.2.0 -nest-asyncio==1.5.4 -notebook==6.4.8 -notebook-shim==0.1.0 numpy==1.22.3 -ordered-set==4.0.2 -packaging==20.9 pandas==1.4.1 -pandocfilters==1.5.0 -parso==0.8.3 -pep517==0.12.0 -pexpect==4.8.0 -pickleshare==0.7.5 -ply==3.11 -progress==1.6 -prometheus-client==0.13.1 -prompt-toolkit==3.0.28 -ptyprocess==0.7.0 -pure-eval==0.2.2 -pycparser==2.21 -pygame==2.1.2 -Pygments==2.11.2 -pyOpenSSL==21.0.0 -pyparsing==2.4.7 -pyrsistent==0.18.1 +pydantic==1.9.0 python-dateutil==2.8.2 +python-dotenv==0.19.2 pytz==2021.3 -pyzmq==22.3.0 -qtconsole==5.2.2 -QtPy==2.0.1 -requests==2.27.0 -resolvelib==0.5.5 -retrying==1.3.3 -Send2Trash==1.8.0 +requests==2.27.1 six==1.16.0 sniffio==1.2.0 -snowballstemmer==2.2.0 -soupsieve==2.3.1 -Sphinx==4.4.0 -sphinxcontrib-applehelp==1.0.2 -sphinxcontrib-devhelp==1.0.2 -sphinxcontrib-htmlhelp==2.0.0 -sphinxcontrib-jsmath==1.0.1 -sphinxcontrib-qthelp==1.0.3 -sphinxcontrib-serializinghtml==1.1.5 -stack-data==0.2.0 -team==1.0 -terminado==0.13.3 -testpath==0.6.0 -toml==0.10.2 -tomli==2.0.0 -tornado==6.1 -traitlets==5.1.1 +starlette==0.17.1 +typing_extensions==4.1.1 urllib3==1.26.8 -wcwidth==0.2.5 -webencodings==0.5.1 -websocket-client==1.3.1 -widgetsnbextension==3.5.2 +uvicorn==0.17.6 |