-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
44 lines (39 loc) · 1.32 KB
/
main.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
const PORT = 8080;
const express = require("express");
const axios = require("axios");
const cheerio = require("cheerio");
const app = express();
// api usage:
/*
/vg?&inquiry=searchword eks bitcoin
This will return all articles on bitcoin
*/
app.get("/vg", (req, res) => {
let param = req.query.inquiry;
let paramCap = param.charAt(0).toUpperCase() + param.slice(1);
axios.get("https://www.vg.no")
.then((response) => {
const html = response.data;
let $ = cheerio.load(html);
let responseArr = [];
let items = $(".article-container");
items.each((i, el) => {
let base = $(el).children("a").children("div").children("h3").attr("aria-label");
if (base !== undefined) {
if (base.includes(paramCap) || base.includes(param.toLowerCase())) {
let url = $(el).children("a").attr("href");
responseArr.push({
Title: base,
Link: url
});
}
}
});
res.json(responseArr);
}).catch((error) => {
console.log(error);
})
});
app.listen(PORT, () => {
console.log(`server started at port: ${PORT}`)
});