forked from PatrickJS/Reddit-Insight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbLibrary.js
146 lines (138 loc) · 4.6 KB
/
dbLibrary.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
var mongoose = require('mongoose'),
lib = require('./myLibrary'),
_ = require('underscore'),
Schema = mongoose.Schema;
module.exports = {
_postsSchema: null,
_subsSchema: null,
_moodel: null,
_throttledPullData: null,
_options: {
host: 'www.reddit.com',
//path, see start()
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
_createSubsSchema: function() {
return new mongoose.Schema({
header_img: String,
header_title: String,
description: String,
description_html: String,
title: String,
url: String,
created: Number,
created_utc: Number,
public_description: String,
accounts_active: String,
over18: Boolean,
header_size: Schema.Types.Mixed,
subscribers: Number,
display_name: String,
id: String,
name: {type: String, index: {unique: true, dropDups: true}}
}, { autoIndex: true });
},
_createPostsSchema: function() {
return new mongoose.Schema({
domain:String,
banned_by:String,
media_embed: Schema.Types.Mixed,
subreddit:String,
selftext_html: Schema.Types.Mixed,
selftext:Schema.Types.Mixed,
likes:Schema.Types.Mixed,
link_flair_text:Schema.Types.Mixed,
id: String,
clicked:Boolean,
title:String,
media:Schema.Types.Mixed,
score:Number,
approved_by:Schema.Types.Mixed,
over_18:Boolean,
hidden:Boolean,
thumbnail:String,
subreddit_id:String,
edited:Boolean,
link_flair_css_class:Schema.Types.Mixed,
author_flair_css_class:Schema.Types.Mixed,
downs:Number,
saved:Boolean,
is_self: Boolean,
permalink: String,
name: {type: String, index: {unique: true, dropDups: true}},
created: Number,
url: String,
author_flair_text: Schema.Types.Mixed,
author: String,
created_utc: Number,
ups: Number,
num_comments: Number,
num_reports: Schema.Types.Mixed,
distinguished: Schema.Types.Mixed
}, { autoIndex: true });
},
start: function(interval, path, schema) {
//schema posts, subs
this._throttledPullData = _.throttle(lib.getJSON, 2000);
mongoose.connect("mongodb://localhost/RedditInsight");
var self = this;
var dataBase = mongoose.connection;
dataBase.on('err', console.error.bind(console, 'connection error:'));
dataBase.once('open', function(){
console.log('connected to dataBase: ', dataBase.db.databaseName);
if(schema === 'posts'){
self._postsSchema = self._createPostsSchema();
self._moodel = mongoose.model('AllTopPosts', self._postsSchema);
} else if(schema === 'subs'){
self._subsSchema = self._createSubsSchema();
self._moodel = mongoose.model('allsubs', self._subsSchema);
} else {
throw "Unknown schema!: '"+ schema + "', should be posts or subs";
}
self._options.path = path;
var intervalId = setInterval(function(intervalId) {
self.pullData(intervalId);
}, interval);
});
},
pullData: function(intervalId){
var self = this;
this._moodel.count({}, function(err, count) {
if(err){
console.log('from count error: ', JSON.stringify(err));
clearInterval(intervalId);
console.log('Stopped intervalId: ', intervalId);
throw JSON.stringify(err);
} else if (count) {
self._moodel.findOne({}, {name: 1}, {sort:{_id : -1}}, function (err, doc) {
if(err){
console.log('from find error: ', JSON.stringify(err));
clearInterval(intervalId);
console.log('Stopped intervalId: ', intervalId);
throw JSON.stringify(err);
}
var afterString = "&after=" + doc.name;
var editedOptions = _.clone(self._options);
editedOptions.path = editedOptions.path + afterString;
console.log('total count is ' + count + ', name is ' + doc.name + ', using url: ', editedOptions.path);
self._throttledPullData(editedOptions, self._saveResult, self);
});
} else if (count === 0) {
console.log('first time access with no data, using url: ', self._options.path);
self._throttledPullData(self._options, self._saveResult, self);
}
});
},
_saveResult: function(statusCode, result, self){
var errorCallback = function(err, docs){
if(err) {console.log("\n\nerror saving: ", data, "error: ", err);}
}, data;
for(var i = 0; i < result.data.children.length; i++){
data = result.data.children[i].data;
new self._moodel(data).save(errorCallback);
}
}
};