diff --git a/.gitignore b/.gitignore index 582c801..312d486 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ tmp ## ENV Vars ## .env -config/client_google_auth.json \ No newline at end of file +config/client_google_auth.json + +## output ## +*.ics \ No newline at end of file diff --git a/src/GameSource.ts b/src/GameSource.ts index dcc88a0..553d1de 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -8,9 +8,8 @@ import moment from 'moment'; // require // search for upcomming games export default class GameSource { - constructor() { } - async getGamesFromHaifa() { + async getGamesFromHaifa(): Promise { 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]}`, diff --git a/src/Ics.ts b/src/Ics.ts new file mode 100644 index 0000000..21c9c1a --- /dev/null +++ b/src/Ics.ts @@ -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; + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 46f5e09..0568daa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); \ No newline at end of file diff --git a/src/types/index.ts b/src/types/index.ts index 6111f8e..72fc622 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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; }; } \ No newline at end of file