-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·122 lines (104 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
const { Hiker } = require("./lib/hiker.js");
const { program } = require("commander");
async function createHiker(graph, username, password) {
if ((password && !username) || (username && !password)) {
console.error("Username and password must be provided together\n ");
process.exit(0);
}
try {
const hiker = new Hiker();
await hiker.connect(graph, username, password);
return hiker;
} catch (error) {
console.error("Failed to connect to database:", error.message);
return undefined;
}
}
program
.name("wikihiker")
.description(
"A web crawler for Wikipedia, finding paths between two articles."
)
.version("1.0.3");
program
.command("search")
.argument("<startURL>", "starting URL")
.argument("<targetURL>", "target URL")
.option("-d, --depth <number>", "search depth", 3)
.option("-v, --verbosity <number>", "verbosity level", 1)
.option(
"-g, --graph database <path>",
"database path",
"neo4j://localhost:7687"
)
.option("-u, --username <username>", "username", null)
.option("-p, --password <password>", "password", null)
.action(async (startURL, targetURL, options) => {
const depth = options.depth;
const graph = options.graph;
const username = options.username;
const password = options.password;
const verbosity = options.verbosity;
let searchedPages;
let res;
const hiker = await createHiker(graph, username, password);
if (!hiker) {
process.exit(0);
}
if (options.verbosity > 0) {
if (graph) {
console.log("Graph database:", graph);
}
console.log("Start URL:", startURL);
console.log("Target URL:", targetURL);
console.log("Depth:", options.depth);
console.log("\n");
}
try {
res = await hiker.hike(startURL, targetURL, depth);
searchedPages = await hiker.getDistanceTraveled();
} catch (error) {
console.error("Failed to search:", error.message);
process.exit(0);
}
if (res != undefined) {
console.log("\n");
console.log("Path found:");
res.forEach((x) => console.log(x));
console.log("\n");
console.log("Searched pages:", searchedPages);
await hiker.disconnect();
} else {
console.log("No path found");
}
process.exit(0);
})
.showHelpAfterError("(add --help or -h for additional information)");
program
.command("clear")
.option(
"-g, --graph database <path>",
"database path",
"neo4j://localhost:7687"
)
.option("-u, --username <username>", "username", null)
.option("-p, --password <password>", "password", null)
.action(async (options) => {
const username = options.username;
const password = options.password;
const graph = options.graph;
const hiker = await createHiker(graph, username, password);
if (!hiker) {
process.exit(0);
}
try {
console.log("Deleted nodes:", await hiker.forget());
} catch (error) {
console.error("Failed to clear database:", error.message);
process.exit(0);
}
await hiker.disconnect();
})
.showHelpAfterError("(add --help or -h for additional information)");
program.parse(process.argv);