-1

Sequel to this question: Live statistics chess960 from chess.com?

So suppose I go to like

https://api.chess.com/pub/player/gmwso/games/2020/12

or

https://api.chess.com/pub/player/gmwso/games/2020/12/pgn

there's gonna be a bunch of stuff like say

[UTCDate "2018.01.03"]
[WhiteElo "2706"]
[BlackElo "2940"]

How do I get this data into a spreadsheet like column 1 is all the dates, column 2 is the corresponding white elo, column 3 black elo, col4 white username and col5 black username?


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.

BCLC
  • 79
  • 1
  • 3
  • 13
  • Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see what questions you can ask. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman May 01 '21 at 11:43
  • @Progman what's this welcome to stack overflow? my 1st post on the site was 7 years ago. are you the downvoter? – BCLC May 01 '21 at 11:44
  • 1
    @BCLC ... that is a great challenge ! It is easy to fetch data from json, what is much more difficult is then to data in columns. I will have a look at it. Great problem. – Mike Steelson May 01 '21 at 12:05
  • 1
    sure that my regexextract formula could be improved ! – Mike Steelson May 01 '21 at 12:31

1 Answers1

1

From lichess.org, assuming the pgn file has been loaded into your drive (if I catch info directly from lichess.org, google alerts 'Exceeded maximum execution time')

// Mike Steelson

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('↓ lichess.org ↓')
    .addItem('parse PGN file from Drive', 'readPGNFromDrive')
    .addToUi();
}

function readPGNFromDrive() {

  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('from drive')
  var id = sh.getRange('B1').getValue()
  var list = ['Event', 'Site', 'Date', 'White', 'Black', 'Result', 'UTCDate', 'UTCTime', 'WhiteElo', 'BlackElo', 'WhiteRatingDiff', 'BlackRatingDiff', 'Variant', 'TimeControl', 'ECO', 'Termination', 'FEN', 'SetUp', 'sequence']

  sh.deleteRows(2,sh.getLastRow()+2)
  var result=[]
  result.push(list)
  var data = DriveApp.getFileById(id).getBlob().getDataAsString().split(String.fromCharCode(10,10,10))
  for (var i=0; i<data.length; i++) {
    var prov=[]
    var parts = data[i].split(String.fromCharCode(10,10))
    var pparts = parts[0].split(String.fromCharCode(10))
    for (var x=0; x<pparts.length; x++){
      pparts[x]=pparts[x].replace('[','"').replace(']','').replace(' "','":"') // "key":"value"
    }
    var donnees = JSON.parse('{'+pparts.join(',')+'}') // mise au format json
    list.forEach(function(path){
      if (path == 'sequence'){prov.push(parts[1])}
      else {prov.push(donnees.item(path))}
    })
    result.push(prov)
  }
  sh.getRange(2,1,result.length,result[0].length).setValues(result)
}

Object.prototype.item=function(i){return this[i]};

https://docs.google.com/spreadsheets/d/1ZX-YefSfeNx0z6mo7Bo3EY_tOkYcVLT7Ke0fqQUiHNI/copy

Mike Steelson
  • 11,710
  • 2
  • 4
  • 19
  • thanks a million Mike Steelson !! for getting from google drive: perfect!!! for getting from lichess, how about just doing 1 month at a time like with chessdotcom? eg here's what i have for [aug2021](https://lichess.org/api/games/user/flyingjfish?tags=true&clocks=false&evals=false&opening=false&since=1627747200000&until=1630425600000&perfType=chess960) – BCLC Aug 14 '21 at 06:08
  • 1
    same as previous answer, take a new copy of https://docs.google.com/spreadsheets/d/1asiDRDusHYSDXK0c8gR_GuMIC1Nh0GNXiBpYctrLLlA/copy and choose tab pgn file, put the url in A1 and adapt row#2 (already done) – Mike Steelson Aug 14 '21 at 15:51
  • 1
    I will work tomorrow on how to retrieve the different informations that are included in the file (as event, date, white, black ...). You will et then a complete new set of scripts. – Mike Steelson Aug 14 '21 at 20:06
  • work on what tomorrow? looks like it's already finished. so yeah it's really just that all the games return 'Exceeded maximum execution time' but it looks to be ok for just a month at a time? – BCLC Aug 15 '21 at 05:05
  • 1
    since different sources have different data, I will try to retrieve it so you don't have to fill in row # 2 – Mike Steelson Aug 15 '21 at 05:08
  • i don't know what you mean but i already started to create statistics out of the data i got from lichess with the from drive thing. now with the directly from lichess/pgn/api whatever you've done it's gonna be more convenient for me in the future. thanks a million. but if you still wanna do something, then sure i'll check it out and upvote you. but really i can take it from here. again, thanks a million – BCLC Aug 15 '21 at 05:41