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(); require('dotenv').config();
import axios from "axios"; import axios from "axios";
import { GoogleCalendarEvent } from "./types"; import { GoogleCalendarEvent } from "./types";
import { parse } from 'node-html-parser';
import moment from 'moment'; // require
// This calss will be the game source. // This calss will be the game source.
// search for upcomming games // search for upcomming games
export default class GameSource { export default class GameSource {
constructor() {} constructor() { }
async getGamesFromSerapi() { async getGamesFromSerapi() {
console.log("GET GAMES") console.log("GET GAMES")
@ -17,10 +20,43 @@ export default class GameSource {
} }
async getGamesFromHaifa() { async getGamesFromHaifa() {
console.log("GET GAMES")
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);
console.log(result); 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) { orderGames(result: any) {

View file

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

View file

@ -30,12 +30,9 @@ class App {
async getNewGamesAndUpdateCalendar() { async getNewGamesAndUpdateCalendar() {
console.log("GET NEW GAMES AND UPDATE CALENDAR") console.log("GET NEW GAMES AND UPDATE CALENDAR")
this.gameSource.getGamesFromHaifa().then((games: any) => {
console.log(games); const games = await this.gameSource.getGamesFromHaifa();
}); this.googleCalendar.updateNewEvent(games);
// this.gameSource.getGamesFromSerapi().then((games: any) => {
// console.log(games);
// });
} }
} }