Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 LCK 比赛日历 #9

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,112 @@ function buildFolders(games) {
});
}

async function dealLCKGames() {
// 请求数据
let games = []
let yearStr = ""
page = 1
// 请先设置环境变量,Github Actions 需要在仓库 Settings - Security - Secrets and variables - Actions - Variables 设置 TOKEN
const TOKEN = process.env.TOKEN;
console.log(TOKEN);
while (true) {
// API 文档及 Token 申请地址:https://pandascore.co
let body = (await request.get(`https://api.pandascore.co/lol/matches?filter[league_id]=293&sort=&per_page=100&page=${page}`).set("Authorization", `Bearer ${TOKEN}`).set("Content-Type", "application/json")).body;
// 仅处理当年的
if (yearStr.length == 0) {
yearStr = body[0].begin_at.split("-")[0]
}
let filtered = body.filter(function(item) {
return item.serie.full_name.endsWith(yearStr);
});
games.push(...filtered);
let lastOne = body[body.length - 1];
let serieName = lastOne.serie.full_name;
if (!serieName.endsWith(yearStr)) {
break;
} else {
page++;
}
}
games.reverse();

// 移除之前的文件
if (fs.existsSync(`./${yearStr}_lck`)) fs.rmdirSync(`./${yearStr}_lck`, { recursive: true });
if (!fs.existsSync(`./${yearStr}_lck`)) fs.mkdirSync(`./${yearStr}_lck`);
if (!fs.existsSync(`./${yearStr}_lck/team`)) fs.mkdirSync(`./${yearStr}_lck/team`);

// 处理并生成没有提醒的全文件
let gameObjsWithNoAlarms = games.map(function(game) {
return lckGamesToICSObjs(game, false, yearStr);
});
let noAlarmFileName = `./${yearStr}_lck/${yearStr}_lck.ics`;
generateLCKICSAndWrite(gameObjsWithNoAlarms, noAlarmFileName);

// 处理并生成有提醒的全文件
let gameObjsWithAlarms = games.map(function(game) {
return lckGamesToICSObjs(game, true, yearStr);
});
let alarmFileName = `./${yearStr}_lck/${yearStr}_lck-alarm.ics`;
generateLCKICSAndWrite(gameObjsWithAlarms, alarmFileName);

// 生成各队伍有提醒和无提醒的文件
let teams = games.map(function(item) {
if (item.opponents.length <= 0) {
return []
}
return [item.opponents[0].opponent.acronym, item.opponents[1].opponent.acronym]
});
let teamNames = [...new Set(teams.flat())];
for (let teamName of teamNames) {
let teamGameObjsWithNoAlarms = gameObjsWithNoAlarms.filter(function(game) {
return game.title.split(" ").includes(teamName);
});
let noAlarmFileName = `./${yearStr}_lck/team/${teamName}.ics`
generateLCKICSAndWrite(teamGameObjsWithNoAlarms, noAlarmFileName)
let teamGameObjsWithAlarms = gameObjsWithAlarms.filter(function(game) {
return game.title.split(" ").includes(teamName);
});
let alarmFileName = `./${yearStr}_lck/team/${teamName}-alarm.ics`
generateLCKICSAndWrite(teamGameObjsWithAlarms, alarmFileName);
}
}

function generateLCKICSAndWrite(games, fileName) {
const result = icsTool.createEvents(games);
if (result.error) {
console.error(result.error);
} else {
fs.writeFileSync(fileName, result.value);
}
}

function lckGamesToICSObjs(game, hasAlarm, yearStr) {
const gameDate = new Date(new Date(game.scheduled_at).getTime() - 8 * 60 * 60 * 1000);
const gameEndDate = new Date(gameDate.getTime() + 2 * 60 * 60 * 1000);
let slug = game.slug;
let gameName = game.name;
if (game.status != "not_started") {
scoreA = game.results[0].score
scoreB = game.results[1].score
gameName += ` - ${scoreA} : ${scoreB}`
}
return {
title: gameName,
description: slug,
start: [gameDate.getFullYear(), gameDate.getMonth() + 1, gameDate.getDate(), gameDate.getHours(), gameDate.getMinutes()],
end: [gameEndDate.getFullYear(), gameEndDate.getMonth() + 1, gameEndDate.getDate(), gameEndDate.getHours(), gameEndDate.getMinutes()],
url: "https://www.twitch.tv/lck",
status: "TENTATIVE",
calName: `${yearStr}_lck`,
startInputType: "utc",
startOutputType: "utc",
endInputType: "utc",
endOutputType: "utc",
// 提醒应该不用区分是否比赛结束
alarms: hasAlarm ? [{ action: "audio", trigger: { minutes: 30, before: true, repeat: 1, attachType: "VALUE=URI", attach: "Glass" } }] : null,
}
}

async function main() {
const buffer = (await request.get(API_URL)).body;
const gameBundle = JSON.parse(buffer);
Expand All @@ -143,6 +249,7 @@ async function main() {
generateICS(gameBundle, false, game);
generateICS(gameBundle, true, game);
});
dealLCKGames();
}

main();