Skip to content

Commit b03dcfd

Browse files
committed
Updated files
2 parents 66faf3f + 9b61a69 commit b03dcfd

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

spider5/spider.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ const TaskQueue = require("./taskQueue");
1414
const validator = require("./validator");
1515

1616
function spiderLinks(currentUrl, body, nesting, callback) {
17-
if(nesting === 0) {
17+
if(nesting === 0)
1818
return process.nextTick(callback);
19-
}
2019

2120
const links = utilities.getPageLinks(currentUrl, body);
22-
if(links.length === 0) {
21+
if(links.length === 0)
2322
return process.nextTick(callback);
23+
<<<<<<< HEAD
2424
}
25+
=======
26+
>>>>>>> development
2527
let downloadQueue = new TaskQueue(cmdConfig.get("concurrency",2));
2628

2729
let completed = 0, hasErrors = false;
@@ -93,14 +95,27 @@ function spider(url, nesting, callback) {
9395
});
9496
}
9597

96-
if (!validator.validate())
97-
process.exit();
98+
const errors = validator.validate();
99+
if (errors.length || cmdConfig.get("help"))
100+
{
101+
console.log(cmdConfig.usage);
102+
errors.forEach((err) =>
103+
{
104+
console.error(err);
105+
});
106+
process.exit(errors.length);
107+
}
98108

99109
spider(cmdConfig.get("url"), cmdConfig.get("nesting",1), (err) => {
100110
if(err) {
101-
console.log(err);
102-
process.exit();
111+
console.error(err);
112+
process.exit(1);
103113
} else {
104114
console.log("Download complete");
115+
<<<<<<< HEAD
116+
=======
117+
process.exit(0);
118+
>>>>>>> development
105119
}
106120
});
121+

spider5/validator.js

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const validator = require("validator");
66
const cmdConfig = require("./cmdconfig");
77
const assert = require("assert");
88

9+
<<<<<<< HEAD
910
module.exports.validate = function()
1011
{
1112
const options = cmdConfig.options;
@@ -56,12 +57,65 @@ assert(options._all.url,"No url specified");
5657
console.error(err.message);
5758
}
5859
}
59-
60-
if (assertCount || options._all.help)
61-
{
62-
console.error(cmdConfig.usage);
63-
return false;
64-
}
65-
66-
return true;
60+
=======
61+
module.exports.validate = function() {
62+
const options = cmdConfig.options;
63+
let errors = [];
64+
errors = validateEmptyURL(options._all.url, errors);
65+
errors = validateURLFormat(options._all.url, errors);
66+
errors = validateConcurrency(options._all.concurrency, errors);
67+
errors = validateNesting(options._all.nesting, errors);
68+
return errors;
6769
};
70+
71+
function validateEmptyURL(url, errors) {
72+
try {
73+
assert(url, "No url specified");
74+
} catch (err) {
75+
errors.push(err.message);
76+
}
77+
return errors;
78+
}
79+
>>>>>>> development
80+
81+
function validateURLFormat(url, errors) {
82+
if (url) {
83+
try {
84+
assert(validator.isURL(url, {
85+
protocols: ["http", "https"],
86+
require_host: true,
87+
require_valid_protocol: true,
88+
require_protocols: true
89+
}), url + " is invalid.");
90+
} catch (err) {
91+
errors.push(err.message);
92+
}
93+
}
94+
return errors;
95+
}
96+
97+
function validateConcurrency(concurrency, errors) {
98+
if (concurrency !== undefined) {
99+
try {
100+
assert(validator.isInt(concurrency.toString(), {
101+
min: 1
102+
}), "Concurrency must be greater than 0");
103+
} catch (err) {
104+
errors.push(err.message);
105+
}
106+
}
107+
return errors;
108+
}
109+
110+
function validateNesting(nesting, errors) {
111+
if (nesting !== undefined) {
112+
try {
113+
assert(validator.isInt(nesting.toString(), {
114+
gt: 0
115+
}), "Nesting must be greater than 0");
116+
} catch (err) {
117+
errors.push(err.message);
118+
}
119+
}
120+
return errors;
121+
}

0 commit comments

Comments
 (0)