JavaScript Command To Return All Club Members In Plain Text Format - Anyone?

Sort:
Colby-Covington

Hi, I run a club that features a section where the club member's account names are displayed on a Wall of Fame.

https://www.chess.com/club/the-chess-elite

Example: @Member1 @Member2 @Member 3 .... etc.

So far I have just been using copy/paste to add new members, but it's starting to get a little too much, which is why I'd like to automate the process.

What I would like to achieve:

1.) All club member names in listed, plain text format.

  • Member1
  • Member2
  • Member3
  • Member4
  • Member5

After this I will just use a text parser to put "@" in front of each name to achieve this:

  • @Member1
  • @Member2
  • @Member3
  • @Member4
  • @Member5

2.) All titled player's account names in listed, plain text format. Here possibly with the correct title.

  • CM@Member1
  • FM@Member2
  • IM@Member3
  • GM@Member4

What I've got so far with the help of @APISTOTELHS for direct Firefox Console Input:

var request = new XMLHttpRequest();
request.open('GET','https://api.chess.com/pub/club/the-chess-elite/members',false);
request.send();
var response = JSON.parse(request.responseText);
var list = '';
for(type in response)for(member of response[type])list += member.username + '\r\n';
console.log(list);

My one problem is that https://api.chess.com/pub/club/the-chess-elite/members only returns 171 members instead of the total 490. What am I missing here?

Tricky_Dicky

The end point for club members is split in three activity categories.

Weekly, Monthly and all time.

https://www.chess.com/news/view/published-data-api#pubapi-endpoint-club-members

The code you are using is displaying the third but not the first two or is displaying them in order but overwriting each time so you only see the final list.

 

Colby-Covington

Hello, thank you that makes sense.

If you have the time, could you suggest what needs to be changed about the code to output all three categories, and effectively display all 490 members?

I remember Java basics, but I'm kind of a noob tbh.

APISTOTELHS
Tricky_Dicky έγραψε:

The code you are using is displaying the third but not the first two or is displaying them in order but overwriting each time so you only see the final list.

The code does return all the members.

The first "for" iterates the "weekly", "monthly" and "all_time" sections while the second "for" gets all the members.

@Colby-Covington, If you want the members to be shorted alphabetically and have their title as prefix (much much slower because it then needs to check for titled players one by one):

var request = new XMLHttpRequest();
request.open('GET','https://api.chess.com/pub/club/the-chess-elite/members',false);
request.send();
var response = JSON.parse(request.responseText);
var list = [];
for(type in response)for(member of response[type])list.push(member.username);
list.sort();
var result = '';
for(member of list)
{
request.open('GET',`https://api.chess.com/pub/player/${member}`,false);
request.send();
response = JSON.parse(request.responseText);
member = '@' + member;
if(response.title)
member = response.title + ' ' + member;
result += member + '\r\n';
}
console.log(result);

Colby-Covington

 

First of all, thank you both so much. You are obviously wizards with this so please bear with me a little.

This is my console output for the command below.

https://pastebin.com/LhZxWr26

var request = new XMLHttpRequest();
request.open('GET','https://api.chess.com/pub/club/the-chess-elite/members',false);
request.send();
var response = JSON.parse(request.responseText);
var list = '';
for(type in response)for(member of response[type])list += member.username + '\r\n';
console.log(list);

It keeps returning only 171 members, ending with member Ziryab, so it's probably sorted alphabetically.

Also when I just click https://api.chess.com/pub/club/the-chess-elite/members I can only see those exact 171 members. Weekly, Monthly and All Time, show the same 171 members.

Not really sure why that is tbh.

That is probably why the second command with titled players doesn't work for me either, because these 171 members do not have any titled players in them.

APISTOTELHS

In the output you posted I can see all the 490 members!

APISTOTELHS

Btw, replace the '\r\n' with '\n' to get rid of the empty lines.

APISTOTELHS

Also the code for the titled members is very slow, several minutes slow.

Colby-Covington

Sorry man, you were absolutely right.happy.png  Can I offer you something for this? You really helped me out here, because I was stuck.

One more little question if you don't mind.

Is it possible to display the following separately with @ and title or no title?

  • Only titled players
  • Only untitled players
  • Only 2200+ players
  • Sorted by most recently joined

 

Tricky_Dicky

Try putting the list into a spreadsheet and you can sort and filter as much as you want.

Williamfwm

Definitely second the Excel recommendation. But if you must know....

----------------

In JavaScript you can do .sort() and pass in what's called a "comparator" function to sort an array of items ("weekly": [.....] is an array)

    response.weekly.sort( (a, b) => b.joined - a.joined );


For the other requests you'd need to cross-reference that information with the player info as APISTOTELHS said, since  it's not in the response from that particular API endpoint.

You could append it to the corresponding object in the weekly array as you go, or build a new array with the info added, and then do the sort like above. That's a little more involved, and if I were building this script I'd start to want to cache that information somewhere so I don't do hundreds of API requests each time.

But anyway yes hypothetically if you merged the "titled" information into the objects in the response.weekly array then you do listing only titled:

    onlyTitled = response.weekly.filter(x => x.title)

or only non-titled:
    nonTitled = response.weekly.filter(x => !x.title)

The rating is yet another request (e.g. https://api.chess.com/pub/player/williamfwm/stats - makes sense, since there's many types of ratings) so you'd have to merge that in then filter and sort on it. But pretending you had merged in a fide attribute to every entry in the array then it's the "joined" code except now it's .fide not .joined the comparator is doing the math on