-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.js
326 lines (239 loc) · 9.03 KB
/
populate.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#! /usr/bin/env node
console.log('This script is running!!!!');
var async = require('async')
var Actor = require('./models/Actor.js');
var Script = require('./models/Script.js');
const _ = require('lodash');
const dotenv = require('dotenv');
var mongoose = require('mongoose');
var fs = require('fs')
var highUsers = require('./highusers.json');
var actors1 = require('./actorsv1.json');
var posts1 = require('./postsv1.json');
var post_reply1 = require('./post_replyv1.json');
var actorReply = require('./actorReply.json');
var notify = require('./notify.json');
var dd = require('./upload_post_replyv1.json');
dotenv.load({ path: '.env' });
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
//var connection = mongo.connect('mongodb://127.0.0.1/test');
mongoose.connect(process.env.PRO_MONGODB_URI || process.env.PRO_MONGOLAB_URI);
var db = mongoose.connection;
mongoose.connection.on('error', (err) => {
console.error(err);
console.log('%s MongoDB connection error. Please make sure MongoDB is running.', chalk.red('✗'));
process.exit();
});
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
function timeStringToNum (v) {
var timeParts = v.split(":");
return parseInt(((timeParts[0] * (60000 * 60)) + (timeParts[1] * 60000)), 10);
}
function getLikes() {
var notRandomNumbers = [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6];
var idx = Math.floor(Math.random() * notRandomNumbers.length);
return notRandomNumbers[idx];
}
function getReads(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
function highActorCreate(random_actor) {
actordetail = {};
actordetail.profile = {};
actordetail.profile.name = random_actor.name.first.capitalize() +' '+random_actor.name.last.capitalize();
actordetail.profile.gender = random_actor.gender;
actordetail.profile.location = random_actor.location.city.capitalize() +', '+random_actor.location.state.capitalize();
actordetail.profile.picture = random_actor.picture.large;
actordetail.class = 'high_read';
actordetail.username = random_actor.login.username;
var actor = new Actor(actordetail);
actor.save(function (err) {
if (err) {
console.log("Something went wrong!!!")
return -1;
}
console.log('New high Actor: ' + actor.username);
});
}
function ActorCreate(actor1) {
actordetail = {};
actordetail.profile = {};
actordetail.profile.name = actor1.name
actordetail.profile.gender = actor1.gender;
actordetail.profile.location = actor1.location;
actordetail.profile.picture = actor1.picture;
actordetail.profile.bio = actor1.bio;
actordetail.profile.age = actor1.age;
actordetail.class = actor1.class;
actordetail.username = actor1.username;
var actor = new Actor(actordetail);
actor.save(function (err) {
if (err) {
console.log("Something went wrong!!!")
return -1;
}
console.log('New Actor: ' + actor.username);
});
}
function PostCreate(new_post) {
Actor.findOne({ username: new_post.actor}, (err, act) => {
if (err) { console.log(err); return next(err); }
console.log('Looking up Actor ID is : ' + act._id);
var postdetail = new Object();
postdetail.actor = {};
postdetail.body = new_post.body
postdetail.post_id = new_post.id;
postdetail.class = new_post.class;
postdetail.picture = new_post.picture;
postdetail.likes = getLikes();
postdetail.lowread = getReads(6,20);
postdetail.highread = getReads(145,203);
postdetail.actor.$oid = act._id.toString();
//postdetail.actor=`${act._id}`;
//postdetail.actor2=act;
postdetail.time = timeStringToNum(new_post.time);
console.log('Looking up Actor: ' + act.username);
//console.log(mongoose.Types.ObjectId.isValid(postdetail.actor.$oid));
//console.log(postdetail);
fs.appendFileSync('upload_postsv1.json', JSON.stringify(postdetail));
});
};
function PostReplyCreateFinal(new_post){
Script.findOne({ post_id: new_post.replyID}, function(err, pr){
if(err) {
console.log(err);
return
}
console.log('In SCRIPT');
console.log('In Reply: ' + pr._id);
//console.log('Looking up Actor ID is : ' + act._id);
var postdetail = new Object();
postdetail.actor = {};
postdetail.reply = {};
postdetail.body = new_post.body
postdetail.post_id = new_post.id;
postdetail.class = new_post.class;
postdetail.picture = new_post.picture;
postdetail.likes = new_post.likes;
postdetail.lowread = new_post.lowread;
postdetail.highread = new_post.highread;
postdetail.actor.$oid = new_post.actor.$oid;
postdetail.reply.$oid = pr._id.toString();
postdetail.time = new_post.time;
fs.appendFileSync('upload_post_replyv2.json', JSON.stringify(postdetail));
});
}
function PostReplyCreate(new_post) {
Actor.findOne({ username: new_post.actor}, function(err, act){
if(err) {
console.log(err);
return
}
console.log('Looking up Actor: ' + act.username);
console.log('Try for post: ' + new_post.reply);
var postdetail = new Object();
postdetail.actor = {};
postdetail.replyID = new_post.reply;
postdetail.body = new_post.body
postdetail.post_id = 300 + new_post.id;
postdetail.class = new_post.class;
postdetail.picture = new_post.picture;
postdetail.likes = getLikes();
postdetail.lowread = getReads(6,20);
postdetail.highread = getReads(145,203);
postdetail.actor.$oid = act._id.toString();
//postdetail.reply.$oid = pr._id.toString();
console.log('Time is now: ' + new_post.time);
postdetail.time = timeStringToNum(new_post.time);
fs.appendFileSync('upload_post_replyv0.json', JSON.stringify(postdetail));
});
};
function NotifyCreate(new_notify) {
Actor.findOne({ username: new_notify.actor}, (err, act) => {
if (err) { console.log(err); return next(err); }
console.log('Looking up Actor ID is : ' + act._id);
var notifydetail = new Object();
if (new_notify.userPost >= 0 && !(new_notify.userPost === ""))
{
notifydetail.userPost = new_notify.userPost;
console.log('User Post is : ' + notifydetail.userPost);
}
else if (new_notify.userReply >= 0 && !(new_notify.userReply === ""))
{
notifydetail.userReply = new_notify.userReply;
console.log('User Reply is : ' + notifydetail.userReply);
}
else if (new_notify.actorReply >= 0 && !(new_notify.actorReply === ""))
{
notifydetail.actorReply = new_notify.actorReply;
console.log('Actor Reply is : ' + notifydetail.actorReply);
}
notifydetail.actor = {};
notifydetail.notificationType = new_notify.type;
notifydetail.actor.$oid = act._id.toString();
notifydetail.time = timeStringToNum(new_notify.time);
//console.log('Looking up Actor: ' + act.username);
//console.log(mongoose.Types.ObjectId.isValid(notifydetail.actor.$oid));
//console.log(notifydetail);
fs.appendFileSync('upload_replyv2.json', JSON.stringify(notifydetail));
});
};
function actorNotifyCreate(new_notify) {
Actor.findOne({ username: new_notify.actor}, (err, act) => {
if (err) { console.log(err); return next(err); }
console.log('Looking up Actor ID is : ' + act._id);
var notifydetail = new Object();
notifydetail.userPost = new_notify.userPostId;
notifydetail.actor = {};
notifydetail.notificationType = 'reply';
notifydetail.replyBody = new_notify.body;
notifydetail.actor.$oid = act._id.toString();
notifydetail.time = timeStringToNum(new_notify.time);
//console.log('Looking up Actor: ' + act.username);
//console.log(mongoose.Types.ObjectId.isValid(notifydetail.actor.$oid));
//console.log(notifydetail);
fs.appendFileSync('upload_actorReplyV2.json', JSON.stringify(notifydetail));
});
};
/*
for (var i = 0, len = actors1.length; i < len; i++) {
console.log("@@@Looking at "+actors1[i].username);
ActorCreate(actors1[i]);
}
/*
for (var i = 0, len = highUsers.results.length; i < len; i++) {
highActorCreate(highUsers.results[i]);
}
for (var i = 0, len = notify.length; i < len; i++) {
NotifyCreate(notify[i]);
}
for (var i = 0, len = posts1.length; i < len; i++) {
PostCreate(posts1[i]);
}
for (var i = 0, len = post_reply1.length; i < len; i++) {
PostReplyCreate(post_reply1[i]);
}
for (var i = 0, len = dd.length; i < len; i++) {
PostReplyCreateFinal(dd[i]);
}
for (var i = 0, len = actorReply.length; i < len; i++) {
actorNotifyCreate(actorReply[i]);
}
for (var i = 0, len = actorReply.length; i < len; i++) {
actorNotifyCreate(actorReply[i]);
}
*/
for (var i = 0, len = dd.length; i < len; i++) {
PostReplyCreateFinal(dd[i]);
}
//PostReplyCreate(posts1[0]);
//PostCreate(posts1[1]);
//actorNotifyCreate(actorReply[i]);
console.log('After Lookup:');
//All done, disconnect from database
mongoose.connection.close();