chesscom

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package chesscom is the library behind the chesscom command line: the HTTP client, request shaping, and the typed data models for Chess.com.

The Client here is the spine every command shares. It sets a real User-Agent, paces requests so a busy session stays polite, and retries the transient failures (429 and 5xx) that any public site throws under load.

Index

Constants

View Source
const BaseURL = "https://" + Host + "/pub"

BaseURL is the root every request is built from.

View Source
const DefaultUserAgent = "chesscom-cli/0.1.0 (github.com/tamnd/chesscom-cli)"

DefaultUserAgent identifies the client to Chess.com.

View Source
const Host = "api.chess.com"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to Chess.com over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client with the given config.

func (*Client) GetCountry

func (c *Client) GetCountry(ctx context.Context, code string) (*Country, error)

GetCountry fetches country info by ISO code.

func (*Client) GetGames

func (c *Client) GetGames(ctx context.Context, username string, year, month, limit int) ([]Game, error)

GetGames fetches games for a player in a given month.

func (*Client) GetLeaderboard

func (c *Client) GetLeaderboard(ctx context.Context, leaderType string) ([]LeaderEntry, error)

GetLeaderboard fetches leaderboard entries for the given type.

func (*Client) GetPage

func (c *Client) GetPage(ctx context.Context, p string) (*Page, error)

GetPage fetches one page by its path.

func (*Client) GetPlayer

func (c *Client) GetPlayer(ctx context.Context, username string) (*Player, error)

GetPlayer fetches a player profile by username.

func (*Client) GetStats

func (c *Client) GetStats(ctx context.Context, username string) (*PlayerStats, error)

GetStats fetches player rating stats.

func (c *Client) PageLinks(ctx context.Context, p string, limit int) ([]*Page, error)

PageLinks is kept for the kit domain driver.

type Config

type Config struct {
	BaseURL   string
	UserAgent string
	Rate      time.Duration
	Timeout   time.Duration
	Retries   int
}

Config holds all tunables for the client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults for the Chess.com API.

type Country

type Country struct {
	Name string `json:"name"`
	Code string `json:"code"`
}

Country is Chess.com country info.

type Domain

type Domain struct{}

Domain is the chesscom driver. It carries no state; the per-run client is built by the factory Register hands kit.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns any accepted input into the canonical (type, id).

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate is the inverse: the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Game

type Game struct {
	URL           string `json:"url"`
	TimeControl   string `json:"time_control"`
	EndTime       int64  `json:"end_time"`
	WhiteUsername string `json:"white_username"`
	BlackUsername string `json:"black_username"`
	WhiteResult   string `json:"white_result"`
	BlackResult   string `json:"black_result"`
	WhiteRating   int    `json:"white_rating"`
	BlackRating   int    `json:"black_rating"`
	PGN           string `json:"pgn,omitempty"`
}

Game is one Chess.com game.

type LeaderEntry

type LeaderEntry struct {
	Rank        int    `json:"rank"`
	Username    string `json:"username"`
	Score       int    `json:"score"`
	Title       string `json:"title,omitempty"`
	CountryCode string `json:"country_code,omitempty"`
	URL         string `json:"url,omitempty"`
}

LeaderEntry is one entry from a Chess.com leaderboard.

type Page

type Page struct {
	ID    string `json:"id" kit:"id"`
	URL   string `json:"url"`
	Title string `json:"title,omitempty"`
	Body  string `json:"body,omitempty" kit:"body"`
}

Page is used by the kit domain driver as the URI-addressable resource.

type Player

type Player struct {
	Username    string `json:"username"`
	Title       string `json:"title,omitempty"`
	URL         string `json:"url"`
	CountryCode string `json:"country_code,omitempty"`
	Location    string `json:"location,omitempty"`
	Followers   int    `json:"followers"`
	PlayerID    int    `json:"player_id"`
	Status      string `json:"status,omitempty"`
	Joined      int64  `json:"joined,omitempty"`
	LastOnline  int64  `json:"last_online,omitempty"`
}

Player is a Chess.com player profile.

type PlayerStats

type PlayerStats struct {
	Username     string `json:"username"`
	BulletRating int    `json:"bullet_rating,omitempty"`
	BlitzRating  int    `json:"blitz_rating,omitempty"`
	RapidRating  int    `json:"rapid_rating,omitempty"`
	DailyRating  int    `json:"daily_rating,omitempty"`
	BulletWins   int    `json:"bullet_wins,omitempty"`
	BulletLosses int    `json:"bullet_losses,omitempty"`
	BulletDraws  int    `json:"bullet_draws,omitempty"`
	BlitzWins    int    `json:"blitz_wins,omitempty"`
	BlitzLosses  int    `json:"blitz_losses,omitempty"`
	BlitzDraws   int    `json:"blitz_draws,omitempty"`
	RapidWins    int    `json:"rapid_wins,omitempty"`
	RapidLosses  int    `json:"rapid_losses,omitempty"`
	RapidDraws   int    `json:"rapid_draws,omitempty"`
}

PlayerStats holds rating info for a Chess.com player.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL