From 2daf19e30a87f6271aad1b7bc79c0a35847f5049 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Thu, 23 Mar 2023 20:39:29 +0200 Subject: [PATCH] html parser had add + parsing new feed source by it --- src/GameSource.ts | 112 ++++++++++++++++++++++++++++-------------- src/GoogleCalendar.ts | 1 + src/index.ts | 9 ++-- 3 files changed, 78 insertions(+), 44 deletions(-) diff --git a/src/GameSource.ts b/src/GameSource.ts index 0efb33a..f44f1c4 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -1,11 +1,14 @@ require('dotenv').config(); import axios from "axios"; import { GoogleCalendarEvent } from "./types"; +import { parse } from 'node-html-parser'; +import moment from 'moment'; // require + // This calss will be the game source. // search for upcomming games export default class GameSource { - constructor() {} + constructor() { } async getGamesFromSerapi() { console.log("GET GAMES") @@ -17,57 +20,90 @@ export default class GameSource { } async getGamesFromHaifa() { - console.log("GET GAMES") const sourceUrl = `https://mhaifafc.com/games?lang=en`; - const result = await axios.get(sourceUrl) - console.log(result); + const result = await axios.get(sourceUrl); + const parsedResult = parse(result.data.toString()) + const gameBoxs = parsedResult.querySelectorAll(".game-box"); + + const games: GoogleCalendarEvent[] = []; + + for (let gameBox of gameBoxs) { + + const teamsPlaying = gameBox.querySelectorAll(".team-name").map((team: any) => team.text); + const regex = /[\r\n\s]+/g; + const gameHeader = gameBox.querySelector(".game-header").text.replace(regex, " ").trim(); + const headerSplit = gameHeader.split(","); + + + if (headerSplit.length < 4) break; + 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"); + + games.push({ + summary: 'Maccabi Haifa F.C.', + location: "Sammy Ofer Stadium", + description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`, + start: { + dateTime: start, + timeZone: 'Asia/Jerusalem' + }, + end: { + dateTime: end, + timeZone: 'Asia/Jerusalem' + } + }) + } + 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: rootGames.title, - location: rootGames.game_spotlight.stadium, - description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getOpponentIndexByStadium(rootGames.game_spotlight.stadium)].name, + summary: 'Maccabi Haifa F.C.', + location: game.stadium, + description: "Haifa vs. " + game.teams[this.getOpponentIndexByStadium(game.stadium)].name, start: { - dateTime: `2023-03-22T09:00:00`, + dateTime: `${game.date} ${game.time}`, timeZone: 'Asia/Jerusalem' }, end: { - dateTime: `2023-03-22T11:00:00`, + dateTime: `${game.date} ${game.time}`, timeZone: 'Asia/Jerusalem' } }); + }); + // console.log("UPCOMING EVENTS", upcomingEvents) + return upcomingEvents; + } - // 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; - } + getOpponentIndexByStadium(stadium: string) { + if (stadium === "Sammy Ofer Stadium") { + return 1; + } else { + return 0; } + } } \ No newline at end of file diff --git a/src/GoogleCalendar.ts b/src/GoogleCalendar.ts index bb8833f..fad9642 100644 --- a/src/GoogleCalendar.ts +++ b/src/GoogleCalendar.ts @@ -58,6 +58,7 @@ export default class GoogleCalendar { } console.log(event.description + ' created'); }); + }) } diff --git a/src/index.ts b/src/index.ts index ba4f285..b96e7c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,12 +30,9 @@ class App { async getNewGamesAndUpdateCalendar() { console.log("GET NEW GAMES AND UPDATE CALENDAR") - this.gameSource.getGamesFromHaifa().then((games: any) => { - console.log(games); - }); - // this.gameSource.getGamesFromSerapi().then((games: any) => { - // console.log(games); - // }); + + const games = await this.gameSource.getGamesFromHaifa(); + this.googleCalendar.updateNewEvent(games); } }