Skip to content

Chess.com public data API wrapper with "isChanged" and priority queue functionality.

License

Notifications You must be signed in to change notification settings

andyruwruw/chess-web-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Lightweight wrapper for the Chess.com public data API

Overview

chess-web-api is a lightweight, unofficial wrapper for the Chess.com public data API.

It includes helper functions for all available endpoints, such as retrieving player data, current daily chess games, monthly archives, club data, tournaments and more.

Please read Chess.com's notes on data currency, language and rate limits on parallel requests before you implement this into your work.

I'll try to keep this library updated if they're API changes, feel free to submit any issues.

All endpoint descriptions were copied from here. It's here for ease of access only. Please go to the site to ensure the return values have not been changed.

Documentation

Installation

Install via node:

$ npm i chess-web-api

Import the module and instantiate the wrapper.

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI();

You are then free to call any of the methods!

chessAPI.getPlayer('andyruwruw')
    .then(function(response) {
        console.log('Player Profile', response.body);
    }, function(err) {
        console.error(err);
    });

Get additional details about a player in a game.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get ratings, win/loss, and other stats about a player's game play, tactics, lessons and Puzzle Rush score.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

This endpoint was removed by Chess.com 8/25/2021. For more information see the forum post.

This endpoint requires a large number of internal resources to maintain, and in the past week it has been used by only one clear developer script with a user-agent that helps us contact the developer. All other access to that endpoint appears to be tests, runaway scripts, or unidentified programs that could work better.

For those looking for a simular endpoint, Player Profile includes a last_online field.

Array of Daily Chess games that a player is currently playing.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Array of Daily Chess games where it is the player's turn to act.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Array of monthly archives available for this player.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Array of Live and Daily Chess games that a player has finished.

Parameters

Name Type Description
username string Username of desired profile.
year string / number Year of matches.
month string / number Month of matches.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Standard multi-game format PGN containing all games for a month.

Parameters

Name Type Description
username string Username of desired profile.
year string / number Year of matches.
month string / number Month of matches.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of clubs the player is a member of, with joined date and last activity date.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of Team matches the player has attended, is partecipating or is currently registered.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of tournaments the player is registered, is attending or has attended in the past.

Parameters

Name Type Description
username string Username of desired profile.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Game data lookup by ID. Includes PGN, metadata, and player info.

For more information on what fields this generates or how it was implemented, see here.

Read Before Using: Fair Usage

chess-web-api's getGameById method is not an official endpoint of Chess.com's Published Data API. It uses a callback from Chess.com's website to get its data.

Therefore it is highly unstable and could be changed without warning or stop functioning. Hammering this endpoint with requests could result in an IP ban from Chess.com. This would not only impact your script, but your access to Chess.com itself.

The use of this endpoint violates Chess.com's Terms of Service, however Chess.com has informed me that as long as the "script is polite", they have no issue and it will go unnoticed. Corrective action will be taken if there is abuse.

There is work to add a simular endpoint to Chess.com, and this library will be updated when that takes place. There is no current timeline there and it could take up to a year.

Please submit an issue if you notice any strange behavior to improve this documentation or make fixes. I'll periodically check to make sure the endpoint is working as intended and make fixes when needed.

Parameters

Name Type Description
id string The game ID.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get additional details about a club.

All club-based URLs use the club's "URL ID" to specify which club you want data for.

The url-ID is the same as found in the URL for the club's web page on www.chess.com. For example, the url-ID of the Chess.com Developer's Club is chess-com-developer-community.

Parameters

Name Type Description
urlID string Club's unique urlID
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of club members (usernames and joined date timestamp), grouped by club-activity frequency. The club-activity is one of this actions.

Parameters

Name Type Description
urlID string Club's unique urlID
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of daily and club matches, grouped by status (registered, in progress, finished).

Parameters

Name Type Description
urlID string Club's unique urlID
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a daily, live and arena tournament.

All tournaments-based URLs use the tournament's "URL ID" to specify which tournament you want data for.

The url-ID is the same as found in the URL for the tournament's web page on www.chess.com. For example, the url-ID of the Chess.com Developer's Club is -33rd-chesscom-quick-knockouts-1401-1600

Parameters

Name Type Description
urlID string Tournaments's unique urlID
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a tournament's round.

Parameters

Name Type Description
urlID string Tournaments's unique urlID
round string / number Round number
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a tournament's round group.

Parameters

Name Type Description
urlID string Tournaments's unique urlID
round string / number Round number
group string / number Group number
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a team match and players playing that match. After the match is finished there will be a link to each player's stats endpoint, in order to get up-to-date information about the player.

All team matches-based URLs use the match "ID" to specify which match you want data for.

The ID is the same as found in the URL for the team match web page on www.chess.com. For example, the ID WORLD LEAGUE Round 5: Romania vs USA Southwest is 12803.

Parameters

Name Type Description
id string / number Id of desired team match.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a team match board. Only in-progress or finished games will be included, so there may be one or two games in this list.

Parameters

Name Type Description
id string / number Id of desired team match.
board string / number Board identifier
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a team match and players playing that match. After the match is finished there will be a link to each player's stats endpoint, in order to get up-to-date information about the player.

All live team matches-based URLs use the match "ID" to specify which match you want data for.

The ID is the same as found in the URL for the team match web page on www.chess.com. For example, the ID Friendly 5+2 is 5833.

Parameters

