Last active
May 11, 2022 11:46
-
-
Save infinitless/2a677c3259023eb0ff9d7dd9ca9606d4 to your computer and use it in GitHub Desktop.
Chess Performance
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 | |
import pandas as pd | |
from tabulate import tabulate | |
import sys | |
pd.set_option('display.max_columns', None) | |
pd.set_option('display.max_rows', None) | |
pd.set_option('display.width', 600) | |
playerurl = "https://www.ecfrating.org.uk/v2/new/api.php?v2/players/name/" | |
playerfname = input("Enter Player's (approx.) First Name (e.g Joh for John): ") | |
playerlname = input("Enter Player's (approx.) Last Name (e.g Smi for Smith): ") | |
URL2 = playerurl+playerfname+"%20"+playerlname | |
response2 = requests.get(URL2) | |
data2 = response2.json() | |
try: | |
mydata2 = pd.DataFrame.from_dict(data2['players']) | |
mydata2.index += 1 | |
mydata2 = mydata2.drop(['member_no', 'gender', 'FIDE_no', 'due_date','club_code','nation','nation2','flag','category'], axis=1) | |
mydata2.rename(columns = {'full_name':'Full_Name', 'club_name':'Club','gender':'Gender','date_last_game':'LastGame'}, inplace = True) | |
mydata2.index.name = "Row" | |
print(tabulate(mydata2, headers='keys', tablefmt='psql')) | |
except: | |
print("Player not found!") | |
sys.exit(1) | |
try: | |
chooseplayer = input("Choose Row Number: ") | |
playerID = mydata2.at[int(chooseplayer),'ECF_code'] | |
baseurl = "https://www.ecfrating.org.uk/v2/new/api.php?v2/games/Standard" | |
howmanygames = input("Enter how many past games you want to see: ") | |
URL = baseurl+"/player/"+playerID+"/limit/"+howmanygames | |
response = requests.get(URL) | |
data = response.json() | |
mydata = pd.DataFrame.from_dict(data['games']) | |
mydata.loc[mydata.score==5,'score']='0.5' | |
mydata.loc[mydata.colour=="w",'colour']='White' | |
mydata.loc[mydata.colour=="b",'colour']='Black' | |
mydata.loc[mydata.colour=="W",'colour']='White' | |
mydata.loc[mydata.colour=="B",'colour']='Black' | |
mydata = mydata[mydata.opponent_no != 0] | |
mydata['score'] = pd.to_numeric(mydata['score'],errors='coerce') | |
mydata['increment'] = pd.to_numeric(mydata['increment'],errors='coerce') | |
mydata['opponent_rating'] = pd.to_numeric(mydata['opponent_rating'], errors='ignore') | |
mydata['player_rating'] = pd.to_numeric(mydata['player_rating'], errors='ignore') | |
mydata['diff_rating'] = mydata['opponent_rating']-mydata['player_rating'] | |
mydata = mydata[mydata.score >= 0] | |
avgopprating = mydata['opponent_rating'].mean() | |
totalpercent = mydata['score'].sum() | |
averageopponent = mydata['diff_rating'].mean() | |
myrating = mydata['player_rating'].iloc[0] | |
player_rating_list = mydata['player_rating'].tolist() | |
player_rating_filter = [value for value in player_rating_list if value > 0] | |
ratingdiff = myrating - player_rating_filter[-1] | |
mydata.rename(columns = {'player_rating':'NewRating', 'game_date':'Date','colour':'Colour','score':'Score'}, inplace = True) | |
mydata.round(0) | |
mydata.index.name = "Row" | |
mydata['Versus'] = mydata['opponent_name'].map(str) + ' (' + mydata['opponent_rating'].map(str) + ')' | |
mydata.reset_index() | |
mydata.index += 1 | |
wins = mydata.loc[mydata['Score'] == 1, 'Score'].count() | |
draws = mydata.loc[mydata['Score'] == 0.5, 'Score'].count() | |
losses = mydata.loc[mydata['Score'] == 0, 'Score'].count() | |
winswithW = mydata.loc[(mydata['Score']==1) & (mydata['Colour'] == 'White'), 'Score'].count() | |
winswithB = mydata.loc[(mydata['Score']==1) & (mydata['Colour'] == 'Black'), 'Score'].count() | |
print(f"Statistics:") | |
print(f"WDL: {wins} Wins, {draws} Draws and {losses} Losses") | |
print(f"Wins with White: {winswithW}") | |
print(f"Wins with Black: {winswithB}") | |
print(f"Percentage Score: {(100*int(totalpercent)/int(howmanygames)):.0f} %") | |
print(f"Current Rating: {myrating:.0f}") | |
print(f"Rating change: {ratingdiff:.0f}") | |
print(f"Average Opponent Rating: {avgopprating:.0f}") | |
mydata = mydata.drop( | |
['event_code', 'opponent_name', 'NewRating', 'opponent_rating', 'event_name', 'club_code', 'org_name', 'section_title', 'opponent_no', 'increment', | |
'diff_rating'], axis=1) | |
print(tabulate(mydata, headers='keys', tablefmt='psql')) | |
except: | |
print("No games in database!") | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment