-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_entries.js
83 lines (67 loc) · 2.45 KB
/
parse_entries.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
var CONFIG = require('./public/js/config.js');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var RssParser = require('rss-parser');
var Sentiment = require('sentiment');
var Chalk = require('chalk');
var url = CONFIG.DB_URL + CONFIG.DB_NAME;
var docs = [];
var processed = 0;
CONFIG.RSS_CHANNELS.forEach(parseChannels);
function parseChannels( element, index, array ) {
RssParser.parseURL(element, function( err, parsed ) {
//assert.equal(null, err);
if ( !err ) {
console.log('\n');
console.log(Chalk.black.bgWhite(' ||=======|| Parsing Channel ' + parsed.feed.title + ' ||=======|| '));
parsed.feed.entries.forEach(function(entry) {
console.log(' === Parsing entry -> ' + entry.title + ' ===')
var sent = Sentiment(entry.title)
var ent = { _id: entry.link, title: entry.title, link: entry.link, source: parsed.feed.title.replace(/\W/g, ''), score: sent.score, comparative: parseFloat(sent.comparative.toFixed(3)), pub_date: new Date(entry.pubDate) }
docs.push(ent);
});
} else {
console.log('\n');
console.log(Chalk.black.bgRed(' ||=======|| Error parsing channel Channel ' + element + ' ||=======|| '));
console.log(err);
}
processed += 1;
// sloppy way to sync processed feeds with RSS_CHANNELS array length
if ( processed == array.length ) {
processEntries(docs);
}
});
};
function processEntries( docs ) {
console.log(Chalk.black.bgYellow(' ||=======|| Process entries ||=======|| '));
//console.log(docs);
MongoClient.connect(url, function( err, db ) {
assert.equal(null, err);
console.log('Connected correctly to server');
insertEntries(db, function() {
console.log("Finished on " + new Date());
db.close();
}, docs);
});
};
var insertEntries = function( db, callback, docs ) {
console.log(Chalk.black.bgYellow(' ||=======|| Insert Entries ||=======|| '));
var collection = db.collection('entries');
docs.forEach(function(el, ind, arr){
//console.log(el._id);
collection.findAndModify({ _id: el._id },
[],
{ $set: el },
{ upsert: true },
function( err, result ) {
console.log(Chalk.black.bgGreen(' ||=======|| UPDATE OR NOT ||=======|| '));
//console.log(el._id);
assert.equal(null, err);
console.log('RESULT');
console.log(result);
if (ind == arr.length - 1) {
callback(result);
}
});
});
};