scrape.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const events = require("./index");
  2. const axios = require("axios");
  3. const cheerio = require("cheerio");
  4. const ora = require("ora");
  5. const fs = require("fs");
  6. const BASE_URL = "https://developer.mozilla.org/en-US/docs/Web/Events";
  7. function parseText(val) {
  8. if (val.match(/Yes/)) {
  9. return true;
  10. } else if (val.match(/No/)) {
  11. return false;
  12. }
  13. }
  14. function getValue($, name) {
  15. const text = $("dt, td")
  16. .filter(function() {
  17. return $(this).text() === name;
  18. })
  19. .next()
  20. .text();
  21. return parseText(text);
  22. }
  23. function writeToFile(val) {
  24. fs.writeFileSync(
  25. require.resolve("./dom-event-types.json"),
  26. JSON.stringify(val, null, 2)
  27. );
  28. }
  29. const obj = { ...events };
  30. async function getInfo(event) {
  31. let res;
  32. try {
  33. res = await axios.get(`${BASE_URL}/${event}`);
  34. } catch (err) {
  35. return;
  36. }
  37. const $ = cheerio.load(res.data);
  38. const bubblesVal = getValue($, "Bubbles");
  39. const cancelableVal = getValue($, "Cancelable");
  40. if (bubblesVal === undefined) {
  41. delete obj[event].bubbles;
  42. }
  43. if (cancelableVal === undefined) {
  44. delete obj[event].cancelable;
  45. }
  46. }
  47. (async () => {
  48. const spinner = ora("Scraping MDN").start();
  49. for (const event of Object.keys(events)) {
  50. await getInfo(event);
  51. }
  52. spinner.stop();
  53. console.log("Scraping complete");
  54. writeToFile(obj);
  55. })();