Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

api-sports.js

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-sports.js

Robust client to interact with api-sports

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
decreased by-22.22%
Maintainers
1
Weekly downloads
 
Created
Source

api-sports.js

API Sports is a JavaScript library to interact with sports api.

Features (WIP)

  • Football (partial support)
  • Basketball (coming soon)
  • Volleyball (coming soon)
  • Hockey (coming soon)
  • American Football (coming soon)
  • Rugby (coming soon)

Installation

WARNING: This library is still a work in progress. Many features are not implemented yet.

npm install api-sports.js

Example

Watching a football match 📺🍿

Match updates are polled every 60 seconds by default. This library attempts to get the most information with the least amount of requests.

import { FootballEvents, SportsAPI } from 'api-sports.js';

const sports = new SportsAPI();

sports.setTokens({
  football: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
});

// Debug 🪲
sports.football.on(FootballEvents.Debug, console.log);

// Detect when a football match starts
sports.football.on(FootballEvents.Start, async (event) => {
  console.log(`Fixture ${event.fixtureId} started...`);
});

// Detect when a football match ends
sports.football.on(FootballEvents.End, async (event) => {
  console.log(`Fixture ${event.fixtureId} ended...`);
  process.exit(0);
});

// Detect when a football match status is updated
sports.football.on(FootballEvents.StatusUpdate, (event) => {
  const status = event.getStatus();

  console.log(
    `Fixture ${event.fixtureId} status updated (${status.old} -> ${status.new})`
  );
});

// Detect when a football match score is updated
sports.football.on(FootballEvents.ScoreUpdate, async (event) => {
  const scores = event.getScores();

  const oldScore = (scores.old.home ?? 0) + ' - ' + (scores.old.away ?? 0);
  const newScore = (scores.new.home ?? 0) + ' - ' + (scores.new.away ?? 0);

  console.log(
    `Fixture ${event.fixtureId} score updated (${oldScore} -> ${newScore})`
  );
});

// Detect when a football match substitution is updated
sports.football.on(FootballEvents.Substitution, async (event) => {
  const substitutions = event.getSubstitutions();

  const oldTotal = substitutions.old.length;
  const newTotal = substitutions.new.length;

  console.log(
    `Fixture ${event.fixtureId} substitution updated. Total (${oldTotal} -> ${newTotal})`
  );
});

// Your fixture id
const FIXTURE_ID = 1234135;

// this will immediately start watching the fixture. If the fixture date is in the past, it checks
// the status and if it's not finished, it will start watching it. If it's finished, it will not
// start watching it and instead exit with StaleFixture event. If the fixture date is in the
// future, it will register a cron job for that particular fixture and start watching it when the time comes.
// calling this method multiple times will not always result in multiple requests
// it instead creates a batch of 20 requests, reducing the amount of requests made
await sports.football.fixtures.subscriptions.addFixture(FIXTURE_ID);

MIT License

Authored and maintained by 3Bird

Keywords

FAQs

Package last updated on 28 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc