onezeroz

Untitled

Apr 16th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. const request = require("request");
  2. const fs = require("fs");
  3. const path = require("path");
  4. const cheerio = require("cheerio");
  5.  
  6. function urlToFilename(url) {
  7. const parsedUrl = new URL(url);
  8. const hostname = parsedUrl.hostname;
  9. const pathname = parsedUrl.pathname;
  10.  
  11. return pathname === "/"
  12. ? `${hostname.slice(0, hostname.lastIndexOf("."))}/${hostname.slice(
  13. 0,
  14. hostname.lastIndexOf(".")
  15. )}.html`
  16. : `${hostname.slice(0, hostname.lastIndexOf("."))}/${pathname.slice(
  17. 0,
  18. pathname.lastIndexOf(".")
  19. )}.html`;
  20. }
  21.  
  22. function getPageLinks(url, body) {
  23. const $ = cheerio.load(body);
  24. const linkObjects = $("a");
  25. const links = [];
  26. const urlObj = new URL(url);
  27. linkObjects.each((i, elem) => {
  28. const link = $(elem).attr("href");
  29.  
  30. try {
  31. const linkObj = new URL(link);
  32. if (linkObj.hostname === urlObj.hostname) {
  33. links.push(link);
  34. }
  35. } catch (err) {
  36. // console.error(err);
  37. }
  38. });
  39.  
  40. return links;
  41. }
  42.  
  43. function saveFile(filename, contents, callback) {
  44. fs.mkdir(path.dirname(filename), { recursive: true }, (err) => {
  45. if (err) {
  46. return callback(err);
  47. }
  48.  
  49. fs.writeFile(filename, contents, callback);
  50. });
  51. }
  52.  
  53. function download(url, filename, callback) {
  54. console.log(`Downloading ${url}`);
  55.  
  56. request(url, (err, response, body) => {
  57. if (err) {
  58. return callback(err);
  59. }
  60.  
  61. saveFile(filename, body, (err) => {
  62. if (err) {
  63. return callback(err);
  64. }
  65.  
  66. console.log(`Downloaded and saved: ${url}`);
  67. callback(null, body);
  68. });
  69. });
  70. }
  71.  
  72. function spiderLinks(currentUrl, body, nesting, callback) {
  73. if (nesting === 0) {
  74. return process.nextTick(callback);
  75. }
  76. const links = getPageLinks(currentUrl, body);
  77. function iterate(index) {
  78. if (index === links.length) {
  79. return callback();
  80. }
  81.  
  82. spiderNested(links[index], nesting - 1, (err) => {
  83. if (err) {
  84. return callback(err);
  85. }
  86. iterate(index + 1);
  87. });
  88. }
  89. iterate(0);
  90. }
  91.  
  92. function spiderNested(url, nesting, callback) {
  93. const filename = urlToFilename(url);
  94. fs.readFile(filename, "utf-8", (err, body) => {
  95. if (err) {
  96. if (err.code !== "ENOENT") {
  97. return callback(err);
  98. }
  99.  
  100. return download(url, filename, (err, body) => {
  101. if (err) {
  102. return callback(err);
  103. }
  104.  
  105. spiderLinks(url, body, nesting, callback);
  106. });
  107. }
  108.  
  109. spiderLinks(url, body, nesting, callback);
  110. });
  111. }
  112.  
  113. spiderNested(process.argv[2], 1, (err) => {
  114. if (err) {
  115. console.error(err);
  116. }
  117.  
  118. console.log("All files downloaded");
  119. });
  120.  
Advertisement
Add Comment
Please, Sign In to add comment