[Solved] Live statistics chess960 from chess.com?


Copy of my answer on Chess.SE, in case someone is looking here for an answer.


Yes, it’s possible to obtain the data you want. Chess.com has a REST API which is described in the following news post:

https://www.chess.com/news/view/published-data-api

You can use the following URL to get a list of monthly archives of a players games:

https://api.chess.com/pub/player/{username}/games/archives

In this list you will find URLs which look like this:

https://api.chess.com/pub/player/{username}/games/{YYYY}/{MM}

If you append /pgn to this URL, you will get get all the games in PGN format, which is probably simpler to parse.

Lets look at an example:

https://api.chess.com/pub/player/magnuscarlsen/games/2018/01/pgn

Here you will find games played by Magnus Carlsen in January 2018. This list contains a couple of Chess960 games, which are identified by the following tag:

[Variant "Chess960"]

The following tags will give you the UTC date and time of the game as well as the players ratings at the time:

[UTCDate "2018.01.03"]
[UTCTime "21:50:55"]
[WhiteElo "2706"]
[BlackElo "2940"]

Lichess has also an API to download games, which I already described here.

Code

Here’s some simple Kotlin code to extract the data (you will need to change the file and user name):

import java.io.File

fun main() {
    val data = (File("ChessCom_magnuscarlsen_201801.pgn").readText().trim().split("\n\n\n").map {
        it.split('\n').filter { it.startsWith('[') }.map {
            val t = it.replace(Regex("[\\[\\]]"), "").split(' ', limit = 2)
            t[0] to t[1]
        }.toMap()
    })
    data.forEach {
        if (it["Variant"] == "\"Chess960\"") {
            println("${it["UTCDate"]} ${it["UTCTime"]} ${it[if (it["White"] == "\"MagnusCarlsen\"") "WhiteElo" else "BlackElo"]}")
        }
    }
}

Result:

"2018.01.03" "21:50:55" "2706"
"2018.01.03" "21:09:41" "2727"
"2018.01.03" "19:43:22" "2703"

16

solved Live statistics chess960 from chess.com?