-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvotebot.js
More file actions
233 lines (204 loc) · 10.1 KB
/
votebot.js
File metadata and controls
233 lines (204 loc) · 10.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
var voteQueue = [];
var progress = 0;
let progress_bar = document.getElementById("progress");
function showProgress() {
var dots = "";
for(let i = 0; i < progress; i++) {
dots = dots + ".";
}
progress_bar.innerHTML = "Работаем" + dots;
progress = progress + 1;
if(progress > 3) {
progress = 0;
}
}
function showStop() {
progress_bar.innerHTML = "Остановлено";
}
function toggleKey() {
let key = document.getElementById("k");
if(key.type == "text") {
key.type = "password";
} else {
key.type = "text";
}
}
function recoverData() {
let raw_users = document.getElementById("voteaccounts");
let key = document.getElementById("k");
let username = document.getElementById("username");
raw_users.value = localStorage.getItem("raw_users");
key.value = localStorage.getItem("key");
username.value = localStorage.getItem("username");
}
function parseAccounts(inp) {
let accs = inp.split(",");
for(let i = 0; i < accs.length; i++) {
accs[i] = accs[i].trim();
}
return accs;
}
function showAccounts(ta) {
var accs = parseAccounts(ta.value);
var view = document.getElementById("accounts_view");
var html = "";
for(var i = 0; i < accs.length; i++) {
html = html + "<br><a href='" + golosbase +"/@" + accs[i] + "'>@"+accs[i]+ "</a>";
}
view.innerHTML = html;
}
var votepower = 0;
var workerTimer;
function startVoting() {
console.log("startBot");
showAccounts(document.getElementById("voteaccounts"));
steem.api.setWebSocket(golos_ws);
var users = parseAccounts(document.getElementById("voteaccounts").value),
k = document.getElementById("k").value,
username = document.getElementById("username").value,
votepower = document.getElementById("votepower").value,
delay = document.getElementById("delay").value,
debug = document.getElementById("debug").value,
time, starttime, acttime, t = 1000,
period = 10 * 60,
utime, start, history,
raw_users = document.getElementById("voteaccounts").value;
function isDebug() {
return debug == "debug";
}
localStorage.setItem("raw_users", raw_users);
localStorage.setItem("key", k);
localStorage.setItem("username", username);
if(typeof users == "undefined" ||users.length == 0 || users.length == 1 && users[0] == "") {
alert("Введите имена пользователей за которыми следить!");
return;
}
if(typeof k != "undefined" && !k.startsWith("5")) {
alert("Возможно вы ввели открытый ключ, тебуется однако приватный (начинается с '5'");
}
//Инициализация кэша
var accounts = {};
for(let i = 0; i < users.length; i++) {
accounts[users[i]] = {lastId:-1, queue: []}; //-1 неизвестно, ждем новых постов
}
console.log(accounts);
var checkDelay = 10000;
var votingDelay = 3000;
steem.api.getDynamicGlobalProperties(function(err, result) {
starttime = Date.parse(result.time) / t;
});
//console.log("время запуска = " + starttime);
progressTimer = setInterval(function() {
showProgress();
}, 1000);
workerTimer = setInterval(function() {
steem.api.getDynamicGlobalProperties(function(err, result) {
acttime = Date.parse(result.time) / t;
});
for(let i = 0; i < users.length; i++) {
let u = users[i];
console.log("получение истории пользователя " +u);
steem.api.getAccountHistory(u, -1, 20, function(err, result) {
if(err != null) {
console.error("Ошибка получения истории ", err);
return;
}
//получили 10 последних записей из истории
//console.log(result);
for(var ai = 0; ai < result.length; ai++) {
let heId = result[ai][0];
//проверить id, больше ли, чем уже прочитанный ранее?
if(isDebug()) {
console.log("сохраненный id = " + accounts[u].lastId + " " + heId );
}
if(accounts[u].lastId < heId) {
let he = result[ai][1].op;
let time = result[ai][1].timestamp;
let utime = Date.parse(time) / t;
let tx = result[ai][1].trx_id;
//console.log(utime);
if(typeof he !== "undefined") {//Отсеять ошибки
let op = he[0];
let entry = he[1];
let nice_name = u + " / " + entry.permlink;
let earliesttime = acttime - (delay*60); //проверить, не созрел ли пост для голосования
if(op == "comment" && entry.author == u && entry.parent_author == "" && !entry.body.match("^@@ .* @@")) {
if(isDebug()) {
console.log("найден пост " + nice_name);
console.log("время запуска = " + starttime);
console.log("время опроса = " + acttime);
console.log("время время созревания = " + earliesttime);
console.log("время создания поста = " + utime);
}
if(utime < earliesttime) {
//console.log(entry);
console.log(heId + ":" + accounts[u].lastId + " добавить в очередь : " + u + " / " + entry.permlink);
//добавляем в очередь для голосования
accounts[u].queue.push({
author : u,
permlink : entry.permlink,
title : entry.title,
tx : tx,
id : heId
});
votehtml = '<div><a href="https://golos.io/@' + u + '/' + entry.permlink + '"><strong>' + u + ": " + entry.title + '</strong></a> <img id="' + tx + '" src="ic_check_box_outline_blank_white_24dp.png"/></div>';
document.getElementById('nicedata').insertAdjacentHTML('afterbegin', votehtml);
checkDelay = checkDelay + votingDelay;
}
}
}
}
}
//недавние голосования
var actualVotes = [];
var goVote = setInterval(function() {
let vote = accounts[u].queue.shift();
if(typeof vote !== "undefined") {
//На всякий случай исключить двойное голосование
let voteKey = vote.author + "/" + vote.permlink;
if(!actualVotes.includes(voteKey)) {
actualVotes.push(voteKey);
//убедиться, может быть раньше голосовали
steem.api.getActiveVotes(vote.author, vote.permlink, function(err, result) {
//console.log(result);
var alreadyVoted = false;
for(let i = 0; i < result.length; i++) {
if(result[i].voter == username) {
alreadyVoted = true;
break;
}
}
if(!alreadyVoted) {
steem.broadcast.vote(k, username, vote.author, vote.permlink, votepower * 100, function(err, result) {
console.log(err,result);
let itemVote = document.getElementById(vote.tx);
if(typeof err == "undefined") {
alreadyVoted = true;
} else {
itemVote.src = "ic_error_white_24dp.png";
}
});
if(checkDelay > 10000) {
checkDelay = checkDelay - votingDelay;
}
}
if(alreadyVoted) {
let itemVote = document.getElementById(vote.tx);
itemVote.src = "ic_check_box_white_24dp.png";
accounts[u].lastId = vote.id;
}
});
}
} else {
clearInterval(goVote);
}
}, votingDelay);
});
}
}, checkDelay);
}
function stopVoting() {
clearInterval(workerTimer);
clearInterval(progressTimer);
showStop();
}