html parser had add + parsing new feed source by it

This commit is contained in:
Kfir Dayan 2023-03-23 20:39:29 +02:00
parent f74b2294d0
commit 2daf19e30a
3 changed files with 78 additions and 44 deletions

View file

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

View file

@ -58,6 +58,7 @@ export default class GoogleCalendar {
}
console.log(event.description + ' created');
});
})
}

View file

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