Skip to content

Instantly share code, notes, and snippets.

@1devNdogs
Last active March 2, 2021 21:47
Show Gist options
  • Select an option

  • Save 1devNdogs/2dfdcc35f6e357419cdf42b39d0ed2bd to your computer and use it in GitHub Desktop.

Select an option

Save 1devNdogs/2dfdcc35f6e357419cdf42b39d0ed2bd to your computer and use it in GitHub Desktop.

Revisions

  1. 1devNdogs renamed this gist Mar 2, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. 1devNdogs created this gist Mar 2, 2021.
    79 changes: 79 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    const rp = require("request-promise");
    const $ = require("cheerio");
    const monthArray = [
    "#mes_enero",
    "#mes_febrero",
    "#mes_marzo",
    "#mes_abril",
    "#mes_mayo",
    "#mes_junio",
    "#mes_julio",
    "#mes_agosto",
    "#mes_septiembre",
    "#mes_octubre",
    "#mes_noviembre",
    "#mes_diciembre",
    ];
    checkIt();

    async function checkIt() {
    const thisMonth = monthArray[new Date().getMonth()];
    const thisYear = new Date().getFullYear();
    return rp({
    uri: "https://www.sii.cl/valores_y_fechas/uf/uf" + thisYear + ".htm",
    })
    .then(function (html) {
    let monthHtml = $(thisMonth, html).find("tbody tr");
    let rowDay1 = 1;
    let rowDay2 = 11;
    let rowDay3 = 21;
    let finalArray = [];

    Array.from(monthHtml).forEach((item, i) => {
    if (i > 0) {
    let one = $("td:nth-child(2)", item)
    .text()
    .replace(".", "")
    .replace(",", ".");
    let two = $("td:nth-child(4)", item)
    .text()
    .replace(".", "")
    .replace(",", ".");
    let three = $("td:nth-child(6)", item)
    .text()
    .replace(".", "")
    .replace(",", ".");

    if (one.length > 0) {
    finalArray.push({ day: rowDay1, uf: parseFloat(one) });
    }
    if (two.length > 0) {
    finalArray.push({ day: rowDay2, uf: parseFloat(two) });
    }
    if (three.length > 0) {
    finalArray.push({ day: rowDay3, uf: parseFloat(three) });
    }

    rowDay1++;
    rowDay2++;
    rowDay3++;
    }
    });
    finalArray.sort(compare);
    console.log(finalArray);
    })
    .catch(function (err) {
    console.log(err);
    return { err };
    });
    }

    function compare(a, b) {
    if (a.day < b.day) {
    return -1;
    }
    if (a.day > b.day) {
    return 1;
    }
    return 0;
    }