-
Notifications
You must be signed in to change notification settings - Fork 4
/
Start_NonTechnicalInterface.js
161 lines (134 loc) · 5.5 KB
/
Start_NonTechnicalInterface.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
let sources = require("./data/sources.json");
let articles = require("./data/articles.json");
let Download = require('./Includes/Download.js');
let ArticleScanner = require('./Includes/ArticleScanner.js');
let LinkScanner = require('./Includes/LinkScanner.js');
let Article = require('./Includes/Article.js');
let Link = require('./Includes/Link.js');
let DataManager = require('./Includes/DataManager.js');
let DataAPI = require('./Includes/DataAPI.js');
let config = require("./data/config.json");
let cheerio = require('cheerio');
let request = require('request');
let Express = require('express');
let ExpressRest = require('express-rest');
Vibrant = require('node-vibrant');
//End imports
//Todo: Make it reliable (Every word gets its proper article)
//(Image, description, reachable link, short title, containing the word)
//Todo: Make the page responsive (bootstrap grid)
//Todo: Generate different tiles (done:1x1 Todo:2x1 1x2 description)
//Inspiration https://www.theguardian.com/international
// https://www.washingtonpost.com/
function getToday(){
return Math.floor(Date.now() / 1000 / 60 / 60 / 24);
}
function getLink(link, query, rank, callback){
request({url: link.url, headers: {'User-Agent': 'request'}}, function(error, response, body) {
if(typeof response == 'undefined')
{
console.log("Error downloading: " + this.url);
callback(undefined);
return;
}
if (!error && response.statusCode == 200) {
let $ = cheerio.load(body);
let article = {
rank:rank,
link:link,
query:query,
title:$("meta[property='og:title']").attr("content"),
desc:$("meta[property='og:description']").attr("content"),
img:$("meta[property='og:image']").attr("content")
};
if(article.link != undefined && article.query != undefined && article.title != undefined && article.desc != undefined && article.img != undefined)
{
if(article.img != undefined)
Vibrant.from(article.img).getPalette((err, palette) => article.palette = palette);
let found = false;
for(let a in articles)
{
if(articles[a].link.url == article.link.url){
found = true;
break;
}
}
let endsWith = function(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
if(found == false && article.link.title != undefined && article.link.title.length < 70 && endsWith(article.link.title, "...") == false)
{
articles.push(article);
callback(article);
return;
}
else
{
callback(undefined);
return;
}
}
}
});
}
function getValidLink(api, links, query, rank, articles){
api.getLink(links[0], function(link){
getLink(link, query, rank, function(result){
if(result != undefined)
{
let articleAlreadyUsed = false;
for(let i in articles)
if(articles[i].link.title == result.link.title)
{
articleAlreadyUsed = true;
break;
}
if(articleAlreadyUsed == false){
articles.push(result);
articles.sort(function(a,b) {return (a.rank > b.rank) ? 1 : ((b.rank > a.rank) ? -1 : 0);});
console.log("Found valid article: " + articles.length + " " + JSON.stringify(query));
}
else
{
console.log("Found valid article but already in list: " + JSON.stringify(query));
}
}
else if(links != undefined){
console.log("Retry: " + JSON.stringify(query));
links.shift();
getValidLink(api, links, query, rank, articles);
}
else
console.log("Giving up on" + JSON.stringify(query));
});
});
}
let dm = new DataManager(config.redisPort, function()
{
let api = new DataAPI(dm.client);
let articles = [];
api.getMostPopularWordsOnDay(getToday(), 10, function(popular){
for(let i = 0; i < popular.length && i < 30; i++)
{
let query = [popular[i].word];
api.getSameHeadlineCountForDayAndWord(getToday(), query, function(related){
if(related[0] != undefined)
query.push(related[0].word);
console.log(query);
api.getLinksToWords(query, function(links){
getValidLink(api, links, query, i, articles);
});
});
}
});
let exp = Express();
let rest = ExpressRest(exp);
console.log("Serving: " + __dirname + '/../NonTechnicalFrontend');
exp.use("/", Express.static(__dirname + '/NonTechnicalFrontend'));
rest.get('/api/somearticles/', function(req, rest) {
return rest.ok(articles);
});
let listener = exp.listen(1001, function(){
console.log('Listening on port ' + listener.address().port);
});
});