-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (119 loc) · 4.14 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
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
const express = require('express');
// this will attemp to compress response bodies for all request that traverse through the middleware
const compression = require('compression'); // Nodejs compression middleware
const fs = require('fs'); // make file operation api simple
const path = require('path'); // this module provides utilities for working with file and directory paths.
const request = require('request');
const app = express();
const keys = require('./config/keys');
const port = process.env.PORT || 8080;
const snoowrap = require('snoowrap');
// Middlewares
app.use(compression());
//xpress will have knowledge that it's sitting behind a proxy and that the X-Forwarded-* header fields may be trusted,
//which otherwise may be easily spoofed.
app.enable("trust proxy");
const reddit = new snoowrap(keys);
let current = 0;
let length = 0;
let newStories = [];
let stories = [];
// const based function
const getComments = story => {
reddit.getSubmission(story.id).fetch().then( post => { // Gets info on a given submission
for(let comment of post.comments) { // look for the author name = autotldr (a bot that summaries articles)
if (comment.author.name === 'autotldr') {
current += 1;
console.log("Collecting posts no. that contains autotldr", current);
const description = comment.body_html.substr(
comment.body_html.indexOf("<blockquote>") + 13,
comment.body_html.indexOf("</blockquote>") -
comment.body_html.indexOf("<blockquote>") - 13
);
const image = post.preview && post.preview.images && post.preview.images[0] ? post.preview.images[0].resolutions[3] : [];
// append newStories into the existing array and store it in the same variable
newStories = [...newStories,
{
description,
domain: post.domain,
id: post.id,
image,
position: post.position,
title: post.title,
url: post.url
}
];
}
// If the loop has reached the end of the array, then set current and length back to 0 and store
// newStories array into stories variable.
// if ( current >= length && length > 0) {
// current = 0;
// length = 0;
// stories = newStories;
// }
}
})
.catch( err => {
console.log(err);
});
};
const getStories = () => {
// get a list of hot posts on this "worldnews" subreddit (u want the id only to get the full data later on)
reddit.getSubreddit("worldnews").getTop({ time: 'day' }).then( posts => {
newStories = [];
// console.log(posts);
length = posts.length;
console.log("Length of post collected: ", length);
for (var i=0; i<posts.length; i++) {
getComments({
...posts[i],
position: i,
});
}
})
.catch( err => {
console.log(err);
});
};
// call once before the 60s
getStories();
//getStories every 60 secs
setInterval(() => {
current = 0;
newStories = [];
getStories();
}, 60000);
// expose the end-points by sending the object stories
app.get('/worldnews', (req, res) => {
res.send({ newStories });
});
// let's encrypy free certificate authority so as to enable https
app.use((req, res, next) => {
if (req.secure || req.headers.host === `localhost:${port}` || req.url.includes('/.well-known/acme-challenge')) {
next();
} else {
res.redirect(`https://${req.headers.host}${req.url}`);
}
});
app.get(`/.well-known/acme-challenge/${process.env.LETS_ENCRYPT_ROUTE}`,
(req, res) => {
res.send(process.env.LETS_ENCRYPT_VERIFICATION);
},
);
if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets
// link our main.js file, or main.css file!
app.use(express.static('client/build')); // if there is no such route, look into the client/build directory
// Express will serve up the index.html file
// if it dosen't recognize the route
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
app.listen(port, err => {
if (err) {
console.log(err);
return;
}
console.log(`App and API is livessss at http://localhost:${port}`);
});