Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fetch from "node-fetch";
- import readline from "readline";
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- })
- rl.question(`Random Wikipedia article or input query? (R) (I)`, c => {
- if (c == "R") {
- getRandomTitle();
- rl.close()
- }
- else if (c == "I") {
- rl.question(`Please input a wikipedia article.`, c2 => {
- findPath(c2);
- rl.close()
- })
- }
- })
- function getRandomTitle() {
- fetch("https://en.wikipedia.org/w/api.php?action=query&list=random&format=json&rnnamespace=0&rnlimit=1")
- .then(function (response) { return response.json(); })
- .then(function (response) {
- var t = response.query.random[0].title;
- findPath(t);
- })
- }
- function findPath(query) {
- var cur = query;
- console.log(cur);
- doDoom(makeUrl(cur));
- function doDoom(url) {
- fetch(url)
- .then(function (response) { return response.json(); })
- .then(function (response) {
- var pages = response.query.pages[Object.keys(response.query.pages)[0]].revisions[0]['*'];
- var t = pages.toLowerCase();
- var str = t
- var indices = [];
- for (var i = 0; i < str.length; i++) {
- if (str[i] === "[") indices.push(i);
- }
- if (cur.includes(" ")) {
- pages = pages.substring(t.substring(0, t.indexOf("'''" + cur.toLowerCase().substring(0, cur.indexOf(" ")))).lastIndexOf("\n"));
- }
- else {
- pages = pages.substring(t.substring(0, t.indexOf("'''" + cur.toLowerCase())).lastIndexOf("\n"));
- }
- pages = removePar(pages);
- while (pages.substring(pages.indexOf("[[") + 2, pages.indexOf("[[") + 2 + 5) == "File:") {
- pages = pages.substring(pages.indexOf("]]") + 2);
- }
- pages = pages.substring(pages.indexOf("[[") + 2, pages.indexOf("]]"));
- if (pages.includes("|")) {
- pages = pages.substring(0, pages.indexOf("|"));
- }
- if (pages.includes("#")) {
- if (pages.indexOf("#") == 0) {
- pages = pages.substring(pages.indexOf("#") + 1);
- }
- else {
- pages = pages.substring(0, pages.indexOf("#"));
- }
- }
- cur = pages;
- //incase parent title
- for(var i in indices){
- 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(")") ){
- cur = t.substring(indices[i] + 1, indices[i] + 2 + t.substring(indices[i] + 1).indexOf(")") );
- }
- if(cur.toLowerCase() + "|" == t.substring(indices[i] + 1, indices[i] + cur.length+ 2)){
- break;
- }
- }
- console.log(cur);
- //console.log("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
- if (cur != "philosophy" && cur != "Philosophy" ) {
- doDoom(makeUrl(cur));
- }
- })
- .catch(function (error) { console.log(error); });
- }
- }
- function removePar(str) {
- var Output = str;
- while (Output != (Output = Output.replace(/\s*\([^()]*\)/g, "")));
- while (Output != (Output = Output.replace(/<[^<>]*>/g, "")));
- Output = replaceBrackets(Output);
- return Output;
- }
- function replaceBrackets(text) {
- var str = text;
- var result = '';
- var lvl = 0;
- for (var i = 0, len = str.length; i < len; i++) {
- var ch = str.charAt(i);
- if (ch == '{')
- lvl++;
- if (lvl == 0)
- result += ch;
- if (ch == '}')
- lvl--;
- }
- return result.replace(/\s{2,}/g, ' ');
- }
- function checkRedLink(url){
- fetch(url)
- .then(function (response) { return response.json(); })
- .then(function (response) {
- })
- }
- function makeUrl(wha) {
- var u = "https://en.wikipedia.org/w/api.php";
- var params = {
- action: "query",
- titles: wha,
- prop: "revisions",
- rvprop: "content",
- format: "json",
- };
- u = u + "?origin=*";
- Object.keys(params).forEach(function (key) { u += "&" + key + "=" + params[key]; });
- return u;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement