Skip to content

Commit

Permalink
Check urls in the posts pulled from telescope
Browse files Browse the repository at this point in the history
  • Loading branch information
katyasichov committed Nov 6, 2020
1 parent aca94df commit 5b3e109
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 60 deletions.
76 changes: 16 additions & 60 deletions Utest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
const chalk = require("chalk");
const boxen = require("boxen");
const yargs = require("yargs");
const fetch = require("node-fetch");
const fs = require("fs");
const version = require("./package.json").version;
const { boxenOptions } = require("./styling/style");

const posts = require("./telescope/posts");
const fileHandler = require("./utils/fileProcessing");

const vmessage = chalk.white.bold(`Utest version: ${version}`);
const msgBox = boxen(vmessage, boxenOptions);
Expand All @@ -16,80 +15,37 @@ const argv = yargs
.usage("Usage: $0 [option]")
.alias("f", "file")
.nargs("f", 1)
.describe("f", "Load a file")
.demandOption(["f"])
.describe("f", "Load a file")
.help("h")
.alias("h", "help")
.alias("v", "version")
.alias("V", "version")
.alias("j", "json")
.alias("t", "telescope")
.describe("telescope", "check urls in telescope last 10 posts")
.describe("json", "print in JSON format")
.version(`${msgBox}`)
.describe("version", "show version information").argv;

// Create stream with the file
const s = fs.createReadStream(argv.file);

let urlList;
s.on("data", (buf) => {
// Get all the URL links from the file
urlList = buf
.toString()
.match(/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi);
});

//print responses colorized
function printResponse(data) {
for (var item of data) {
if (item.status == 200) {
console.log(
chalk.green.bold(`[GOOD] Status: [${item.status}] ${item.url}`)
);
} else if (item.status == 400 || item.status == 404) {
console.log(chalk.red.bold(`[BAD] Status: [${item.status}] ${item.url}`));
} else {
console.log(
chalk.grey.bold(`[UNKNOWN] Status: [${item.status}] ${item.url}`)
);
}
}
if(argv.telescope)
{
posts.listPosts();
}

function checkStatus(data) {
for (var item of data) {
if (item.status == 400 || item.status == 404 || item.status == "UNKNOWN") {
return 1;
}
if(argv.file)
{
if(argv.json)
{
fileHandler.processFile(argv.file,true);
}else{
fileHandler.processFile(argv.file);
}
return 0;

}

s.on("end", async () => {
var responseStatusByUrl = [];
var statusResponseForUrl;

//Iterate through the links and check their status

await Promise.all(
urlList.map(async (url) => {
try {
const urlTest = await fetch(url, { method: "head", timeout: 1500 });
statusResponseForUrl = { url: `${url}`, status: `${urlTest.status}` };
responseStatusByUrl.push(statusResponseForUrl);
} catch (error) {
statusResponseForUrl = { url: `${url}`, status: "UNKNOWN" };
responseStatusByUrl.push(statusResponseForUrl);
}
})
);
if (argv.json) {
console.log(JSON.stringify(responseStatusByUrl));
} else {
printResponse(responseStatusByUrl);
}

process.exit(checkStatus(responseStatusByUrl));
});
process.on("exit", function (code) {
return console.log(`About to exit with code ${code}`);
});
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"homepage": "https://github.com/egrinberg/Utest#readme",
"dependencies": {
"axios": "^0.21.0",
"boxen": "^4.0.0",
"chalk": "^2.4.2",
"node-fetch": "^2.6.1",
Expand Down
63 changes: 63 additions & 0 deletions telescope/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

const axios = require('axios');
const fs = require("fs");
const fileHandler = require("../utils/fileProcessing");
const fetch = require("node-fetch");



const listPosts = async () => {
try {

var postsURL = 'http://localhost:3000/posts';

const response = await fetch(postsURL);
const data = await response.json();


var ids = [];

for(var item of data) {
ids.push(item.id);
}

Promise.all(
ids.map(async (id) => {
try {
var postUrl = postsURL.concat('/',id);
await writePostToFile(postUrl,id);

} catch (error) {
console.log(error);
}
})
);

} catch (err) {
console.error(err);
}
};


const writePostToFile = async (post,id) => {

fetch(post)
.then(
res =>
new Promise((resolve, reject) => {
const path = `./telescope/posts/${id}`;
const dest = fs.createWriteStream(path);
res.body.pipe(dest);
res.body.on("end", () => resolve(path));
dest.on("error", reject);
})
)
.then(path => {
console.log(path);
let p = `${path}`;
fileHandler.processFile(p);
});
}

module.exports = {listPosts};

85 changes: 85 additions & 0 deletions utils/fileProcessing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

const fetch = require("node-fetch");
const chalk = require("chalk");
const fs = require("fs");



let urlList;

const processFile = (filename,json) => {

// Create stream with the file
let s = fs.createReadStream(filename);

return new Promise((resolve, reject) => {

s.on("data", (buf) => {
// Get all the URL links from the file
urlList = buf
.toString()
.match(/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi);
// console.log(`${filename} ${urlList}`)
});

s.on("end", async () => {
var responseStatusByUrl = [];
var statusResponseForUrl;

//Iterate through the links and check their status

await Promise.all(
urlList.map(async (url) => {
try {
const urlTest = await fetch(url, { method: "head", timeout: 1500 });
statusResponseForUrl = { url: `${url}`, status: `${urlTest.status}` };
responseStatusByUrl.push(statusResponseForUrl);
} catch (error) {
statusResponseForUrl = { url: `${url}`, status: "UNKNOWN" };
responseStatusByUrl.push(statusResponseForUrl);
}
})
);
if (json) {
console.log(JSON.stringify(responseStatusByUrl));
} else {
printResponse(responseStatusByUrl);
}

process.exit(checkStatus(responseStatusByUrl));

});

s.on("error",error => reject(error));
});


}

//print responses colorized
function printResponse(data) {
for (var item of data) {
if (item.status == 200) {
console.log(
chalk.green.bold(`[GOOD] Status: [${item.status}] ${item.url}`)
);
} else if (item.status == 400 || item.status == 404) {
console.log(chalk.red.bold(`[BAD] Status: [${item.status}] ${item.url}`));
} else {
console.log(
chalk.grey.bold(`[UNKNOWN] Status: [${item.status}] ${item.url}`)
);
}
}
}

function checkStatus(data) {
for (var item of data) {
if (item.status == 400 || item.status == 404 || item.status == "UNKNOWN") {
return 1;
}
}
return 0;
}

module.exports = { processFile }

0 comments on commit 5b3e109

Please sign in to comment.