Newer
Older
get-corhal / index.mjs
#!/usr/bin/env node
import fetch from "node-fetch";
import "cli-progress";
import cliProgress from "cli-progress";

/**
 * Display on stdout all notices, without suffix and prefix of an array
 * @param {object[]} notices
 * @param {boolean} last if these are the last notices
 */
const display = (notices, last = false) => {
    const noticesStr = notices
        .map(notice => JSON.stringify(notice))
        .join(",\n");
    console.log(noticesStr + (last ? "" : ","));
}

const bar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_grey);
let afterKeyToken;
let errorPages = 0;

console.log("[");

const response = await fetch("https://corhal-api.inist.fr/mergedDocuments?size=50");
let notices = /** @type {object[]} */(await response.json());
const totalCount = Number(response.headers.get("x-total-count"));
let resultCount = notices.length;
bar.start(totalCount, resultCount);
afterKeyToken = response.headers.get("after-key-token");

const max = Math.max(10_000_000, totalCount);
// const max = 300;
// const max = 10_000;
// const max = 50;

while (afterKeyToken && resultCount < max) {
    display(notices);
    const response = await fetch(`https://corhal-api.inist.fr/after/${afterKeyToken}`);
    if (response.status !== 200) {
        errorPages += 1;
        continue;
    }
    notices = /** @type {object[]} */(await response.json());
    resultCount += notices.length;
    bar.update(resultCount);
    afterKeyToken = response.headers.get("after-key-token");
}

bar.stop();

display(notices, true);

console.log("]");

if (errorPages) {
    console.error(`There has been ${errorPages} pages in error!`);
}