-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave-data.js
More file actions
389 lines (353 loc) · 10.8 KB
/
save-data.js
File metadata and controls
389 lines (353 loc) · 10.8 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var utils = require('./utils');
var mail = require('./mail');
const steem = require('steem');
const { forEach } = require('p-iteration');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
var config = utils.getConfig();
// Connection URL
const url = config.mongo_uri;
var postsProcessing = false;
var db;
var collection;
// Database Name
const db_name = config.db_name;
const collection_name = 'posts';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
if(!err) {
assert.equal(null, err);
console.log("Connected successfully to server");
db = client.db(db_name);
// Get the documents collection
collection = db.collection(collection_name);
updateUserTokens();
runPostsProcess();
//run every 31 mins
setInterval(runPostsProcess, 31 * 60 * 1000);
//run every 40 mins
setInterval(updateUserTokens, 41 * 60 * 1000);
} else {
utils.log(err, 'import');
/*mail.sendPlainMail('Database Error', err, '')
.then(function(res, err) {
if (!err) {
console.log(res);
} else {
utils.log(err, 'import');
}
});
process.exit();*/
}
});
function runPostsProcess(){
if(postsProcessing){
return;
}
postsProcessing = true;
getPosts();
}
async function getPosts(index) {
console.log('>>>>>>>> attempt getPosts <<<<<<<<<<<');
console.log('---- Getting Posts ----');
var query = {tag: config.main_tag, limit: 100};
if (index) {
console.log('--> More posts <--');
query.start_author = index.start_author;
query.start_permlink = index.start_permlink;
}
//grab banned user list before rewarding
var banned_users = await db.collection('banned_accounts').find({ban_status:"active"}).toArray();
console.log('found banned users');
steem.api.getDiscussionsByCreated(query, function (err, result) {
if (result && !err) {
if(result.length == 0 || !result[0]) {
utils.log('No posts found for this tag: ' + config.main_tag, 'import');
postsProcessing = false;
return;
}
console.log('Post count: ' + result.length);
let posts = utils.filterPosts(result, banned_users, config.account, config.main_tag);
//if the result was not an array, bail out
if (posts == -1){
console.log('done looking for posts');
postsProcessing = false;
return;
}
console.log('Filtered count: ' + posts.length);
// Upsert posts
var bulk = collection.initializeUnorderedBulkOp();
console.log('check post');
var step_count = -1;
for(var i = 0; i < posts.length; i++) {
let post = posts[i]
try {
post.json_metadata = JSON.parse(post.json_metadata);
step_count = post.json_metadata.step_count;
if (step_count < 5000)
continue;
else if (step_count < 6000)
post.token_rewards = 20;
else if(step_count < 7000)
post.token_rewards = 35;
else if(step_count < 8000)
post.token_rewards = 50;
else if(step_count < 9000)
post.token_rewards = 65;
else if(step_count < 10000)
post.token_rewards = 80;
else if(step_count > 150000)
continue;
else
post.token_rewards = 100;
} catch (err) {
utils.log('Error parsing json metadata');
console.log(err);
continue;
}
bulk.find( { permlink: post.permlink } ).upsert().replaceOne(
post
);
}
//do not attempt insertion if no results found on this round
if (posts.length == 0){
let last_post = result[result.length - 1];
if (!index || (index.start_permlink != last_post.permlink && index.start_author != last_post.author && result.length >= 100)){
return getPosts({start_author: last_post.author, start_permlink: last_post.permlink});
}
}else{
bulk.execute()
.then(async function (res) {
var mes = res.nInserted + ' posts inserted - ' + res.nUpserted + ' posts upserted - ' + res.nModified + ' posts updated';
utils.log(mes, 'import');
let last_post = posts[posts.length - 1];
await processTransactions(posts);
console.log('Inserted transactions');
if (!index || (index.start_permlink != last_post.permlink && index.start_author != last_post.author && result.length >= 100))
return getPosts({start_author: last_post.author, start_permlink: last_post.permlink});
console.log('No more new posts');
return;
})
.catch(function (err) {
utils.log(err, 'import');
/*mail.sendPlainMail('Error en mongo upsert', err, config.report_emails)
.then(function(res, err) {
if (!err) {
console.log(res);
return;
} else {
console.log(err);
return;
}
});*/
}).finally(function() {
//making sure we don't get caught up in infinite loop after some error
postsProcessing = false;
});
}
} else {
utils.log(err, 'import');
/*mail.sendPlainMail('0 posts...', err, config.report_emails)
.then(function(res, err) {
if (!err) {
console.log(res);
return;
} else {
console.log(err);
return;
}
});*/
}
});
}
async function processTransactions(posts) {
console.log('---- Updating Transactions ----');
let transactions = [];
let collection = db.collection('token_transactions');
var bulk = collection.initializeUnorderedBulkOp();
await forEach(posts, async (post) => {
//by default the reward owner is the author
var reward_user = post.author;
var activity_type = 'Post';
var note = '';
//if we find this is a charity run, let's switch it to the actual charity name
if (typeof post.json_metadata.charity != 'undefined' && post.json_metadata.charity != '' && post.json_metadata.charity != 'undefined'){
reward_user = post.json_metadata.charity;
activity_type = 'Charity Post';
note = 'Charity donation via activity by user '+post.author;
}
let post_transaction = {
user: reward_user,
reward_activity: activity_type,
token_count: post.token_rewards,
url: post.url,
date: post.created,
note: note
}
bulk.find(
{
user: post_transaction.user,
reward_activity: post_transaction.reward_activity,
url: post_transaction.url
})
.upsert().replaceOne(post_transaction);
transactions.push(post_transaction);
post.active_votes.forEach(async vote => {
//skip self vote from rewards
if (post.author != vote.voter){
let vote_transaction = {
user: vote.voter,
reward_activity: 'Post Vote',
token_count: 1,
url: post.url,
date: vote.time
}
bulk.find(
{
user: vote_transaction.user,
reward_activity: vote_transaction.reward_activity,
url: vote_transaction.url
})
.upsert().replaceOne(vote_transaction);
transactions.push(vote_transaction);
}
});
let reblogs = await steem.api.getRebloggedByAsync(post.author, post.permlink);
console.log('------------------ REBLOGS --------------------');
console.log(reblogs);
reblogs.forEach(async reblog => {
if(reblog != post.author){
let reblog_transaction = {
user: reblog,
reward_activity: 'Post Reblog',
token_count: 1,
url: post.url,
date: post.created
}
console.log('---Reblog transaction ----');
console.log(reblog_transaction);
bulk.find(
{
user: reblog_transaction.user,
reward_activity: reblog_transaction.reward_activity,
url: reblog_transaction.url
})
.upsert().replaceOne(reblog_transaction);
transactions.push(reblog_transaction);
}
});
});
// console.log(transactions);
return bulk.execute();
}
async function updateUserTokens() {
console.log('---- Updating Users ----');
try{
let query = await db.collection('token_transactions').aggregate([
{ $group: { _id: "$user", tokens: { $sum: "$token_count" } } },
{ $sort: { tokens: -1 } },
{ $project: {
_id: "$_id",
user: "$_id",
tokens: "$tokens",
}
}
])
let user_tokens = await query.toArray();
await db.collection('user_tokens').remove({});
return await db.collection('user_tokens').insert(user_tokens);
}catch(err){
console.log('>>save data error:'+err.message);
}
}
async function processVotedPosts() {
let transactions = await getAccountVotes(config.account);
let postsData = [];
await forEach(transactions, async (txs) => {
await steem.api.getContentAsync(txs.author, txs.permlink)
.then(postObject => {
if (utils.checkBeneficiary(postObject) || postObject.author == config.account) {
console.log('--- Post has correct beneficiaries or was posted by config account -----');
postsData.push(postObject);
} else {
console.log('--- Post missing beneficiaries -----');
console.log(postObject.url);
}
})
.catch(error => {console.log(error)});
});
upsertPosts(postsData);
return postsData;
}
async function getAccountVotes(account) {
let voteByAccount = []
await steem.api.getAccountHistoryAsync(account, -1, 10000)
.filter( tx => tx[1].op[0] === 'vote' && tx[1].op[1].voter == account)
.each((transaction) => {
voteByAccount.push(transaction[1].op[1])
console.log(transaction[1].op[1])
})
console.log(voteByAccount.length);
return voteByAccount;
}
async function getReblogs(post) {
console.log('----- Getting reblogs ----');
let res;
res = await steem.api.getRebloggedByAsync(post.author, post.permlink);
console.log(res);
return res;
}
async function upsertPosts(posts) {
// Upsert posts
var bulk = collection.initializeUnorderedBulkOp();
for(var i = 0; i < posts.length; i++) {
let post = posts[i]
var step_count = -1;
try {
post.json_metadata = JSON.parse(post.json_metadata);
step_count = post.json_metadata.step_count;
if (step_count < 5000)
continue;
else if (step_count < 6000)
post.token_rewards = 20;
else if(step_count < 7000)
post.token_rewards = 35;
else if(step_count < 8000)
post.token_rewards = 50;
else if(step_count < 9000)
post.token_rewards = 65;
else if(step_count < 10000)
post.token_rewards = 80;
else
post.token_rewards = 100;
} catch (err) {
utils.log('Error parsing json metadata');
console.log(err);
continue;
}
bulk.find( { permlink: post.permlink } ).upsert().replaceOne(
post
);
}
return bulk.execute()
.then(async function (res) {
var mes = res.nInserted + ' posts inserted - ' + res.nUpserted + ' posts upserted - ' + res.nModified + ' posts updated';
utils.log(mes, 'import');
await processTransactions(posts);
console.log('----Super upsert ready ----');
return;
})
.catch(function (err) {
utils.log(err, 'import');
mail.sendPlainMail('Error en mongo upsert', err, config.report_emails)
.then(function(res, err) {
if (!err) {
console.log(res);
return;
} else {
console.log(err);
return;
}
});
})
}