Name Type Description
id string / number Id of desired live team match.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get details about a team match board. Only in-progress or finished games will be included, so there may be one or two games in this list.

Parameters

Name Type Description
id string / number Id of desired live team match.
board string / number Board identifier
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Get additional details about a country.

All country-based URLs use the country's 2-character ISO 3166 code (capitalized) to specify which country you want data for.

Find their code's here.

Additional countries not listed on that official list have been posted by Chess.com.

Parameters

Name Type Description
iso string Country's ISO identifier
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of usernames for players who identify themselves as being in this country.

Parameters

Name Type Description
iso string Country's ISO identifier
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

List of URLs for clubs identified as being in or associated with this country.

Parameters

Name Type Description
iso string Country's ISO identifier
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Information about the daily puzzle found in www.chess.com.

Parameters

Name Type Description
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Information about a randomly picked daily puzzle.

Parameters

Name Type Description
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Information about Chess.com streamers.

Parameters

Name Type Description
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

It displays information about top 50 player for daily and live games, tactics and lessons.

Parameters

Name Type Description
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Retrieves an array of usernames of players with a given title.

Valid title abbreviations are: GM, WGM, IM, WIM, FM, WFM, NM, WNM, CM, WCM.

Parameters

Name Type Description
titleAbbrev string GM, WGM, IM, WIM, FM, WFM, NM, WNM, CM, WCM.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Priority Queue

The Chess.com public data API does not allow you to make two requests simultaneously. If you have more than two active requests at a time, you'll recieve a 429 Too Many Requests error.

chess-web-api can be initialized with a priority queue. All requests made through the queue will be made as soon as the previous returns.

To use chess-web-api with a queue, instantiate the wrapper with the following option set to true:

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

Using the queue requires the passing of a callback function as a parameter.

To add something to the queue, use the method dispatch.

dispatch(method, callback, parameters, options, callbackParameters, priority)

Adds an item to the priority queue.

The provided callback method will be provided two parameters (response, error). Response is the full request result.

Additional parameters can be passed to the callback in callbackParameters.

The dispatch function determines the difference between parameters to be passed into the chess-web-api method vs the callback function by order.

The first array passed into dispatch will always be passed into the chess-web-api method.

The second array will always be sent to the callback function, with the response to the request as the first parameter. See example.

Parameters

Name Type Description
method function chess-web-api function for request.
callback function Function to be called with result
parameters array Array of parameters to be passed into the method.
options object Added options to the end of the URL (optional)
callbackParameters array Array of parameters to be passed on to the callback method along with the response. (optional)
priority number Priority in queue (1 is highest priority) (optional)

Example:

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

let printResults = function(response, error, sampleParameter1, sampleParameter2) {
    console.log(response.body);
    console.log(sampleParameter1);
    console.log(sampleParameter2);
}

chessAPI.dispatch(chessAPI.getPlayer, printResults, ["andyruwruw"], {}, 1, ["callbackParameter", "anotherCallbackParameter"]);
chessAPI.dispatch(chessAPI.getTitledPlayers, printResults, ["GM"]);
chessAPI.dispatch(chessAPI.getPlayerCurrentDailyChess, printResults, ["andyruwruw"], ["callbackParameter"]);
chessAPI.dispatch(chessAPI.getPlayerCompleteMonthlyArchives, printResults, ["andyruwruw", 2019, 10], {}, ["callback parameter"]);

If you initialize your ChessWebAPI with the queue enabled, you can still call any of the regular functions without using the queue.

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

chessAPI.getTitledPlayers('GM')
    .then(function(response) {
        console.log('Grand Masters', response.body.players);
    }, function(err) {
        console.error(err);
    });

clearQueue()

Should you wish to clear the queue you can run the following method.

Example:

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

let printResults = function(response, error) {
    console.log(response.body);
}

chessAPI.dispatch(chessAPI.getPlayer, printResults, ["andyruwruw"], {}, 1, ["callbackParameter", "anotherCallbackParameter"]);

chessAPI.clearQueue();

Query for Changes

The Chess.com public data API also allows clients to provide an etag from a previous response to check if data since the last request has changed.

This has been implemented into chess-web-api's ifChanged method.

Allows you to make any of the helper functions with the added parameter of the etag provided in the header of the last simular request.

Parameters

Name Type Description
etag string ID of last request made. Found in the header.
method function chess-web-api function for request.
parameters array Array of parameters to be passed into the method.
options object Added options to the end of the URL (optional)
callback function Function to be called with result (optional)

Returns

// If data has changed, response is attatched.
{
    changed: true,
    response: // response from chess.com
}

// If data has not been updated.
{
    changed: false,
}

Example:

var games, etag;

// Gets the games and stores an etag.
var trackGames = async function() {
    let response = await chessAPI.getPlayerCurrentDailyChess('andyruwruw');
    games = response.body.games;
    etag = response.headers.etag;

    // runs checkGames every 10 seconds.
    setInterval(checkGames, 10000);
}

var checkGames = async function() {
    // Makes the request but with ifChanged
    let gamesChanged = await chessAPI.ifChanged(etag, chessAPI.getPlayerCurrentDailyChess, ['andyruwruw']);

    // Updates variables if new data is available.
    if (gamesChanged.changed) {
        games = gamesChanged.response.body.games;
        etag = gamesChanged.response.headers.etag;
    }
}

trackGames();

ENJOY THE WRAPPER

alt text

- Andrew Young