call the last 10 games

Sort:
Italian-Player

Hi guys, I would like to call the last 10 games from my archive, do you have an example of code to do this? 

skelos

What language are you using?

The steps are reasonably simple, and I could work up an example (by simplifying some existing code) in perl, but anything else I don't have right to hand.

1. Download the list of archives (yes, you want this, else you don't know what months there are games for)

2. Working backwards from latest to earliest download each and

    a) Sort by EndDate (I think now done for us, but it's safe)

    b) Add games from that archive up to ten

    c) If you don't have ten games from the first archive file, proceed to the next and repeat from (a).

 

If you have questions, or do want a perl example, ask. I won't spend the time to create one if that's not the language you're using. And it probably isn't ...

Italian-Player

my code is very easy

how can I turn it?

 

<?php
$url = 'https://api.chess.com/pub/player/erik/games/2018/07/pgn';
$chh = curl_init();
//curl_setopt($chh,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chh,CURLOPT_HEADER, false);
curl_setopt($chh,CURLOPT_FOLLOWLOCATION, true);
curl_setopt($chh,CURLOPT_URL,$url);
//curl_setopt($chh,CURLOPT_REFERER,$url);
curl_setopt($chh,CURLOPT_RETURNTRANSFER,TRUE);
$result=curl_exec($chh);
curl_close($chh);
file_put_contents('/output.pgn',$result);
?>

skelos

That code is not close to what you need:

1. You're assuming that there is an archive for this month; the documentation says to collect the list of archives first as their may be (and are, for some members) missing months where they didn't complete a game

2. You will need to parse the JSON returned to you by curl to extract the PGN which is all one file

3. You then need to ensure the sorting is correct, take up to ten games and if there were fewer, go back to an earlier month.

 

Your code might be simple, but I'm sorry it's not nearly complete.

skelos

I need to correct myself; it is too long since I wrote my game download code and I'd forgotten some details. sad.png

1. (unchanged) fetch the list of available archives for the player

2. (unchanged) fetch the most recent archive

3. (changed) parse the JSON of the archive, checking order by end_date, and for games you want extract the PGN from the JSON for that game. If you don't get ten games from the most recent archive, go back to the previous one from #1 and fetch it (step two, although now not the most recent archive) and apply step #3 again.

 

My error was mis-remembering that the PGN data is stored per-game within the JSON, and not per-month.

My own download code turns the downloaded code into per-month PGN files (rated and unrated) with usually multiple games, as I already had code which parsed files downloaded via the web interface to players' archives. It may be time to re-write that code as I want a new feature more easily obtained with the full JSON data to hand.

 

My apologies for the partially incorrect initial information.

Italian-Player

thanks skelos , in the end I chose another solution, download the first month and then all the others in the append, 'it is important to have the last, even if they are not precise 10

skelos

Thanks for the update, @Italian-Player. If you used PHP, is it appropriate to create a thread for it? There are threads for Python and Perl at least, and it would be nice to see information about how to access api.chess.com from more languages.

Italian-Player

yes it is. share own code for help and example, the api are not complete for many thinks