Advertisement
Guest User

Untitled

a guest
Mar 25th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. import fetch from "node-fetch";
  2. import readline from "readline";
  3.  
  4. const rl = readline.createInterface({
  5. input: process.stdin,
  6. output: process.stdout
  7. })
  8.  
  9. rl.question(`Random Wikipedia article or input query? (R) (I)`, c => {
  10. if (c == "R") {
  11. getRandomTitle();
  12. rl.close()
  13. }
  14. else if (c == "I") {
  15. rl.question(`Please input a wikipedia article.`, c2 => {
  16. findPath(c2);
  17. rl.close()
  18. })
  19. }
  20. })
  21.  
  22. function getRandomTitle() {
  23. fetch("https://en.wikipedia.org/w/api.php?action=query&list=random&format=json&rnnamespace=0&rnlimit=1")
  24. .then(function (response) { return response.json(); })
  25. .then(function (response) {
  26. var t = response.query.random[0].title;
  27. findPath(t);
  28. })
  29. }
  30. function findPath(query) {
  31. var cur = query;
  32. console.log(cur);
  33.  
  34. doDoom(makeUrl(cur));
  35.  
  36.  
  37.  
  38. function doDoom(url) {
  39. fetch(url)
  40. .then(function (response) { return response.json(); })
  41. .then(function (response) {
  42.  
  43. var pages = response.query.pages[Object.keys(response.query.pages)[0]].revisions[0]['*'];
  44. var t = pages.toLowerCase();
  45.  
  46. var str = t
  47. var indices = [];
  48. for (var i = 0; i < str.length; i++) {
  49. if (str[i] === "[") indices.push(i);
  50. }
  51.  
  52. if (cur.includes(" ")) {
  53. pages = pages.substring(t.substring(0, t.indexOf("'''" + cur.toLowerCase().substring(0, cur.indexOf(" ")))).lastIndexOf("\n"));
  54. }
  55. else {
  56. pages = pages.substring(t.substring(0, t.indexOf("'''" + cur.toLowerCase())).lastIndexOf("\n"));
  57. }
  58.  
  59. pages = removePar(pages);
  60.  
  61. while (pages.substring(pages.indexOf("[[") + 2, pages.indexOf("[[") + 2 + 5) == "File:") {
  62. pages = pages.substring(pages.indexOf("]]") + 2);
  63. }
  64. pages = pages.substring(pages.indexOf("[[") + 2, pages.indexOf("]]"));
  65.  
  66. if (pages.includes("|")) {
  67. pages = pages.substring(0, pages.indexOf("|"));
  68. }
  69. if (pages.includes("#")) {
  70. if (pages.indexOf("#") == 0) {
  71. pages = pages.substring(pages.indexOf("#") + 1);
  72. }
  73. else {
  74. pages = pages.substring(0, pages.indexOf("#"));
  75. }
  76. }
  77. cur = pages;
  78. //incase parent title
  79.  
  80. for(var i in indices){
  81. if(cur.toLowerCase() == t.substring(indices[i] + 1, indices[i] + cur.length+ 1) && t.substring(indices[i] + 1, indices[i] + 1 + t.substring(indices[i] + 1).indexOf("]]")).includes(")") ){
  82. cur = t.substring(indices[i] + 1, indices[i] + 2 + t.substring(indices[i] + 1).indexOf(")") );
  83. }
  84. if(cur.toLowerCase() + "|" == t.substring(indices[i] + 1, indices[i] + cur.length+ 2)){
  85. break;
  86. }
  87. }
  88. console.log(cur);
  89. //console.log("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
  90. if (cur != "philosophy" && cur != "Philosophy" ) {
  91. doDoom(makeUrl(cur));
  92. }
  93. })
  94. .catch(function (error) { console.log(error); });
  95. }
  96.  
  97.  
  98.  
  99. }
  100. function removePar(str) {
  101. var Output = str;
  102. while (Output != (Output = Output.replace(/\s*\([^()]*\)/g, "")));
  103. while (Output != (Output = Output.replace(/<[^<>]*>/g, "")));
  104. Output = replaceBrackets(Output);
  105. return Output;
  106. }
  107. function replaceBrackets(text) {
  108. var str = text;
  109. var result = '';
  110. var lvl = 0;
  111. for (var i = 0, len = str.length; i < len; i++) {
  112. var ch = str.charAt(i);
  113. if (ch == '{')
  114. lvl++;
  115.  
  116. if (lvl == 0)
  117. result += ch;
  118.  
  119. if (ch == '}')
  120. lvl--;
  121. }
  122. return result.replace(/\s{2,}/g, ' ');
  123. }
  124. function checkRedLink(url){
  125. fetch(url)
  126. .then(function (response) { return response.json(); })
  127. .then(function (response) {
  128.  
  129.  
  130.  
  131. })
  132. }
  133. function makeUrl(wha) {
  134. var u = "https://en.wikipedia.org/w/api.php";
  135.  
  136. var params = {
  137. action: "query",
  138. titles: wha,
  139. prop: "revisions",
  140. rvprop: "content",
  141. format: "json",
  142. };
  143.  
  144. u = u + "?origin=*";
  145. Object.keys(params).forEach(function (key) { u += "&" + key + "=" + params[key]; });
  146. return u;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement