From ab2efcde6a5fac062ace86aa1705dc47ce69f438 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Thu, 17 Mar 2022 00:47:22 +0200 Subject: Little Restructuring --- AllAboutData/getData.py | 32 +++++++++++++------------------- AllAboutData/utils.py | 30 ++++++++++++++++++++++++++++++ AllAboutData/utils/osSpecific.py | 30 ------------------------------ 3 files changed, 43 insertions(+), 49 deletions(-) create mode 100644 AllAboutData/utils.py delete mode 100644 AllAboutData/utils/osSpecific.py (limited to 'AllAboutData') 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.py b/AllAboutData/utils.py new file mode 100644 index 0000000..d5f36cf --- /dev/null +++ b/AllAboutData/utils.py @@ -0,0 +1,30 @@ +import os +import sys + +# terminal commands, which are unfortunately os-specific +def whichOs(): + ''' Returns "windows" if used os is windows. If not, returns "good" ''' + + if sys.platform == "win32": + return "windows" + else: + return "good" + +def deleteDataDir(): + ''' Removes Data directory from working directroy ''' + + if whichOs() == "windows": + os.system("rmdir \s Data") + else: + os.system("rm -r Data") + + +def addDataDir(): + ''' Adds data directory from working directroy ''' + + if whichOs() == "windows": + os.system("mkdir Data\Players") + else: + os.system("mkdir -p Data/Players") + print("Created new empty Data directory") + diff --git a/AllAboutData/utils/osSpecific.py b/AllAboutData/utils/osSpecific.py deleted file mode 100644 index c89e65a..0000000 --- a/AllAboutData/utils/osSpecific.py +++ /dev/null @@ -1,30 +0,0 @@ -import os -import sys - -# terminal commands, which are unfortunately os-specific -def whichOs(): -''' Returns "windows" if used os is windows. If not, returns "good" ''' - - if sys.platform == "win32": - return "windows" - else: - return "good" - -def deleteDataDir(): -''' Removes Data directory from working directroy ''' - - if whichOs() == "windows": - os.system("rmdir \s Data") - else: - os.system("rm -r Data") - - -def addDataDir(): -''' Adds data directory from working directroy ''' - - if whichOs() == "windows": - os.system("mkdir Data\Players") - else: - os.system("mkdir -p Data/Players") - print("Created new empty Data directory") - -- cgit v1.2.3