NBA 比赛有 3 种:
在 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®ion=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"
}
}
}