Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

108 linhas
3.2 KiB

  1. #!/usr/bin/node
  2. const fs = require('fs');
  3. const request = require('request');
  4. const jsdom = require("jsdom");
  5. const { JSDOM } = jsdom;
  6. var BASEURL = process.argv[2];
  7. if(!BASEURL) {
  8. console.log(`### ERROR! No base URL provided!`);
  9. console.log(`Usage: node scrape.js {URL to scrape REQUIRED}`);
  10. process.exit(1);
  11. }
  12. OUTPUT = BASEURL.replace(/http:\/\/|https:\/\//g, "");
  13. OUTPUT = OUTPUT.replace(/\//g, '_');
  14. OUTPUT = OUTPUT.trim();
  15. if(OUTPUT.endsWith('_')) OUTPUT = OUTPUT.substring(0, OUTPUT.length - 1)
  16. try {
  17. fs.mkdirSync(OUTPUT, {recursive: true})
  18. } catch(e) {}
  19. console.log(`### Scraping icons from ${BASEURL} ...`);
  20. // Fetch page HTML from server
  21. request.get(BASEURL, (err, resp, body) => {
  22. if(err) {
  23. console.log(`### ERROR! Loading page - ${err}`);
  24. return;
  25. }
  26. // Load and parse into a virtual DOM document
  27. var doc = new JSDOM(body).window.document;
  28. let count = 0;
  29. //var viewBox = null;
  30. // Process inline SVGs
  31. var svgs = doc.getElementsByTagName('svg');
  32. for(let svg of svgs) {
  33. var viewBox = null;
  34. var name = `svg-${++count}`;
  35. if(svg.getAttribute('data-slug-id')) {
  36. name = svg.getAttribute('data-slug-id')
  37. }
  38. if(svg.getAttribute('viewBox')) {
  39. viewBox = svg.getAttribute('viewBox');
  40. }
  41. // If the SVG has <use> we need to handle that
  42. uses = svg.getElementsByTagName('use')
  43. for(let use of uses) {
  44. let href = use.getAttribute('xlink:href') || use.getAttribute('href');
  45. // Use is an id of another SVG element in the page
  46. if(href.startsWith('#')) {
  47. name = href.substring(1);
  48. let symbol = doc.getElementById(href.substring(1));
  49. viewBox = symbol.getAttribute('viewBox');
  50. //console.log(viewbox);
  51. // Remove the <use> node and insert the contents of the referred symbol
  52. svg.removeChild(use);
  53. svg.innerHTML = svg.innerHTML + symbol.innerHTML;
  54. }
  55. // Use is pointing at external URL
  56. if(href.startsWith('http')) {
  57. // NOT HANDLED - NOT EVEN SURE ITS PART OF THE SVG SPEC
  58. }
  59. }
  60. // Skip SVGs which just contain the defs
  61. if(svg.firstElementChild && svg.firstElementChild.nodeName == "defs") {
  62. continue;
  63. }
  64. console.log(`### - ${name} (Inline SVG)`)
  65. let viewBoxAttr = viewBox ? `viewBox="${viewBox}"` : '';
  66. let svgContent = `<svg xmlns="http://www.w3.org/2000/svg" ${viewBoxAttr}>${svg.innerHTML}</svg>`;
  67. fs.writeFileSync(`${OUTPUT}/${name}.svg`, svgContent)
  68. }
  69. // Process images
  70. var images = doc.getElementsByTagName('img');
  71. for(let img of images) {
  72. let src = img.getAttribute('src');
  73. if (src) {
  74. let fileName = src.split('/');
  75. fileName = fileName[fileName.length - 1];
  76. if(fileName.endsWith('.jpeg') || fileName.endsWith('.jpg')) continue;
  77. console.log(`### - ${fileName}`);
  78. if (src.startsWith('//')) src = 'https:' + src;
  79. if (!src.startsWith('http')) src = BASEURL + "/" + src;
  80. // Fetch image file and write to disk
  81. request.get(src)
  82. .pipe(fs.createWriteStream(`${OUTPUT}/${fileName}`))
  83. .on('error', err => {
  84. console.log(`### ERROR! Unable to fetch: ${err}`);
  85. })
  86. }
  87. }
  88. })