chess.com api and the search for en passant checkmate
The chess.com API gives you access to a crazy amount of data on games played on the site. Armed with the knowledge that this data was at my fingertips, I set out to do what any sane person would do: find en passant checkmates. For those not in the know, en passant check mate is kind of the king of moves in chess meme circles. So some sort of python script that identified en passant check mates that occured on the site would be of great value to me.
First things first, I would need a method of grabbing lots of games from the api. This would be achieved by looking at players on the site and searching their game archives. As I couldn’t think of any obvious way to get completely random players on the site, I used the API’s lists of all titled players (GM, IM, WIM, etc.) on the site. This is what I came up with ->
def get_archive_urls(titled_urls):
players = []
for url in titled_urls:
title_list = requests.get(url).json()
title_list = title_list['players']
players.extend(title_list)
archive_urls = []
for username in players:
games = 'https://api.chess.com/pub/player/' + username + '/games/2022/05'
archive_urls.append(games)
return archive_urls
get_archive_urls([
'https://api.chess.com/pub/titled/GM',
'https://api.chess.com/pub/titled/WGM'
])
This function reads a list of urls, gets a list of all the usernames from the api and then inserts this username into a new url which will allow us to access their games from a particular month. I then returns a list of all of these game archive urls.
The next order of business is taking this list of urls and turning it into a list of games. It looks quite similar to the previous example ->
def grab_games(archive_urls):
games = []
for url in archive_urls:
archive = requests.get(url).json()
archive_games = archive['games']
games.extend(archive_games)
return games
Feeding the first function into the second ->
grab_games(get_archive_urls([
'https://api.chess.com/pub/titled/GM',
'https://api.chess.com/pub/titled/WGM'
]))
We get a very long list of json objects (is that the right phrase? um). Each corresponding to one of games played by GMs and WGMs on chess.com during May of 2022. Come back next time to see what we can do with this very long list. Here’s a taster of what the list looks like printed to a terminal - lots of possiblities.