-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
176 lines (147 loc) · 5.05 KB
/
server.js
File metadata and controls
176 lines (147 loc) · 5.05 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
console.log("🔄 Restarting ...");
// init project
const express = require("express");
const MongoClient = require("mongodb").MongoClient;
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bodyParser = require("body-parser");
const editJSONFile = require("edit-json-file");
const cron = require("node-cron");
let Parser = require("rss-parser");
let parser = new Parser({});
const file = editJSONFile("/logs.json");
// Establish a connection with the Mongo Database
// Get the username, password, host, and databse from the .env file
const mongoDB =
"mongodb+srv://" +
process.env.USERNAME +
":" +
process.env.PASSWORD +
"@" +
process.env.HOST +
"/" +
process.env.DATABASE;
// console.log("Connection String: "+mongoDB);
mongoose.connect(mongoDB, { useNewUrlParser: true, retryWrites: true });
//debugging
mongoose.connection.on("connected", function() {
// console.log('Mongoose connected to '+process.env.DATABASE);
});
mongoose.connection.on("error", function(err) {
// console.log('Mongoose connection error: '+err);
});
mongoose.connection.on("disconnected", function() {
// console.log('Mongoose disconnected.');
});
//start express
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// set the view engine
app.set("view engine", "ejs");
app.set("views", __dirname + "/views/");
// Load routes
const apiRouter = require("./routes/api");
const indexRouter = require("./routes/index");
app.use("/", indexRouter);
app.use("/api/book", apiRouter);
app.use("/api/post", apiRouter);
app.use("/api/search", apiRouter);
const url =
"mongodb+srv://" +
process.env.USERNAME +
":" +
process.env.PASSWORD +
"@" +
process.env.HOST +
"/" +
process.env.DATABASE;
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});
//Healper Functions
function setKey(item, index) {
var externalSourceUrl = "{ externalSourceUrl: " + item + "}";
return externalSourceUrl;
}
// CRON JOBS
cron.schedule("* * * * *", () => {
console.log("Checking RSS");
//IMPORT LINKS FROM DATABASE
// let sitesToCheck = [
// "http://gndclouds.cc/feed/feed.xml",
// "https://interconnected.org/home/feed"
// ];
// Example Data with correct Schema
let sitesToCheck = [
{
url: "https://interconnected.org/home/feed",
userID: "MQc2aPSY0763",
lastChecked: "2021-01-31T13:14:44.639+00:00",
lastBuildDate: "2021-04-04T00:00:00Z"
}
// {
// url: "http://gndclouds.cc/feed/feed.xml",
// userID: "MQc2aPSY0763",
// lastChecked: "2021-01-31T13:14:44.639+00:00",
// lastBuildDate: "2021-04-04T00:00:00Z"
// },
// {
// url: "https://futureland.tv/gndclouds/gndclouds-cc.rss",
// userID: "MQc2aPSY0763",
// lastChecked: "2021-01-31T13:14:44.639+00:00",
// lastBuildDate: "Fri, 07 May 2021 13:28:01 +0000"
// }
];
// Checking Links for change to Data
for (let i = 0; i < sitesToCheck.length; i++) {
const arr = []; // make new array for articles
console.log("Checking " + sitesToCheck[i].url + "for updates");
(async () => {
let feed = await parser.parseURL(sitesToCheck[i].url); // EDIT: pass in i from array
// Chech feed data
// Check feed data and db date to determine if there has been a change in the RSS
if (sitesToCheck[i].lastBuildDate !== feed.lastBuildDate) {
// if dates are not the same then ...
console.log(
"UPDATING: " +
sitesToCheck[i].url +
" has new posts or changes to old posts"
);
const arr = [];
feed.items.forEach(item => {
// Preform a lookup on that feed and get the articles links
let linkLookUp = item.link;
// const cd = arr.map(externalSourceUrl => ({[externalSourceUrl]: linkLookUp}));
arr.push(linkLookUp);
});
var output = arr.map(setKey);
// console.log(output);
// Search for the array lf articels in the DB
MongoClient.connect(url, { useUnifiedTopology: true }, function(
err,
db
) {
if (err) throw err;
var dbo = db.db("tinyprofiles");
//Find the first document in the customers collection:
// console.log("Array 0" + output[0]);
// console.log("Array 1" + output[1]);
dbo.collection("posts").find(output, function(err, result) {
// stack the array here some how
// Search by discordCreatorId
if (err) throw err;
console.log(result); // FIX: CLean this up (@Will: Look at if this is the correct varaible or if there is a way to retunr a lis of which object are and are not found fromt he search)
db.close();
console.log("DB Closed");
});
});
} else {
// If dates are the same then do nothing
console.log("SKIPPING: " + sitesToCheck[i].url + " has no changes");
}
})();
arr.length = 0; // Clear Array
}
});