Only output large thumbnail if it's available

This commit is contained in:
Aditya Thakral 2021-08-25 14:40:02 -04:00
parent 1d2e912269
commit 0a771f56d0
3 changed files with 1497 additions and 18 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
"dependencies": { "dependencies": {
"jsdom": "^16.6.0", "jsdom": "^16.6.0",
"libxmljs": "^0.19.7", "libxmljs": "^0.19.7",
"node-fetch": "^2.6.1",
"showdown": "^1.9.1" "showdown": "^1.9.1"
} }
} }

View File

@ -1,5 +1,6 @@
const fs = require("fs"); const fs = require("fs");
const jsdom = require("jsdom"); const jsdom = require("jsdom");
const fetch = require("node-fetch");
const { JSDOM } = jsdom; const { JSDOM } = jsdom;
const { window } = new JSDOM(""); const { window } = new JSDOM("");
global.window = window; global.window = window;
@ -24,17 +25,25 @@ const file = fs
const xml = libxmljs.parseHtml(file); const xml = libxmljs.parseHtml(file);
const talks = xml.find("//mediaitem"); const talks = xml.find("//mediaitem");
talks.forEach((talk, idx) => { (async () => {
const { filename, markdown } = xml2md(talk, idx); const all = await Promise.all(
talks.map(async (talk, idx) => {
const { filename, markdown } = await xml2md(talk, idx);
fs.promises.writeFile(`./markdown-talks/${filename}.md`, markdown); await fs.promises.writeFile(`./markdown-talks/${filename}.md`, markdown);
}); }),
);
console.log(all.length);
process.exit(0);
})();
/** /**
* *
* @param {libxmljs.Element} talk * @param {libxmljs.Element} talk
*/ */
function xml2md(talk, index) { async function xml2md(talk, index) {
const title = talk.attr("title").value(); const title = talk.attr("title").value();
const abstract = talk const abstract = talk
@ -51,7 +60,13 @@ function xml2md(talk, index) {
.map((s) => s.trim()); .map((s) => s.trim());
const thumbSmall = MIRROR_URL + talk.get("thumbnail").attr("file").value(); const thumbSmall = MIRROR_URL + talk.get("thumbnail").attr("file").value();
const thumbLarge = thumbSmall.replace("-thumb-small", "-thumb-large");
/** @type{string | undefined} */
let thumbLarge = thumbSmall.replace("-thumb-small", "-thumb-large");
if (!(await resourceExists(thumbLarge))) {
thumbLarge = undefined;
}
const links = talk const links = talk
.find("mediafile") .find("mediafile")
@ -83,8 +98,7 @@ title: '${title.replaceAll("'", "''")}'
presentors: presentors:
- '${presentors.join("\n - ").replaceAll("'", "''")}' - '${presentors.join("\n - ").replaceAll("'", "''")}'
thumbnails: thumbnails:
small: '${thumbSmall}' small: '${thumbSmall}'${thumbLarge ? `\n large: ${thumbLarge}` : ""}
large: '${thumbLarge}'
links: links:
${links.join("\n")} ${links.join("\n")}
--- ---
@ -105,3 +119,27 @@ ${converter.makeMarkdown(abstract ?? "")}
markdown, markdown,
}; };
} }
/**
*
* @param {string} url
*/
async function resourceExists(url) {
const time = Math.trunc(Math.random() * 10000);
await sleep(time);
// const timer = setTimeout(() => console.log("not done", time, url), time + 3000);
const response = await fetch(url);
clearTimeout(timer)
return (
response.status.toString().startsWith("2") ||
response.status.toString().startsWith("3")
);
}
function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}