text: const cheerio = require("cheerio");
  const fetch = require("node-fetch");
  const year = 2022;
  
  module.exports = (app) => {
    app.get("/api/getTable", async function (req, res) {
      let table = [];
  
      const url =
        "https://www.royalbavarianliga.de/tabelle.php?ligaid=o2905&saison=" +
        year;
      const response = await fetch(url);
      const body = await response.text();
      const $ = cheerio.load(body);
  
      let list = [];
      $("table.tabelle")
        .find("tbody > tr")
        .each(function (index, element) {
          let temp = {};
          temp.team = $(element).find("td.team").text();
          temp.logo = $(element).find("td.logo > img").attr("src");
          temp.gamesPlayed = $(element).find("td.s").text();
          temp.wins = $(element).find("td.g").text();
          temp.draws = $(element).find("td.u").text();
          temp.losses = $(element).find("td.n").text();
          temp.diff = $(element).find("td.tdiff").text();
          temp.goals = $(element).find("td.t").text();
          temp.goalsAgainst = $(element).find("td.gt").text();
          temp.points = $(element).find("td.p3").text();
          list.push(temp);
        });
  
      res.json({ data: list });
    });
  
    app.get("/api/getGames", async function (req, res) {
      let games = {};
  
      const url =
        "https://www.royalbavarianliga.de/teaminfo.php?saison=" +
        year +
        "&teamid=o594";
      const response = await fetch(url);
      const body = await response.text();
      const $ = cheerio.load(body);
  
      $("table.spiele").each(function (index, element) {
        let comp = "";
        $(element)
          .find("thead > tr > th.hteam")
          .each(function (index, el) {
            comp = $(el).text();
            games[comp] = [];
          });
        $(element)
          .find("tbody > tr")
          .each(function (index, el) {
            let temp = {};
            temp.href = $(el).find("td.info > a").attr("href");
            temp.date = $(el).find("td.datum").text();
            temp.team1 = $(el).find("td.hteam > span.teamname > a").text();
            temp.team2 = $(el).find("td.ateam > span.teamname > a").text();
            temp.goals1 = $(el).find("td.th").text();
            temp.goals2 = $(el).find("td.ta").text();
            temp.info = $(el).find("td.info > a").attr("href");
            temp.location = $(el).find("td.ort > div").attr("title");
            temp.status = $(el).find("td.status").text();
            temp.competition = comp;
  
            games[comp].push(temp);
          });
      });
  
      res.json({ data: games });
    });
  
    app.get("/api/getScorers", async function (req, res) {
      const url = "https://www.royalbavarianliga.de" + req.query.href;
  
      const response = await fetch(url);
      const body = await response.text();
      const $ = cheerio.load(body);
  
      const goalsHome = [];
      const goalsAway = [];
  
      $("table > tbody.verlauf > tr").each(function (index, element) {
        if ($(element).find("td.c > div").hasClass("tor")) {
          const name = $(element).find("td.heim > span.name").text();
          if (name) {
            goalsHome.push([$(element).find("td.c > div").text(), name]);
          }
        }
      });
  
      $("table > tbody.verlauf > tr").each(function (index, element) {
        if ($(element).find("td.c > div").hasClass("tor")) {
          const name = $(element).find("td.ausw > span.name").text();
          if (name) {
            goalsAway.push([$(element).find("td.c > div").text(), name]);
          }
        }
      });
  
      const goals = { home: goalsHome, away: goalsAway };
      res.json({ data: goals });
    });
  };
  