My firsts tests with Chess.com API (some examples with JSON manipulations)

Sort:
skelos

Example code that didn't belong here removed; see the "Working code" thread or go directly to github:

https://github.com/cc-skelos/chess-com-perl

https://github.com/cc-skelos/chess-com-perl/releases

 

skelos

The PGN data for games-in-progress so that I can run PGN Spy over them when there are not enough completed games:

$ ./current-games skelos

[Event "Love is a Battlefield - Board 82"]

[Site "Chess.com"]

[Date "2017.08.20"]

[Round "1"]

[White "KYShawn1"]

[Black "skelos"]

[Result "*"]

[WhiteElo "1660"]

[BlackElo "1477"]

[TimeControl "1/259200"]

[StartTime "07:00:11"]

[Link "https://www.chess.com/daily/game/172818880"]

 

1. e4 c6 2. d4 d5 3. e5 Bf5 4. Nf3 e6 5. Be2 Nd7 6. O-O h6 7. b3 Ne7 8. c4 Ng6 9. Be3 Bb4 10. a3 Ba5 11. cxd5 cxd5 12. b4 Bc7 13. Bd3 Bxd3 14. Qxd3 Nb6 *

 

[Event "1500-1600  The 13th Legion vs TPOC - Board 1"]

...

skelos

The perl code fragment that does the JSON unpacking and output. For anyone unused to perl and references all the $, % and @ characters will look ugly. There's a reason for them, you get used to them, and they may still be ugly. happy.png

 

my $data = decode_json($res->content);

my $game_data = %$data{'games'};

for my $ref (@$game_data) {

    print $$ref{'pgn'}, "\n\n";

}

andreamorandini
skelos wrote: 

my $ua = LWP::UserAgent->new;

$ua->agent("LetMeBeOhGreatCloudflare/1.0");

 

 

@skelos Thank you for your tests, we will highlight in the documentation that in case someone uses a library he should check that a valid user agent is set. 

skelos

[ Edited for grammar. ]

Perl's LWP sets a valid user agent, simply one that Cloudflare reject.

As anything written to access https://api.chess.com is going to be specific to the to the currently provided endpoints, adding a requirement not to use the default user agent string:

User-Agent: libwww-perl/6.29

seems less good to me than asking Cloudflare not to block the module's default identification for api.chess.com, but it's a small issue once found and documented.

In the end, "whatever works" and I'm already using my current-games script to good effect.

Once it's truly settled what the resolution is to be I'll contribute that script as a working example and place it in the public domain.

andreamorandini

@skelos We have disabled the "User-Agent" check in api.chess.com domain, because it's a measure put in place by CloudFlare to prevent automated harvesting by BOTs, that is actually the main intent of an API. So you should be able to use the default user agent without problems now.

 

When you will put your code in Github (or any other public service of your choice, of course) we will link it in a more visible place to let people experiment with it.

skelos

OK ... I'll figure that out. What I have currently is basically demonstration code only. I put it under "working code" because it does (although give the "clubs" script an invalid user name and the error message it not helpful, I just found out).

Clearly, a public repository and links from here would be more useful that posting things directly here.

Again, I'll look into github (since git has mindshare over Mercurial, which is my go-to source code control system, if only because it hit 1.0 before git!).

When I set something up, I'll post. It would be a Good Thing I believe if there were code examples in common scripting/programming languages, and if perl isn't any longer the "language of the web" it's still popular. happy.png

skelos

If it's appropriate, I'll edit my posts down to links to make this thread more readable? I'm not 100% sure that's the best thing to do, but will accept advice!

bcurtis

I would love if the forums (especially the sticky posts, like "Working Code") were a good resource for future developers to learn about what's going on, like a reference source. That has to be balanced with the conversation that requires many voices and helps us refine things. I think that if you would like to edit your posts to be more of a short description with short examples and a link to learn more, then it might be a nice compromise. However you would like it to read best to some curious developer a year from now!

And we're happy to link to non-github resources, but we'd like them to be code-oriented (e.g., BitBucket) so people know what to expect.

Thank you for sharing your progress!

skelos

@bcurtis I'm afraid I didn't even think of github. A real oops in this day and age.

I'll clean up those posts of mine; I've about six things to do first and then to decide if github is the right place, but git has mindshare, @MGleason uses it for PGN Spy, so it's currently my #1 choice. #2 would be a site of my own.

If there's a preference for github from people here (chess.com and/or others following the thread) I'm happy to hear it, as that would make the choice easy.

skelos

OK, code up on github (see earlier note) and I cleaned up the "Working Code" thread as much someone who can delete a post's contents but not delete the post may. Do feel free to go and delete the "see above" collection of posts; they are useless for the future.

I'm in two minds about editing down the code in this thread. I should definitely have started a new thread, but either we delete the whole troubleshooting exercise (which is now fixed, but the idea that Cloudflare may by intent or accident restore some blocks might be worth keeping?).

Open to advice on this one; will do cleanup if requested, and I give permission (not that it's really required, but I'm saying "I won't mind" if someone goes through and deletes a bunch of my earlier posts in this thread.