-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (69 loc) · 1.93 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
require('dotenv').config(); // get environment variables
const PORT = process.env.PORT || 8675; // set port to env or 8675
const app = require("express")();
const axios = require("axios");
const cheerio = require("cheerio");
const sources = [
{
name: "infosecurity",
address:
"https://www.infosecurity-magazine.com/advanced-persistent-threats",
},
{
name: "hackernews",
address:
"https://www.thehackernews.com/search/label/Advanced%20Persistent%20Threat",
},
];
// search terms
const searchTerms = [
`Iranian", "Iran`,
"North Korean",
"Chinese",
"Russian",
"APT",
];
/**
* get all articles. changed to a function to prevent runs on server restart
* that might cause issues when calling the same server multiple times
* @param {Function} callback callback function to handle async returns
*/
function getAllArticles(callback) {
let articles = []; // list of articles
let completedSearches = 0; // number of searches completed
// for each source
sources.forEach((source) => {
axios.get(source.address).then((response) => {
let html = response.data;
let $ = cheerio.load(html);
// for each term
searchTerms.forEach((term) => {
// get article
$(`a:contains("${term}")`, html).each(function () {
let title = $(this).text();
let url = $(this).attr("href");
articles.push({
title,
url,
sourcename: source.name,
});
});
});
// add one to completed
completedSearches++;
// when complete, return all articles to callback
if (completedSearches === sources.length) {
callback(articles);
}
});
});
}
app.get("/", (req, res) => {
res.json("Welcome to my APT API.");
});
app.get("/news", (req, res) => {
getAllArticles((articles) => {
res.json(articles);
});
});
app.listen(PORT, () => console.log(`server running on PORT ${PORT}`));