npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

football-metrics-toolkit

v1.0.1

Published

A Node.js library for collecting football statistics data. This tool provides a simple and flexible API to fetch and parse football statistics from Fbref.

Downloads

134

Readme

Football Metrics Toolkit

A Node.js library for collecting football statistics data. This tool provides a simple and flexible API to fetch and parse football statistics from Fbref. This project is still ongoing active development. More features like player data, international competitions, and more are coming soon.

:warning: Important notice

This package fetches data from FBref (Sports Reference). By using this package, you agree to comply with FBref's terms of service and data usage rules:

  • Data is sourced from FBref.com and Sports Reference LLC.
  • You must provide attribution to FBref/Sports Reference when using the data
  • The tool implements rate limiting to avoid excessive requests
  • Not intended for wholesale copying of the FBref database
  • Usage must comply with FBref's Terms of Use

Features

  • 📊 Fetch various types of football statistics (standings, squad stats, player stats)
  • 🔍 Filter data by specific teams and columns
  • 📈 Support for different statistical categories (standard stats, advanced stats, etc.)
  • ⚡ Efficient parsing and data combining
  • 🛡️ Built-in rate limiting and error handling

Installation

npm install football-metrics-toolkit

Quick Start

To import the FootballMetricsClient and make a query, follow the example provided below:

import { FootballMetricsClient, TableType } from 'football-metrics-toolkit';

const client = new FootballMetricsClient();

const data = await client.fetchTable({
    league: 'ligue-1',
    season: '2023-2024',
    tables: [
        TableType.OVERALL_STANDINGS,
        TableType.SQUAD_STATS.STANDARD.FOR,
        TableType.SQUAD_STATS.ADVANCED_GOALKEEPING.FOR
    ],
    teams: ['Paris S-G', 'Lille'],
    cols: ['team', 'wins', 'xg_for', 'gk_psxg_net_per90']
});

console.log(data);

Expected response:

{
  'Paris S-G': { wins: '22', xg_for: '68.8', gk_psxg_net_per90: '+0.24' },
  Lille: { wins: '16', xg_for: '48.6', gk_psxg_net_per90: '+0.16' }
}

Whilte the client is fetching data live logs and warnings will be printed on the console. To disable these warnings, pass in the { console = false } parameter when initializing the client, i. e.

const client = new FootballMetricsClient({ console: false });

When quering for opponent data, the requested statistics must begin with the prefix against_:

const data = await client.fetchTable({
    league: 'premier-league',
    season: '2020-2021',
    // no `tables` parameter, client defaults to extracting data from all possible tables
    team: 'Arsenal',
    cols: ['gk_psxg_net_per90', 'against_gk_psxg_net_per90']
})

Expected response:

{
  Arsenal: { gk_psxg_net_per90: '+0.03', against_gk_psxg_net_per90: '-0.14' }
}

You can also convert the output to CSV format and save it to a .csv file:

import { convertToCSV, saveToCSV } from 'football-metrics-toolkit';

const csv = convertToCSV(data);
await saveToCSV(csv, 'football.csv');

API Reference

FootballStatsClient

The main class for interacting with the API.

Methods

fetchTable(options)

Fetches and parses table data based on the provided options. Parameters:

  • options (Object):
    • league (string): League identifier (e.g. 'ligue-1', 'premier-league'). A list of supported league identifiers can be found here.
    • season (string): Season identifier (e.g. '2023-2024').
    • table(TableType, optional) or tables (Array of TableType, optional): Array of specific table(s) to fetch. If left empty, all available tables will be fetched. A list of TableType definitions can be found here.
    • team (string, optional) or teams (Array of string, optional): Array of specific team names to fetch. If left empty, all teams in the given league will be fetched.
    • cols (Array, optional): Array of column identifiers to include. If left empty, all available columns in the given tables will be fetched.

Returns: Promise<Object>: The fetched and parsed data.

Table Types

Available table types are organized in categories:

TableType.OVERALL_STANDINGS
TableType.HOME_STANDINGS
TableType.AWAY_STANDINGS
TableType.SQUAD_STATS.STANDARD.FOR
TableType.SQUAD_STATS.STANDARD.AGAINST
TableType.SQUAD_STATS.GOALKEEPING.FOR
TableType.SQUAD_STATS.GOALKEEPING.AGAINST
// ... and more

More comprehensive documentation on table types, as well as the columns that they return, can be found here.

Rate Limiting

To comply with FBref's terms of service and avoid overwhelming their servers, this package implements rate limiting:

  • Maximum 20 requests per minute

License

This project is licensed under the MIT License - see the LICENSE file for details.

Legal Notice

This package is not affiliated with, endorsed by, or in any way officially connected with FBref or Sports Reference LLC. All product and company names are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.

Acknowledgements

Data provided by FBref.com and Sports Reference LLC. All data usage must comply with their Terms of Use.

Notes

This project is still under active development, so it might still be a bit unstable. Unaccounted for errors can still happen, and they will be fixed eventually. More features are also coming soon.