logo科技微讯

NBA赛程检测

作者:科技微讯
日期:2022-02-09
📝 笔记

NBA 比赛有 3 种:

  • 常规赛:10 月到次年 4 月,30 支球队,东西两个联盟,各 3 个赛区,每个赛区 5 个球队,即 30/2/3 = 5;
  • NBA 全明星赛:次年 2 月,常规赛将会暂停 1 周来举行一年一度(1999 年曾因劳资纠纷停办)的 NBA 全明星赛(All-Star Game)
  • 季后赛:4 月或 5 月,东西联盟各 8 支球队,季后赛的最后一轮就是所谓的总决赛,是最后两支球队争夺总冠军,需要对战 7 场,持续多天。

在 nba 的官方微信小程序「NBA 会员」找到的接口:

const api = "https://miniapp.nbaqmq.com/api/nba/games/20220310/date";

球队的 logo:

const api = "https://res.nba.cn/media/img/teams/logos/CHA_logo.svg";

可以通过 espn 的 api 获取每天的 NBA 赛程。

const got = require("got");
const formatDate = require("./utils/formatDate");

async function app() {
  const today = formatDate(new Date()).MMDD.replace(/-/gi, "");
  const yesterday = formatDate(
    new Date(Date.now() - 24 * 60 * 60 * 1000)
  ).MMDD.replace(/-/gi, "");
  const api =
    "https://secure.espn.com/core/nba/schedule?xhr=1&render=true&device=mobile&country=hk&lang=en&region=us&site=espn&edition-host=espn.com&site-type=full";
  const json = await got(api).json();
  const scheduleToday = json.content.schedule[today].games;
  const scheduleYesterday = json.content.schedule[yesterday].games;
  //筛选出距离现在 24 小时内开始的赛程,也可以筛选出距离现在还有 5 分钟就要开始的赛程
  const match = [...scheduleToday, ...scheduleYesterday]
    .filter((item) => {
      const now = Date.now();
      const gameTime = new Date(item.date).getTime();
      if (gameTime - now > 0 && gameTime - now < 24 * 60 * 60 * 1000) {
        return true;
      }
    })
    .map((item) => {
      delete item.competitions;
      return item;
    });
  console.log(match[0]);
}

app();

打印出来的数据如下:

{
  "date": "2022-02-10T00:00Z",
  "uid": "s:40~l:46~e:401360644",
  "name": "San Antonio Spurs at Cleveland Cavaliers",
  "season": { "year": 2022, "type": 2, "slug": "regular-season" },
  "links": [
    {
      "isExternal": false,
      "shortText": "Gamecast",
      "rel": ["summary", "desktop", "event"],
      "language": "en-US",
      "href": "http://www.espn.com/nba/game/_/gameId/401360644",
      "text": "Gamecast",
      "isPremium": false
    }
  ],
  "id": "401360644",
  "shortName": "SA @ CLE",
  "status": {
    "period": 0,
    "displayClock": "0.0",
    "clock": 0,
    "type": {
      "name": "STATUS_SCHEDULED",
      "description": "Scheduled",
      "id": "1",
      "state": "pre",
      "completed": false,
      "detail": "Wed, February 9th at 7:00 PM EST",
      "shortDetail": "2/9 - 7:00 PM EST"
    }
  }
}
donation赞赏
thumbsup0
thumbsdown0
暂无评论