import axios from "axios";
import _ from "lodash";
import htmlParser from "node-html-parser";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";

export const ibm = async () => {
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
  const { data } = await axios.get(
    "https://ondeck.console.cloud.ibm.com/docs/cloud-infrastructure?topic=cloud-infrastructure-ibm-cloud-ip-ranges"
  );

  const root = htmlParser.parse(data);
  const ips = _.flattenDeep(
    ["#section-front-end-network", "#section-load-balancer-ips"].map(
      (cssSelector) =>
        root
          .querySelectorAll(`${cssSelector} td:nth-child(3)`)
          .map((e) => e.text.split("\n").map((l) => l.trim()))
    )
  ).filter((e) => e);
  await fs.promises.writeFile(
    path.join(__dirname, "../ip_ranges/CLOUDFLARE.json"),
    JSON.stringify({
      NN: ips.map((prefix) => ({ prefix })),
    })
  );
};

// (async () => console.log(await ibm()))();
