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

3
.gitignore vendored
View file

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

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

View file

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