bug fixed passing the wrong attributes to create function of ics

This commit is contained in:
Kfir Dayan 2023-03-28 17:28:48 +03:00
parent 463573d5a6
commit 67babeac15
5 changed files with 77 additions and 22 deletions

5
.gitignore vendored
View file

@ -10,4 +10,7 @@ tmp
## ENV Vars ##
.env
config/client_google_auth.json
config/client_google_auth.json
## output ##
*.ics

View file

@ -8,9 +8,8 @@ import moment from 'moment'; // require
// search for upcomming games
export default class GameSource {
constructor() { }
async getGamesFromHaifa() {
async getGamesFromHaifa(): Promise<GoogleCalendarEvent[]> {
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
const result = await axios.get(sourceUrl);
const parsedResult = parse(result.data.toString())
@ -21,12 +20,9 @@ export default class GameSource {
for (let gameBox of gameBoxs) {
const teamsPlaying = gameBox.querySelectorAll(".team-name").map((team: any) => team.text);
console.log("TEAMS PLAYING", teamsPlaying)
const regex = /[\r\n\s]+/g;
const gameHeader = gameBox.querySelector(".game-header").text.replace(regex, " ").trim();
console.log("GAME HEADER", gameHeader)
const headerSplit = gameHeader.split(",");
console.log("HEADER SPLIT", headerSplit)
// In data, if there is no time, it means it's the last game for the calender
const lastGameForCalender = headerSplit.length < 4;
if (lastGameForCalender) break;
@ -34,10 +30,13 @@ export default class GameSource {
const gameDate = headerSplit[2].trim();
const gameTime = headerSplit[3].trim();
const start = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm").format("YYYY-MM-DDTHH:mm:ss");
const end = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm").add(2, "hours").format("YYYY-MM-DDTHH:mm:ss");
const startOriginal = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm").format("DD/MM/YYYY HH:mm");
const endOriginal = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm").add(2, "hours").format("DD/MM/YYYY HH:mm");
games.push({
const start = [moment(startOriginal, 'DD/MM/YYYYHH:mm').year(), moment(startOriginal, 'DD/MM/YYYYHH:mm').month() + 1, moment(startOriginal, 'DD/MM/YYYYHH:mm').date(), moment(startOriginal, 'DD/MM/YYYYHH:mm').hour(), moment(startOriginal, 'DD/MM/YYYYHH:mm').minute()];
const end = [moment(endOriginal, 'DD/MM/YYYYHH:mm').year(), moment(endOriginal, 'DD/MM/YYYYHH:mm').month() + 1, moment(endOriginal, 'DD/MM/YYYYHH:mm').date(), moment(endOriginal, 'DD/MM/YYYYHH:mm').hour(), moment(endOriginal, 'DD/MM/YYYYHH:mm').minute()]
games.push({
summary: 'Maccabi Haifa F.C.',
location: "Sammy Ofer Stadium",
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,

47
src/Ics.ts Normal file
View file

@ -0,0 +1,47 @@
import { GoogleCalendarEvent } from "./types";
import * as ics from 'ics';
const uuid = require('uuid').v4;
export default class Ics {
generateIcsOutputFromGames = (games: GoogleCalendarEvent[]) => {
// let icsOutput = '';
// for (let game of games) {
// icsOutput += this.generateIcsOutputFromGame(game);
// }
// console.log(icsOutput)
// return icsOutput;
return this.generateIcsOutputFromGame(games[0])
}
generateIcsOutputFromGame = (game: GoogleCalendarEvent) => {
const { summary, location, description, start, end } = game;
const icsEvent: ics.EventAttributes = {
title: summary,
description,
location,
start: [start.dateTime[0], start.dateTime[1], start.dateTime[2], start.dateTime[3], start.dateTime[4]],
end: [end.dateTime[0], end.dateTime[1], end.dateTime[2], end.dateTime[3], end.dateTime[4]],
url: 'https://mhaifafc.com/',
status: 'CONFIRMED',
busyStatus: 'BUSY',
productId: 'https://mhaifafc.com/',
recurrenceRule: '',
attendees: [],
alarms: [],
categories: [],
organizer: { name: 'Maccabi Haifa F.C.', email: '' },
uid: uuid
};
const { error, value } = ics.createEvent(icsEvent);
if (error) {
console.log(error);
return '';
}
return value;
}
}

View file

@ -1,14 +1,22 @@
import GoogleCalendar from './GoogleCalendar';
import GameSource from './GameSource';
// import ics from 'ics';
import fs from 'fs';
import { GoogleCalendarEvent } from './types';
import Ics from './Ics';
class App {
googleCalendar: GoogleCalendar;
gameSource: any;
googleToken: any;
gameSource: GameSource;
ics: Ics;
googleToken: string;
constructor() {
this.googleCalendar = new GoogleCalendar();
this.gameSource = new GameSource();
this.ics = new Ics();
}
async init() {
@ -18,8 +26,6 @@ class App {
async getNewGamesAndUpdateCalendar() {
console.log("GET NEW GAMES AND UPDATE CALENDAR")
const games = await this.gameSource.getGamesFromHaifa();
this.googleCalendar.updateNewEvent(games);
}
@ -27,14 +33,14 @@ class App {
const app = new App();
// const start = async () => {
// await app.init();
// await app.getNewGamesAndUpdateCalendar();
// }
const start = async () => {
const games = await app.gameSource.getGamesFromHaifa();
const icsEvents = app.ics.generateIcsOutputFromGames(games);
console.log(icsEvents)
}
start();

View file

@ -3,11 +3,11 @@ export interface GoogleCalendarEvent {
location: string;
description: string;
start: {
dateTime: string;
dateTime: number[];
timeZone: string;
};
end: {
dateTime: string;
dateTime: number[];
timeZone: string;
};
}