add moment + move the logic of GameResource Data inside

This commit is contained in:
Kfir Dayan 2023-03-22 21:25:20 +02:00
parent f42ce1e319
commit 654d9d0655
4 changed files with 70 additions and 53 deletions

11
package-lock.json generated
View file

@ -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",

View file

@ -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"

View file

@ -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;
}
}
}

View file

@ -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();
@ -88,3 +43,4 @@ const start = async () => {
await app.getNewGamesAndUpdateCalendar();
}
start();
// console.log(moment("Sel, 4/4").format());