Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

extract - Live statistics chess960 from chess.com?

Cross-posted chess se, but nothing.


Both lichess and chess.com have the feature to play the variant chess960 live. However, only lichess has a graph showing how your live chess960 rating has changed over time. Lichess also shows other statistics like highest, lowest, best wins, worst losses, average opponent rating, etc. (chess.com does have this for correspondence chess960 though.)

I could create my own graph and statistics in Excel/Google Sheets by manually recording each game's date and my rating afterwards indicated beside my username, but...

Question: Is there a way to obtain, or what in general is the way to go about obtaining, ratings after each chess960 game using some kind of script that sees a player's public profile and then extracts the data?

I have a feeling this kind of script has been done before even if this was not specifically done for chess.com's live chess960. The script doesn't have to graph (pretty easy to do once you have to the data: just use excel/google sheets). I just need the script to collect all the dates and rating numbers for each line of the user's games.


Edit 1: Not sure of on-topic, off-topic stuff on stack of, but i've posted on stack of before. My 1st post was in 2014. It seems these post is getting negative reaction due to that I seem like I'm asking to be spoon fed or something. I don't believe spoon feeding is necessarily an issue here if it's not some homework thing, and spoon feeding is not necessarily what I am asking or at least intend (or 'am intending' ?) to ask anyway. You could just give me the general ideas. For example, if this is to do with 'scraping' or something, then just say so.'

However, I don't quite see this question as any different as like these:

  1. How do I get notified if SE tweets my question? --> Here you could argue I'm asking about se itself on se, so it should be allowed. I've asked chess.com people, but they haven't replied to me, so here I am.

  2. Pricing when arbitrage is possible through Negative Probabilities or something else --> I mean is the guy spoonfeeding or whatever by writing the script?

Edit 2: Additionally, what I'm trying to get at in this post is avoiding the concept of reinventing the wheel/wheel reinvention. I mean, I can't possibly be the 1st person in the history of the internet to ever want to extract their data from chess.com or lichess or something. Plus, chess is a game that has been around for awhile. It isn't like csgo or valorant w/c is relatively new. I really do not see any point A - to look up myself how to do go about extracting data from a site as an alternative to manually typing it up myself and of course B - to manually type it up myself when it would seem pretty weird if there weren't already readily available methods to do this.


Update 2: Fixed now. see the 'json' vs the 'preformed'. WOW.

Update 1: It appears Mike Steelson has an answer here, where the code is given as

=arrayformula( regexextract(split( substitute(substitute(substitute(getDataJSON(A1;"/games";"/pgn");"[";"");"]";"");"""";"") ;char(10));"s.*") )

with an example given here

https://docs.google.com/spreadsheets/d/1MX1o5qdy0K3gTMzbimUV3SmFf-0XPCSJ8Vz4IjI-8Ak/copy

It appears there's a problem when it gets to the case of chess960 only. Consider for example this player: Replacing 'gmwso' with the player's username will yield a weird output. i imagine the output will be messier for mixed chess960 and chess.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("


").map {
        it.split('
').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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...