summaryrefslogtreecommitdiff
path: root/getData.py
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-03-16 02:46:20 +0200
committerRasmus Luha <rasmus.luha@gmail.com>2022-03-16 02:46:20 +0200
commitb88db78b1e2885d51f3edd7084d5026b400daa35 (patch)
tree5ce4af67cb07da2eb2a4b14667b39dd08676b427 /getData.py
parentd834c529ae6c0c2d99888c23a37752e8de4fb127 (diff)
Players requests
Diffstat (limited to 'getData.py')
-rw-r--r--getData.py45
1 files changed, 41 insertions, 4 deletions
diff --git a/getData.py b/getData.py
index 466ed45..db11dc3 100644
--- a/getData.py
+++ b/getData.py
@@ -21,15 +21,15 @@ else:
playersDir = "Data/Players"
# Requesting data about NBA teams
-url = "https://free-nba.p.rapidapi.com/teams"
+url = "https://free-nba.p.rapidapi.com/"
querystring = {"page":"0"}
headers = {
- 'x-rapidapi-host': "free-nba.p.rapidapi.com",
- 'x-rapidapi-key': API_KEY
+ 'x-rapidapi-host': "free-nba.p.rapidapi.com",
+ 'x-rapidapi-key': API_KEY
}
# Adding data to teams file
-response = requests.request("GET", url, headers=headers, params=querystring)
+response = requests.request("GET", url+"teams", headers=headers, params=querystring)
teamsDf = pd.DataFrame(response.json()["data"])
teamsDf.set_index("id")
teamsDf = teamsDf.drop("id", axis=1)
@@ -37,3 +37,40 @@ teamsDf = teamsDf.drop("id", axis=1)
teamsDf.to_csv(teamsFile)
#######################################################
+# Now requesting players for each team
+# First request is made just to get the amount of pages that must be looped through
+
+querystring = {"per_page":"100","page":"0"}
+response = requests.request("GET", url+"players", headers=headers, params=querystring)
+pageCount = response.json()["meta"]["total_pages"]
+
+
+for el in range(1, pageCount+1):
+
+ # Requesting pages in loop till pageCount
+ querystring = {"per_page":"100","page":el}
+ response = requests.request("GET", url+"players", headers=headers, params=querystring)
+ data = response.json()["data"]
+
+ for player in data:
+ teamName = player["team"]["full_name"]
+ playerDf = pd.DataFrame(columns=["first_name", "last_name", "position", "height_feet", "height_inches"])
+
+ playerSeries = pd.Series({"first_name" : player["first_name"],
+ "last_name" : player["last_name"],
+ "position" : player["position"],
+ "height_feet" : player["height_feet"],
+ "height_inches" : player["height_inches"]})
+
+ #add player to dataframe
+ playerDf.loc[len(playerDf)] = playerSeries
+ #add dataframe to File
+ playerDf.to_csv(playersDir+teamName, mode='a', index=False, header=False)
+ print("Page "+el+" read.")
+print("All done, check Data Dir")
+
+
+
+
+
+