-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitorWeb.js
74 lines (71 loc) · 3.42 KB
/
monitorWeb.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
var request = require('sync-request');
var firebase = require('firebase');
// Initialize Firebase
var config = {
apiKey: "AIzaSyACmrWGV7KPqfHVJQLbfxyTU1DIWWkg55M",
authDomain: "page-monitor-b5405.firebaseapp.com",
databaseURL: "https://page-monitor-b5405.firebaseio.com",
storageBucket: "page-monitor-b5405.appspot.com",
messagingSenderId: "923435609725"
};
firebase.initializeApp(config);
return firebase.database().ref().once('value').then(function(snapshot) {
var allUserData = snapshot.val();
for (var userKey in allUserData) {
var webpages = allUserData[userKey].webpages;
for (var webKey in webpages) {
var webpage = webpages[webKey];
console.log("Checking: ", webpage.pageLink);
var response = request('GET', webpage.pageLink);
var body = response.body.toString('utf-8');
var webpageData = webpage;
if (response.statusCode == 200) {
if (webpageData.source !== body) {
var date = new Date();
var webData = {
pageTitle: webpageData.pageTitle,
pageLink: webpageData.pageLink,
frequency: webpageData.frequency,
keywords: webpageData.keywords,
lastChecked: date.getDate() + "-" + (parseInt(date.getMonth())+1) + "-" + date.getFullYear(),
lastModified: date.getDate() + "-" + (parseInt(date.getMonth())+1) + "-" + date.getFullYear(),
source: body
};
var updates = {};
updates['/' + userKey + '/webpages' + '/' + webKey] = webData;
firebase.database().ref().update(updates).then(response => {
console.log(webpageData.pageLink + " has changed.");
}).catch(function(error) {
console.error(error);
});
} else {
var date = new Date();
var webData = {
pageTitle: webpageData.pageTitle,
pageLink: webpageData.pageLink,
frequency: webpageData.frequency,
keywords: webpageData.keywords,
lastChecked: date.getDate() + "-" + (parseInt(date.getMonth())+1) + "-" + date.getFullYear(),
lastModified: webpageData.lastModified,
source: webpageData.source
};
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
updates['/' + userKey + '/webpages' + '/' + webKey] = webData;
firebase.database().ref().update(updates).then(response => {
console.log(webpageData.pageLink + " has not changed.");
}).catch(function(error) {
console.error(error);
});
}
}
else {
console.log(error);
}
// });
}
}
// process.exit();
}).catch(function(error) {
console.log("Database ref: ", error);
});