Created
October 1, 2018 08:50
-
-
Save z1lc/e1fc47160d298751c90861e5d2117b12 to your computer and use it in GitHub Desktop.
Use nba-players.herokuapp.com API to download all images for NBA players
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def getTeam(team): | |
request = requests.get('https://nba-players.herokuapp.com/players-stats-teams/' + team) | |
if request.status_code == 200: | |
return request.json() | |
else: | |
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code)) | |
def getImageName(playerName): | |
return "Person_" + playerName.replace(" ", "_").replace("'", "") + "_1.png" | |
teams = [ "gsw", "lac", "lal", "pho", "sac", "dal", "hou", "mem", "nor", "sas", "den", "min", "okc", "por", "uth", "bos", "bro", "nyk", "phi", "tor", "chi", "cle", "det", "ind", "mil", "atl", "cha", "mia", "orl", "was"] | |
fortyPlusPlayers = [] | |
allPlayers = [] | |
print("Finding all players.") | |
for team in teams: | |
for player in getTeam(team): | |
name = player["name"] | |
allPlayers.append(name) | |
if (int(player["minutes_per_game"].replace(":", "")) >= 2400): | |
fortyPlusPlayers.append(name) | |
csv = open("players.csv", 'w') | |
for player in fortyPlusPlayers: | |
csv.write(player + ",<img src=\"" + getImageName(player) + "\">\n") | |
csv.close() | |
print("Found {} players; using {} who have played 40 or more games. Downloading all images into 'image' directory." | |
.format(len(allPlayers), len(fortyPlusPlayers))) | |
goodCount = 0; | |
for player in fortyPlusPlayers: | |
nameArr = player.lower().replace("'", "").replace(".", "").split(" ", 1) | |
image = open("image/" + getImageName(player), 'wb') | |
got = requests.get('https://nba-players.herokuapp.com/players/' + nameArr[1].replace(" ", "_") + '/' + nameArr[0]) | |
if (got.status_code != 200): | |
raise Exception("Couldn't get image for player {}.".format(player)) | |
image.write(got.content) | |
image.close() | |
goodCount += 1 | |
if (goodCount % 10 == 0): | |
print("Successfully downloaded {} of {} images.".format(goodCount, len(fortyPlusPlayers))) | |
print("Done.") |
@jiwoongim
The script uses this API hosted on heroku: https://nba-players.herokuapp.com/
Looks like images are from 2017 - 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what year are these players from?
do you have images for the older years too?