mirror of
https://github.com/fabrice404/olympics-calendar.git
synced 2026-02-14 20:49:04 +00:00
filter by medal events only
This commit is contained in:
@ -40,85 +40,103 @@ export class ICSGenerator {
|
||||
nocKey || "all-nocs",
|
||||
);
|
||||
|
||||
for (const lang of this.calendar.languages) {
|
||||
const pathSportKey = sportKey ? sportKey : "all-sports";
|
||||
const pathNocKey = nocKey ? nocKey : "calendar";
|
||||
let types = ["all-events", "medal-events", "gold-medal-events"];
|
||||
if (nocKey) {
|
||||
types = ["all-events"];
|
||||
}
|
||||
|
||||
const filepath = `./output/${lang.code.toLowerCase()}/${pathSportKey.toLowerCase()}/${pathNocKey.toLowerCase()}.ics`;
|
||||
mkdirSync(filepath.split("/").slice(0, -1).join("/"), { recursive: true });
|
||||
for (const type of types) {
|
||||
for (const lang of this.calendar.languages) {
|
||||
const pathSportKey = sportKey || "all-sports";
|
||||
let pathCalendar = "calendar";
|
||||
if (type != "all-events") {
|
||||
pathCalendar = type;
|
||||
} else if (nocKey) {
|
||||
pathCalendar = nocKey;
|
||||
}
|
||||
|
||||
const titleComponents: string[] = [];
|
||||
if (nocKey) {
|
||||
titleComponents.push(
|
||||
`${this.calendar.nocs.find((n) => n.key === nocKey)!.name[lang.code]}`,
|
||||
const filepath = `./output/${lang.code.toLowerCase()}/${pathSportKey.toLowerCase()}/${pathCalendar.toLowerCase()}.ics`;
|
||||
mkdirSync(filepath.split("/").slice(0, -1).join("/"), { recursive: true });
|
||||
|
||||
const titleComponents: string[] = [];
|
||||
if (nocKey) {
|
||||
titleComponents.push(
|
||||
`${this.calendar.nocs.find((n) => n.key === nocKey)!.name[lang.code]}`,
|
||||
);
|
||||
}
|
||||
if (sportKey) {
|
||||
titleComponents.push(this.calendar.sports.find((s) => s.key === sportKey)!.name[lang.code] || "");
|
||||
}
|
||||
titleComponents.push("Milano Cortina 2026");
|
||||
|
||||
const title = titleComponents.join(" - ");
|
||||
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push("BEGIN:VCALENDAR");
|
||||
lines.push("VERSION:2.0");
|
||||
lines.push(
|
||||
`PRODID:-//fabrice404//olympics-calendar//${lang.code}/${pathSportKey}/${pathCalendar}`,
|
||||
);
|
||||
}
|
||||
if (sportKey) {
|
||||
titleComponents.push(this.calendar.sports.find((s) => s.key === sportKey)!.name[lang.code] || "");
|
||||
}
|
||||
titleComponents.push("Milano Cortina 2026");
|
||||
lines.push(`X-WR-CALNAME:${title}`);
|
||||
lines.push(`NAME:${title}`);
|
||||
|
||||
const title = titleComponents.join(" - ");
|
||||
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push("BEGIN:VCALENDAR");
|
||||
lines.push("VERSION:2.0");
|
||||
lines.push(
|
||||
`PRODID:-//fabrice404//olympics-calendar//${lang.code}/${pathSportKey}/${pathNocKey}`,
|
||||
);
|
||||
lines.push(`X-WR-CALNAME:${title}`);
|
||||
lines.push(`NAME:${title}`);
|
||||
|
||||
this.calendar.events
|
||||
.filter((event) => {
|
||||
if ((sportKey && event.sport !== sportKey) || event.sport === "CER") {
|
||||
return false;
|
||||
}
|
||||
if (nocKey && !event.nocs.includes(nocKey)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.forEach((event) => {
|
||||
lines.push("BEGIN:VEVENT");
|
||||
lines.push(`UID:${event.key.replace(/--/g, "-")}`);
|
||||
lines.push(`DTSTAMP:${event.start.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}`);
|
||||
lines.push(`DTSTART:${event.start.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}`);
|
||||
lines.push(`DTEND:${event.end.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}`);
|
||||
lines.push(`LOCATION:${event.location[lang.code] || ""}`);
|
||||
|
||||
const sport = this.calendar.sports.find(
|
||||
(s) => s.key === event.sport,
|
||||
)!;
|
||||
let description = `DESCRIPTION:${sport.name[lang.code]} - ${event.name[lang.code] || ""}`;
|
||||
let summary = `SUMMARY:${event.name[lang.code] || ""}`;
|
||||
|
||||
if (event.competitors?.length === 2) {
|
||||
const competitor1 = this.getCompetitor(event.competitors[0]!, lang.code);
|
||||
const competitor2 = this.getCompetitor(event.competitors[1]!, lang.code);
|
||||
|
||||
if (competitor1 && competitor2) {
|
||||
summary = `SUMMARY:${competitor1?.flag} ${competitor1.name} - ${competitor2?.name} ${competitor2.flag}`;
|
||||
this.calendar.events
|
||||
.filter((event) => {
|
||||
if ((sportKey && event.sport !== sportKey) || event.sport === "CER") {
|
||||
return false;
|
||||
}
|
||||
} else if (event.competitors?.length > 0) {
|
||||
const competitors = event.competitors
|
||||
.map((competitorId) => this.getCompetitor(competitorId, lang.code))
|
||||
.map((competitor) => `\\n${competitor.flag} ${competitor.name}`).join("");
|
||||
description += `${competitors}`;
|
||||
}
|
||||
if (nocKey && !event.nocs.includes(nocKey)) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
(type === "medal-events" && event.medal === "0") ||
|
||||
(type === "gold-medal-events" && event.medal !== "1")
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.forEach((event) => {
|
||||
lines.push("BEGIN:VEVENT");
|
||||
lines.push(`UID:${event.key.replace(/--/g, "-")}`);
|
||||
lines.push(`DTSTAMP:${event.start.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}`);
|
||||
lines.push(`DTSTART:${event.start.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}`);
|
||||
lines.push(`DTEND:${event.end.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}`);
|
||||
lines.push(`LOCATION:${event.location[lang.code] || ""}`);
|
||||
|
||||
lines.push(summary);
|
||||
lines.push(this.cleanLine(description));
|
||||
lines.push("END:VEVENT");
|
||||
});
|
||||
const sport = this.calendar.sports.find(
|
||||
(s) => s.key === event.sport,
|
||||
)!;
|
||||
let description = `DESCRIPTION:${sport.name[lang.code]} - ${event.name[lang.code] || ""}`;
|
||||
let summary = `SUMMARY:${event.name[lang.code] || ""}`;
|
||||
|
||||
lines.push("END:VCALENDAR");
|
||||
if (event.competitors?.length === 2) {
|
||||
const competitor1 = this.getCompetitor(event.competitors[0]!, lang.code);
|
||||
const competitor2 = this.getCompetitor(event.competitors[1]!, lang.code);
|
||||
|
||||
if (lines.length <= 10) {
|
||||
this.debug("Skipping empty ICS file:", filepath);
|
||||
} else {
|
||||
writeFileSync(filepath, lines.join("\n"), "utf-8");
|
||||
if (competitor1 && competitor2) {
|
||||
summary = `SUMMARY:${competitor1?.flag} ${competitor1.name} - ${competitor2?.name} ${competitor2.flag}`;
|
||||
}
|
||||
} else if (event.competitors?.length > 0) {
|
||||
const competitors = event.competitors
|
||||
.map((competitorId) => this.getCompetitor(competitorId, lang.code))
|
||||
.map((competitor) => `\\n${competitor.flag} ${competitor.name}`).join("");
|
||||
description += `${competitors}`;
|
||||
}
|
||||
|
||||
lines.push(summary);
|
||||
lines.push(this.cleanLine(description));
|
||||
lines.push("END:VEVENT");
|
||||
});
|
||||
|
||||
lines.push("END:VCALENDAR");
|
||||
|
||||
if (lines.length <= 10) {
|
||||
this.debug("Skipping empty ICS file:", filepath);
|
||||
} else {
|
||||
writeFileSync(filepath, lines.join("\n"), "utf-8");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -142,6 +160,7 @@ export class ICSGenerator {
|
||||
|
||||
public generate(): void {
|
||||
this.debug("generate");
|
||||
|
||||
this.generateICSFile(null, null);
|
||||
|
||||
this.calendar.sports.forEach((sport) => {
|
||||
|
||||
Reference in New Issue
Block a user