-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathutopian-bot.js
More file actions
463 lines (399 loc) · 26.1 KB
/
Copy pathutopian-bot.js
File metadata and controls
463 lines (399 loc) · 26.1 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import Stats from './server/models/stats.model';
import Post from './server/models/post.model';
import config from './config/config';
import steem from 'steem';
import { calculatePayout } from './server/steemitHelpers';
import SteemConnect from 'sc2-sdk';
import { createCommentPermlink } from './server/steemitHelpers';
const request = require('superagent');
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect(`${config.mongo.host}`);
const conn = mongoose.connection;
conn.once('open', function ()
{
const paidRewardsDate = '1969-12-31T23:59:59';
const botAccount = process.env.BOT;
const refreshToken = process.env.REFRESH_TOKEN;
const secret = process.env.CLIENT_SECRET;
const forced = process.env.FORCED === 'true' || false;
console.log("-----BOT-------", botAccount);
console.log("-----TOKEN-------", refreshToken);
console.log("-----SECRET-------", secret);
const now = new Date();
const limit = 30;
const query = {
reviewed: true,
'active_votes.voter': { $ne: botAccount },
created: {
$lte: new Date(now.getTime() - 1*60*60*1000).toISOString()
},
cashout_time: {
$gt: paidRewardsDate,
},
};
request
.get(`https://v2.steemconnect.com/api/oauth2/token?refresh_token=${refreshToken}&client_secret=${secret}&scope=vote,comment,comment_delete,comment_options,custom_json,claim_reward_balance,offline`)
.end((err, res) => {
if (res.body.access_token) {
SteemConnect.setAccessToken(res.body.access_token);
} else {
conn.close();
process.exit(0);
return;
}
steem.api.getAccounts(['utopian-io'], function(err, accounts){
if (!err) {
const botStatus = accounts[0];
const secondsago = (new Date().getTime() - new Date(botStatus.last_vote_time + "Z").getTime()) / 1000;
const votingPower = botStatus.voting_power + (10000 * secondsago / 432000);
if(votingPower <= 6000 && !forced) {
console.log("UPS I AM SO TIRED TODAY. VOTED TOO MUCH", votingPower);
conn.close();
process.exit(0);
return;
}
Stats.get()
.then(stats => {
const { categories } = stats;
Post
.list({ skip: 0, limit: limit, query, sort: { net_votes: -1 } })
.then(posts => {
if(posts.length > 0) {
posts.forEach((post, allPostsIndex) => {
steem.api.getAccounts([post.author], (err, accounts) => {
if (!err) {
if (accounts && accounts.length === 1) {
const account = accounts[0];
steem.api.getFollowCount(account.name, function(err, followers) {
const contributionsQuery = {
reviewed: true,
id: { $ne: post.id },
author: post.author,
};
Post
.countAll({ query: contributionsQuery })
.then(contributionsCount => {
Post
.list({ skip: 0, limit: contributionsCount, query })
.then(contributions => {
const bots = [
'voter',
'booster',
'sneaky-ninja',
'boomerang',
'lovejuice',
'buildawhale',
'minnowhelper',
'discordia',
'bellyrub',
'minnowbooster',
'randowhale',
'minnowpond',
'resteembot',
'originalworks',
'treeplanter',
'followforupvotes',
'steemthat',
'frontrunner',
'steemvoter',
'morwhale',
];
const reputation = steem.formatter.reputation(account.reputation);
const posting_rewards = account.posting_rewards;
const curation_rewards = account.curation_rewards;
const categoryStats = categories[post.json_metadata.type];
let contributionsTotalVotes = 0;
contributions.forEach(contribution => contributionsTotalVotes = contributionsTotalVotes + contribution.net_votes);
const contributionsTotalVotesAverage = contributionsTotalVotes > 0 && contributionsCount > 0 ? contributionsTotalVotes / contributionsCount : 0;
const bodyLength = post.body.length;
const average_posts_length = Math.round(categoryStats.average_posts_length);
const bodyBiggerThanAverage = bodyLength > average_posts_length;
const bodyLessThanAverage = bodyLength < average_posts_length;
// if it does not meet the average for only 1000 chars, still considering it average
const bodyInAverage = bodyLength === average_posts_length || bodyLength + 1000 >= average_posts_length;
const tags = post.json_metadata.tags.length;
const average_tags_per_post = Math.round(categoryStats.average_tags_per_post);
const tagsMoreThanAverage = tags > average_tags_per_post;
const tagsLessThanAverage = tags < average_tags_per_post;
// if it does not meet the average for only 1 tag, still considering it average
const tagsInAverage = tags === average_tags_per_post || tags + 1 === average_tags_per_post;
const images = post.json_metadata.image ? post.json_metadata.image.length : (post.body.match(/<img/g) || []).length;
const average_images_per_post = Math.round(categoryStats.average_images_per_post);
const imagesMoreThanAverage = images > average_images_per_post;
const imagesLessThanAverage = images < average_images_per_post;
// if it does not meet the average for only 1 image, still considering it average
const imagesInAverage = images === average_images_per_post || images + 1 === average_images_per_post;
const links = post.json_metadata.links ? post.json_metadata.links.length : 0;
const average_links_per_post = Math.round(categoryStats.average_links_per_post);
const linksMoreThanAverage = links > average_links_per_post;
const linksLessThanAverage = links < average_links_per_post;
// if it does not meet the average for only 1 link, still considering it average
const linksInAverage = links === average_links_per_post || links + 1 === average_links_per_post;
const average_likes_per_post = Math.round(categoryStats.average_likes_per_post);
const votesInAverage = post.net_votes === average_likes_per_post || post.net_votes + 5 >= average_likes_per_post;
const votesMoreThanAverage = post.net_votes > average_likes_per_post;
const payoutDetails = calculatePayout(post);
const totalGenerating = categoryStats.average_paid_authors + categoryStats.average_paid_curators;
const suggestions = [];
const achievements = [];
let vote = 0;
// we love devs
if (post.json_metadata.type === 'development') {
vote = vote + 30;
achievements.push('I am a bot...I love developers... <3');
}
// POSITIVE VOTES
if (tagsInAverage || tagsMoreThanAverage) vote++;
if(bodyInAverage) {
vote = vote + 10;
achievements.push('Good amount of information. Thank you!');
};
if (bodyBiggerThanAverage) {
// length of the body bigger than average in its category. The contribution is informative
if (bodyLength - 1500 > average_posts_length) {
vote = vote + 5;
}
if (bodyLength - 3000 > average_posts_length) {
vote = vote + 5;
}
if (bodyLength - 5000 > average_posts_length) {
vote = vote + 5;
}
achievements.push('A very informative contribution. Good job!');
};
if (votesInAverage) {
vote = vote + 10;
achievements.push('Votes on this contribution are going well. Nice!');
}
if (votesMoreThanAverage) {
// the contribution is having more than average votes
vote = vote + 5;
if (post.net_votes - 5 > average_likes_per_post) {
vote = vote + 5;
}
if (post.net_votes - 15 > average_likes_per_post) {
vote = vote + 5;
}
if (post.net_votes - 30 > average_likes_per_post) {
vote = vote + 5;
}
achievements.push('You are having more votes than average for this category. Nice!');
};
if (post.net_votes > followers.follower_count && follower_count > 0) {
// giving a tip for account swith small followers or 0
vote = vote + 10;
achievements.push('You just got more votes than your total number of followers. Rock Star!');
}
if (post.net_votes > followers.follower_count / 4 && followers.follower_count > 100) {
// the post is voted by many considering the total number of followers
vote = vote + 5;
if (post.net_votes > followers.follower_count / 3 && followers.follower_count > 100) {
vote = vote + 5;
}
if (post.net_votes > followers.follower_count / 2 && followers.follower_count > 100) {
vote = vote + 5;
}
achievements.push('This contribution is performing very well based on the number of your followers. Kudos!');
}
if (payoutDetails.potentialPayout > totalGenerating) {
// the contribution is generating big payouts
vote = vote + 20;
if (payoutDetails.potentialPayout - 10 > totalGenerating) {
vote = vote + 5;
}
if (payoutDetails.potentialPayout - 25 > totalGenerating) {
vote = vote + 5;
}
if (payoutDetails.potentialPayout - 50 > totalGenerating) {
vote = vote + 10;
}
if (payoutDetails.potentialPayout - 100 > totalGenerating) {
vote = vote + 20;
}
achievements.push('You are generating more rewards than average for this category. Super!');
}
// reputation of the user
if(reputation > 25) vote++;
if(reputation >= 35) vote++;
if(reputation >= 50) vote++;
if(reputation >= 70) vote++;
if(reputation >= 80) vote++;
// rewards for posting
if (posting_rewards > 10000) vote++;
if (posting_rewards > 500000) vote++;
if (posting_rewards > 1000000) vote++;
if (posting_rewards > 10000000) vote++;
if (posting_rewards > 100000000) vote++;
// rewards for curating. More important than posting
if (curation_rewards > 10000) vote++;
if (curation_rewards > 50000) vote++;
if (curation_rewards > 100000) vote++;
if (curation_rewards > 1000000) vote++;
if (curation_rewards > 10000000) vote++;
if (contributionsCount === 0) {
// this is the first contribution of the user accepted in the Utopian feed
// give the user a little gift
vote = vote + 15;
achievements.push('This is your first accepted contribution here in Utopian. Welcome!');
}
// number of contributions in total
if (contributionsCount >= 3) {
// git for being productive
vote = vote + 10;
if (contributionsCount >= 10) {
// git for being productive
vote = vote + 5;
}
if (contributionsCount >= 30) {
// git for being productive
vote = vote + 15;
}
if (contributionsCount >= 50) {
// git for being productive
vote = vote + 20;
}
achievements.push('Seems like you contribute quite often. AMAZING!');
}
// average of votes this user has on all his contributions
if (contributionsTotalVotesAverage > 15) {
vote = vote + 15;
if (contributionsTotalVotesAverage > 35) {
vote = vote + 5;
}
if (contributionsTotalVotesAverage > 50) {
vote = vote + 10;
}
if (contributionsTotalVotesAverage > 100) {
vote = vote + 30;
}
achievements.push('You have a good amount of votes on your contributions. Good job!');
}
if (contributionsTotalVotesAverage > categoryStats.average_likes_per_post) {
// the user has more votes than average on his contributions in total
vote = vote + 20;
achievements.push('In total you have more votes than average for this category. Bravo!');
}
if (achievements.length >= 5) {
// WOW a lot of achievements. Better to give a gift
vote = vote + 20;
achievements.push('You have just unlocked 5 achievements. Yeah!');
}
// NEGATIVE VOTES
if(reputation < 25) vote--;
if(reputation < 15) vote--;
if(reputation < 5) vote--;
if(reputation < 0) vote--;
if (tagsLessThanAverage) vote--;
if (linksLessThanAverage) vote--;
if (bodyLessThanAverage) {
if(average_posts_length - bodyLength > 1000) {
vote--;
}
if(average_posts_length - bodyLength > 3000) {
vote = vote - 5;
}
if(average_posts_length - bodyLength > 7000) {
vote = vote - 5;
}
if(average_posts_length - bodyLength > 10000) {
vote = vote - 5;
}
suggestions.push('Your contribution is less informative than others in this category.');
}
let foundBots = 0;
post.active_votes.forEach((voted, index) => {
if (bots.indexOf(voted.voter) > -1) {
vote = vote - 2.5;
foundBots++;
}
});
if (foundBots > 0) suggestions.push('Utopian has detected ' + foundBots + ' bot votes. I am the only bot you should love!!');
vote = Math.round(vote);
if(vote <= 0) vote = 1;
if(vote > 100) vote = 100;
let commentBody = '';
if (vote > 0) {
commentBody = `### Hey @${post.author} I am @${botAccount}. I have just super-voted you at ${vote}% Power!\n`;
} else {
commentBody = `### Hey @${post.author} I am @${botAccount}. So sad I couldn't super-vote this time!\n`;
}
if (suggestions.length > 0) {
commentBody += '#### Suggestions https://utopian.io/rules\n';
suggestions.forEach(suggestion => commentBody += `-${suggestion}\n`);
}
if (achievements.length > 0) {
commentBody += '#### Achievements\n';
achievements.forEach(achievement => commentBody += `-${achievement}\n`);
}
commentBody += '**Up-vote this comment to grow my power and help Open Source contributions like this one.**';
setTimeout(function() {
console.log("-----VOTING AUTHOR-------", post.author);
console.log("VOTING PERMLINK", post.permlink);
console.log("VOTING VOTE", vote);
achievements.forEach(achievement => console.log(achievement));
suggestions.forEach(suggestion => console.log(suggestion));
const jsonMetadata = { tags: ['utopian-io'], community: 'utopian', app: `utopian/1.0.0` };
const comment = () => {
SteemConnect.comment(
post.author,
post.permlink,
botAccount,
createCommentPermlink(post.author, post.permlink),
'',
commentBody,
jsonMetadata,
).then(() => {
console.log("COMMENT SUBMITTED FROM THEN");
if (allPostsIndex + 1 === posts.length) {
conn.close();
process.exit(0);
}
}).catch(e => {
if (e.error_description == undefined) {
console.log("COMMENT SUBMITTED FROM THEN");
} else {
console.log("COMMENT ERROR", e);
}
});
};
SteemConnect.vote(botAccount, post.author, post.permlink, vote * 100)
.then(() => {
console.log("NOW SUBMITTING COMMENT FROM THEN");
comment();
}).catch(e => {
// I think there is a problem with sdk. Always gets in the catch
if (e.error_description == undefined) {
stats.utopian_votes = [
...stats.utopian_votes,
{
date: new Date().toISOString(),
weight: vote * 100,
permlink: post.permlink,
author: post.author,
}
];
stats.save();
console.log("NOW SUBMITTING COMMENT FROM CATCH");
comment();
}
});
}, allPostsIndex === 0 || 30000 * allPostsIndex);
});
});
});
}
}
});
});
} else {
console.log("NO POSTS");
conn.close();
process.exit(0);
}
});
});
}
});
});
});