Guest User

Untitled

a guest
Mar 14th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import requests
  2.  
  3. # get all WGMs
  4. title = 'WGM'
  5. url = f'https://api.chess.com/pub/titled/{title}'
  6. r = requests.get(url)
  7. players = r.json()['players']
  8.  
  9. # get stats for each player
  10. player_stats = []
  11. for player in players:
  12. url = f'https://api.chess.com/pub/player/{player}/stats'
  13. r = requests.get(url)
  14. stats = r.json()
  15. player_stats.append(stats)
  16.  
  17. def calculate_average_rating(player_stats, time_control):
  18. ratings = []
  19. for stats in player_stats:
  20. if time_control in stats:
  21. current_rating = stats[time_control]['last']['rating']
  22. ratings.append(current_rating)
  23.  
  24. average_rating = sum(ratings)//len(ratings)
  25. return average_rating
  26.  
  27. # get blitz ratings
  28. average_blitz_rating = calculate_average_rating(player_stats, 'chess_blitz')
  29.  
  30. # get bullet ratings
  31. average_bullet_rating = calculate_average_rating(player_stats, 'chess_bullet')
  32.  
  33. # get rapid ratings
  34. average_rapid_rating = calculate_average_rating(player_stats, 'chess_rapid')
  35.  
  36. # get specific player's rating
  37. player = 'nemsko'
  38. url = f'https://api.chess.com/pub/player/{player}/stats'
  39. r = requests.get(url)
  40. player_blitz_rating = r.json()['chess_blitz']['last']['rating']
  41. player_bullet_rating = r.json()['chess_bullet']['last']['rating']
  42. player_rapid_rating = r.json()['chess_rapid']['last']['rating']
  43.  
  44. print(f'average blitz rating for {title}s: {average_blitz_rating}')
  45. print(f'current blitz rating for {player}: {player_blitz_rating}')
  46. print('')
  47.  
  48. print(f'average bullet rating for {title}s: {average_bullet_rating}')
  49. print(f'current bullet rating for {player}: {player_bullet_rating}')
  50. print('')
  51.  
  52. print(f'average rapid rating for {title}s: {average_rapid_rating}')
  53. print(f'current rapid rating for {player}: {player_rapid_rating}')
Advertisement
Add Comment
Please, Sign In to add comment