-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
82 lines (70 loc) · 2.51 KB
/
app.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
const puppeteer = require("puppeteer");
const path = require("path");
const logger = require("morgan");
const http = require("http");
const express = require("express");
const app = express();
app.set("x-powered-by", false);
app.use(logger("combined"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(__dirname + "/dist/Melodify"));
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
// Request methods you wish to allow
res.setHeader("Access-Control-Allow-Methods", "GET", "POST");
// Request headers you wish to allow
res.setHeader("Access-Control-Allow-Headers", "*");
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader("Access-Control-Allow-Credentials", true);
// Pass to next layer of middleware
next();
});
app.get("*", function (req, res, next) {
res.sendFile(path.resolve(__dirname + "/dist/Melodify/index.html"));
});
app.post("/scrape", async function (req, res) {
const url = req.body.url;
async function scrape(url) {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: true,
});
const page = await browser.newPage();
await page
.goto(url)
.then(async () => {
const pAll = await page.$$("p");
var p;
var arrOfWords = [];
//going through all the p tags
for (p of pAll) {
//getting the content
let txt = await p.getProperty("textContent");
let rawTxt = await txt.jsonValue();
//splitting the words from
arrOfWordsNew = rawTxt.split(" ");
//adding to the array of words
arrOfWords = arrOfWords.concat(arrOfWordsNew);
}
// top 150 words
arrOfWords = arrOfWords.slice(0, 150);
// getting the first character of words
var wordAndLength = arrOfWords.map((a) => [a, a.length]);
var charAndLength = arrOfWords.map((a) => [a[0], a.length]);
// console.log(charAndLength);
console.log(wordAndLength);
res.send(wordAndLength);
browser.close();
})
.catch((err) => {
console.log(`Error: ${err}`);
});
}
scrape(url);
});
var server = http.createServer(app);
server.listen(process.env.PORT || 8080);
server.on("error", (err) => console.log("Error Starting Server", error));
server.on("listening", () => console.log("Server Running on port 8080"));