-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
175 lines (153 loc) · 5.39 KB
/
server.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var express = require("express");
var fs = require("fs");
var request = require("request");
var cheerio = require("cheerio");
var bodyParser = require("body-parser");
var app = express();
const cors = require("cors");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.listen("8081");
app.post("/scrape", function(req, res) {
try {
// console.log(req.body);
var url = req.body.url;
if (url) {
var start = new Date();
request(url, function(error, response, html) {
if (!error) {
console.log("Request took:", new Date() - start, "ms");
// console.log("response =>", response);
var $ = cheerio.load(html);
// console.log("html =>", html)
// console.log("h1 =>", $("h1").length);
// console.log("h2 =>", $("h2").length);
// console.log("h3 =>", $("h3").length);
// console.log("h4 =>", $("h4").length);
// console.log("h5 =>", $("h5").length);
// console.log("h6 =>", $("h6").length);
var responseTime = new Date() - start;
var numberOfHeadings =
$("h1").length +
$("h2").length +
$("h3").length +
$("h4").length +
$("h5").length +
$("h6").length;
var h1 = $("h1").length;
var h2 = $("h2").length;
var h3 = $("h3").length;
var h4 = $("h4").length;
var h5 = $("h5").length;
var h6 = $("h6").length;
var title, release, rating;
var json = { title: "", release: "", rating: "" };
title = $("title").text();
// var html = cheerio.parseHTML(html);
// if(html) {
// json.html = html;
// }
var links = [];
var externalLinks = [];
var internalinks = [];
var images = [];
$("a").each((index, value) => {
var link = $(value).attr("href");
// console.log("link", link);
if (link && link !== "" && link !== undefined) {
if (
link.includes(response.request.uri.hostname) ||
link.startsWith("#") ||
link.startsWith("/")
) {
internalinks.push({ link });
} else {
externalLinks.push({ link });
}
}
});
var currDimension = 0;
var maxDimension = 0;
var maxImage = null;
$("img").each((index, value) => {
// console.log("value =>", value)
var image = $(value).attr("data-cfsrc");
var image_data_src = $(value).attr("data-src");
var image_src = $(value).attr("src");
if (image) {
console.log("image =>", image);
images.push({image: image});
currDimension =$(value).attr("width") *$(value).attr("height");
if (currDimension > maxDimension) {
maxDimension = currDimension;
maxImage = image;
}
} else if (image_data_src) {
currDimension = $(value).attr("width") * $(value).attr("height");
if (currDimension > maxDimension) {
maxDimension = currDimension;
maxImage = image_data_src;
}
console.log("image =>", image_data_src);
images.push({image: image_data_src});
} else if(image_src) {
currDimension = $(value).attr("width") * $(value).attr("height");
if (currDimension > maxDimension) {
maxDimension = currDimension;
maxImage = image_src;
}
console.log("image =>", image_src);
images.push({image: image_src});
}
});
console.log("maxImage => ", maxImage);
console.log("maxDimension => ", maxDimension);
console.log("currDimension => ", currDimension);
json.responseTime = responseTime;
json.title = title;
json.maxImage = maxImage;
json.numberOfHeadings = numberOfHeadings;
json.html = html;
json.h1 = h1;
json.h2 = h2;
json.h3 = h3;
json.h4 = h4;
json.h5 = h5;
json.h6 = h6;
json.links = links;
json.internalinks = internalinks;
json.externalLinks = externalLinks;
json.images = images;
} else {
// console.log("error => ", error);
let message = "Something went wrong!";
if (
error["code"] === "ECONNREFUSED" ||
error["code"] === "ENOTFOUND"
) {
message = "URL is not reachable";
}
res.status(500).send({ ...error, message });
}
// console.log(json);
res.send(json);
});
} else {
res.status(500).send({ message: "URL missing" });
}
} catch (error) {
// console.log("err 12=>", error);
res.status(500).send({ message: "Something went wrong" });
}
});
app.get("*", function(req, res) {
res.status(404).send("Not Found");
});
app.post("*", function(req, res) {
res.status(404).send("Not Found");
});
app.use(express.static("public", { index: "index.html" }));
console.log("Navigate to http://localhost:8081/");
exports = module.exports = app;