From 654d9d06554bb12b5f26ed614f56ae92fec1aaec Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Wed, 22 Mar 2023 21:25:20 +0200 Subject: [PATCH] add moment + move the logic of GameResource Data inside --- package-lock.json | 11 +++++++++- package.json | 5 +++-- src/GameSource.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++- src/index.ts | 54 +++++------------------------------------------ 4 files changed, 70 insertions(+), 53 deletions(-) diff --git a/package-lock.json b/package-lock.json index 599cf7b..eaa9623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,8 @@ "dependencies": { "axios": "^1.3.4", "dotenv": "^16.0.3", - "googleapis": "^113.0.0" + "googleapis": "^113.0.0", + "moment": "^2.29.4" }, "devDependencies": { "@types/node": "^18.15.5" @@ -410,6 +411,14 @@ "node": ">= 0.6" } }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index 56fce7b..0148c8c 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,11 @@ "dependencies": { "axios": "^1.3.4", "dotenv": "^16.0.3", - "googleapis": "^113.0.0" + "googleapis": "^113.0.0", + "moment": "^2.29.4" }, "scripts": { - "dev": "nodemon --watch insted --ignore tmp/ public/index.js" + "dev": "nodemon public/index.js" }, "devDependencies": { "@types/node": "^18.15.5" diff --git a/src/GameSource.ts b/src/GameSource.ts index fae007c..c80d246 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -1,5 +1,6 @@ require('dotenv').config(); import axios from "axios"; +import { GoogleCalendarEvent } from "./types"; // This calss will be the game source. // search for upcomming games @@ -10,6 +11,56 @@ export default class GameSource { console.log("GET GAMES") const sourceUrl = `https://serpapi.com/search.json?q=maccabi+haifa+next+games&api_key=${process.env.SERPAPI_KEY}&location=austin,+texas,+united+states`; const result = await axios.get(sourceUrl) - return result; + + const games = this.orderGames(result) + return games; } + + orderGames(result: any) { + const rootGames = result.data.sports_results; + const upcomingEvents: GoogleCalendarEvent[] = []; + upcomingEvents.push({ + summary: rootGames.title, + location: rootGames.game_spotlight.stadium, + description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getOpponentIndexByStadium(rootGames.game_spotlight.stadium)].name, + start: { + dateTime: `2023-03-22T09:00:00`, + timeZone: 'Asia/Jerusalem' + }, + end: { + dateTime: `2023-03-22T11:00:00`, + timeZone: 'Asia/Jerusalem' + } + }); + + // console.log(rootGames.game_spotlight) + // console.log("DONE WITH UPDATING NEW EVENTS") + + + rootGames.games.forEach((game: any) => { + upcomingEvents.push({ + summary: 'Maccabi Haifa F.C.', + location: game.stadium, + description: "Haifa vs. " + game.teams[this.getOpponentIndexByStadium(game.stadium)].name, + start: { + dateTime: `${game.date} ${game.time}`, + timeZone: 'Asia/Jerusalem' + }, + end: { + dateTime: `${game.date} ${game.time}`, + timeZone: 'Asia/Jerusalem' + } + }); + }); + // console.log("UPCOMING EVENTS", upcomingEvents) + return upcomingEvents; + } + + getOpponentIndexByStadium(stadium: string) { + if (stadium === "Sammy Ofer Stadium") { + return 1; + } else { + return 0; + } + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index b121b69..110b525 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import GoogleCalendar from './GoogleCalendar'; import GameSource from './GameSource'; import { GoogleCalendarEvent } from './types/index'; +import moment from 'moment'; // require // crete App class @@ -28,57 +29,11 @@ class App { async getNewGamesAndUpdateCalendar() { console.log("GET NEW GAMES AND UPDATE CALENDAR") - const upcomingEvents: GoogleCalendarEvent[] = []; + this.gameSource.getGames().then((games: any) => { - const rootGames = games.data.sports_results; - console.log("ROOT GAMES", rootGames.games) - upcomingEvents.push({ - summary: rootGames.title, - location: rootGames.game_spotlight.stadium, - description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getOpponentIndexByStadium(rootGames.game_spotlight.stadium)].name, - start: { - dateTime: `2023-03-22T09:00:00`, - timeZone: 'Asia/Jerusalem' - }, - end: { - dateTime: `2023-03-22T11:00:00`, - timeZone: 'Asia/Jerusalem' - } - }); - - console.log(rootGames.game_spotlight) - // console.log("DONE WITH UPDATING NEW EVENTS") - - - // rootGames.games.forEach((game: any) => { - // console.log("GAME", game) - // upcomingEvents.push({ - // summary: 'Maccabi Haifa F.C.', - // location: game.stadium, - // description: "Haifa vs. " + game.teams[this.getOpponentIndexByStadium(game.stadium)].name, - // start: { - // dateTime: `${game.date} ${game.time}`, - // timeZone: 'Asia/Jerusalem' - // }, - // end: { - // dateTime: `${game.date} ${game.time}`, - // timeZone: 'Asia/Jerusalem' - // } - // }); - // }); - // console.log("UPCOMING EVENTS", upcomingEvents) - - // this.googleCalendar.updateNewEvent(upcomingEvents); + console.log(games); }); } - - getOpponentIndexByStadium(stadium: string) { - if (stadium === "Sammy Ofer Stadium") { - return 1; - } else { - return 0; - } - } } const app = new App(); @@ -87,4 +42,5 @@ const start = async () => { await app.init(); await app.getNewGamesAndUpdateCalendar(); } -start(); +start(); +// console.log(moment("Sel, 4/4").format());