-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
389 lines (287 loc) · 12.1 KB
/
index.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
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
'use strict';
const fs = require('fs');
const util = require('util');
const dateFormat = require('dateformat');
const fsReadFile = util.promisify(fs.readFile);
const sleep = util.promisify(setTimeout);
const ACs = require('./classes/ApiCallers');
const LogStream = require('./classes/LogStream');
const LoadQueue = require('./classes/LoadQueue');
module.exports = class VkAlbPoster {
constructor({ group_id, tokens, options }) {
this.storage = {
acs: new ACs(tokens),
group_id,
options
}
}
static delaysTable = [ // delay | uploaded pictures before token ban
[15, 2000],
[45, 2975],
[65, 5000]
];
async post({ albums: newAlbums }) {
const acs = this.storage.acs;
const acsQuantity = Object.keys(acs.list).length;
const _options = this.storage.options;
const albums = {
new: newAlbums
}
const preInitAsyncs = [
// existing albums of the group
VkAlbPoster.getAlbums(acs.next().apiCaller, '-' + this.storage.group_id)
]
for (const albumParams of albums.new) {
preInitAsyncs.push(VkAlbPoster.parseInput(albumParams.descrsPath))
}
const preInitAsyncResults = await Promise.allSettled(preInitAsyncs);
const preInitBads = preInitAsyncResults.filter(result => {
return result.status === "rejected" ? true : false;
});
for (const result of preInitBads) {
console.error(result.reason);
}
if (preInitBads.length) {
throw new Error('Загрузка альбомов отменена. Исправьте все перечисленные выше ошибки');
}
albums.existing = preInitAsyncResults[0].value;
for (let i = 1; i < preInitAsyncResults.length; i++) {
const result = preInitAsyncResults[i];
albums.new[i - 1].parsedDescrs = result.value;
}
const saveDelay = VkAlbPoster.calcSaveDelay(
albums.new, _options.customDelays, acsQuantity
);
VkAlbPoster.log('info', `Запуск с параметрами: \nЗадержка: ${saveDelay / 1000} секунд; Группа: vk.com/club${this.storage.group_id}`);
// main cycle
for (const albumParams of albums.new) {
let jsonFirstPicWritten = false;
VkAlbPoster.log('info', `Подготовка к загрузке альбома "${albumParams.name}"...`);
// if the group already has an album with the given name
if (albums.existing[albumParams.name]) {
albumParams.id = albums.existing[albumParams.name].id;
}
if (!albumParams.id) {
const ac = acs.next();
let newAlbumObj;
try {
newAlbumObj = await VkAlbPoster.createAlbum(ac.apiCaller, {
title: albumParams.name,
group_id: this.storage.group_id
});
} catch (e) {
const msg = `Не удалось создать альбом "${albumParams.name}" в группе vk.com/club${this.storage.group_id}\nТекущий токен: ${ac.token}`;
VkAlbPoster.log('error', msg);
throw e;
}
albumParams.id = newAlbumObj.id;
}
const logFStream = (() => {
try {
const logFStream = new LogStream(albumParams.logFilePath);
logFStream.write(
`{\n "groupId": ${this.storage.group_id},\n "albumId": ${albumParams.id},\n "savedPics": [`
);
return logFStream;
} catch(e) {
VkAlbPoster.log('error', `Ошибка при создании лог-файла "${logFileFullPath}"`);
throw e;
}
})();
for (const apiCallerToken in acs.list) {
const apiCallerParams = acs.list[apiCallerToken];
const apiCaller = apiCallerParams.apiCaller;
const moduleObj = apiCaller.vkAlbPoster = {};
const albumObj = moduleObj[Number(albumParams.id)] = {};
albumObj.uploadUrl = await VkAlbPoster.getUploadServer(
apiCaller, this.storage.group_id, albumParams.id
);
}
VkAlbPoster.log('info', `Подготовка к загрузке альбома "${albumParams.name}" успешна. Ссылка: vk.com/album-${this.storage.group_id}_${albumParams.id}. Загрузка...`);
const loadQueue = new LoadQueue(albumParams.imgsPath, albumParams.id);
const executeChunkSize = 25;
let loadData = (() => {
const shift = acsQuantity * executeChunkSize * 2;
const output = {
loaders: acs,
items: VkAlbPoster.makeLoadData(shift, executeChunkSize, albumParams)
};
albumParams.parsedDescrs = albumParams.parsedDescrs.slice(shift);
return output;
})();
let loadDataLog = [];
let loadResults = await loadQueue.load(loadData);
while (loadResults.length) {
const saveResults = [];
const loadResultsPart = loadResults.slice(0, acsQuantity);
for (let i = 0; i < loadResultsPart.length; i++) {
const loadResult = loadResults[i];
const lastSaveDiff = Date.now() - loadResult.loader.lastSaveCallTime;
const delay = lastSaveDiff > saveDelay ? 0 : saveDelay - lastSaveDiff;
if (delay) { await sleep(delay) }
const output = await VkAlbPoster.savePhotos(
loadResult.loader.apiCaller, loadResult.responses
);
loadResult.loader.lastSaveCallTime = Date.now();
saveResults.push(output);
}
loadDataLog = loadDataLog.concat([].concat.apply([], loadData.items));
for (let i = 0; i < saveResults.length; i++) {
const saveResponses = saveResults[i].response;
const saveErrors = saveResults[i].errors;
if (saveErrors && saveErrors.length) {
logFStream.write(`\n ]\n}`);
logFStream.end();
const errorTypes = {};
saveErrors.map(errorObj => {
if (!errorTypes.hasOwnProperty(errorObj.code)) {
errorTypes[errorObj.code] = { message: errorObj.message, counter: 0 };
}
errorTypes[errorObj.code].counter++;
});
let errorsEnumStr = '';
for (const errorsCode in errorTypes) {
const errorObj = errorTypes[errorsCode];
errorsEnumStr += `\nТекст: ${errorObj.message}; количество ошибок: ${errorObj.counter}`;
}
throw new Error(`${saveErrors.length > 1 ? 'Ошибки' : 'Ошибка'} при сохранении картинок в альбоме:${errorsEnumStr}\nТекущий токен: ${loadResultsPart[i].loader.token}`);
}
for (let x = 0; x < saveResponses.length; x++) {
const inputItem = loadDataLog[x];
const savedItem = saveResponses[x];
const picFilename = JSON.stringify(inputItem.filename);
const picCaption = JSON.stringify(inputItem.caption);
logFStream.write(
`${jsonFirstPicWritten ? ',\n' : '\n'} {\n "filename": ${picFilename},\n "vkId": ${savedItem.id}${picCaption ? ',\n "caption": ' + picCaption : ''}\n }`
);
jsonFirstPicWritten = true;
}
loadDataLog = loadDataLog.slice(saveResponses.length);
}
loadData.items = VkAlbPoster.makeLoadData(executeChunkSize, executeChunkSize, albumParams);
albumParams.parsedDescrs = albumParams.parsedDescrs.slice(executeChunkSize);
loadResults = loadResults
.slice(acsQuantity)
.concat(await loadQueue.load(loadData));
}
logFStream.write(`\n ]\n}`);
logFStream.end();
VkAlbPoster.log('info', `Альбом "${albumParams.name}" загружен`);
}
return 'VkAlbPoster закончил работу';
}
static consoleName = 'vk-groups-albums-poster'; // used in console logging
static log(lvl, msg) {
const prefix = `${dateFormat(Date.now(), 'HH:MM:ss')} ${VkAlbPoster.consoleName}:`;
switch (lvl) {
case 'info':
console.log(prefix, msg);
break;
case 'error':
console.error(prefix, msg);
break;
}
}
static async parseInput(descrsPath) {
const data = await fsReadFile(descrsPath);
const descriptions = JSON.parse(data.toString()).descriptions;
if (!descriptions.length) {
throw new Error(`В "${descrsPath}" не предоставлены названия файлов для загрузки`);
}
const badDescrObjects = {
unpaired: {}
}
for (let i = 1; i <= descriptions.length; i++) {
const descrObj = descriptions[i - 1];
if (!descrObj.filename && descrObj.caption) {
badDescrObjects.unpaired[i] = descrObj;
}
}
if (Object.keys(badDescrObjects.unpaired).length) {
let msg = `В файле описаний по пути "${descrsPath}" обнаружены объекты с указанием подписи к фото, но без указания имени файла. Вы что-то пропустили? Удалите или исправьте объекты перечисленные ниже. № - номер в массиве\n`;
for (const objId in badDescrObjects.unpaired) {
const obj = badDescrObjects.unpaired[objId];
msg = `${msg} №${objId}: ${JSON.stringify(obj)}\n`;
}
throw new Error(msg);
}
return descriptions;
}
static async getAlbums(vk, owner_id) {
const output = {};
const response = await vk.api.photos.getAlbums({ owner_id });
response.items.forEach((item) => { output[item.title] = item });
return output;
}
static async getUploadServer(vk, group_id, album_id) {
const response = await vk.api.photos.getUploadServer({ group_id, album_id });
if (!response || !response.upload_url) {
throw new Error('Failed to get upload url');
}
return response.upload_url;
}
static async createAlbum(vk, args) {
const response = await vk.api.photos.createAlbum(args);
if (response.id) return response;
throw new Error('Failed to create new album');
}
static calcSaveDelay(newAlbums, userDelays, acsQuantity) {
const extraDelay = Number(userDelays.extra) * 1000 || 0;
if (userDelays.hasOwnProperty('constant')) {
return Number(userDelays.constant) * 1000 + extraDelay;
}
let picsOverallCount = 0;
let output = 0;
for (const album of newAlbums) {
picsOverallCount += album.parsedDescrs.length;
}
for (const row of VkAlbPoster.delaysTable) {
const recommendedDelay = row[0];
const picsLimit = row[1] * acsQuantity;
if (picsLimit >= picsOverallCount) {
output = recommendedDelay;
break;
}
}
if (output === 0) {
throw new Error('Слишком мало токенов для такого количества картинок. Избежать "flood control" не удастся. Подробности в документации');
}
return output * 1000 + extraDelay;
}
static makeLoadData(shift, executeChunkSize, albumParams) {
const items = albumParams.parsedDescrs.slice(0, shift); // x2 preload
const splittedItems = [].concat.apply([],
items.map(function(elem, i) {
return i % executeChunkSize ? [] : [items.slice(i, i + executeChunkSize)];
}
) // divide by 25 due to vk.api.execute restrictions
)
return splittedItems;
}
static async savePhotos(vk, saveData) {
const execString = VkAlbPoster.savePhotosExec.toString();
let execBody = execString.slice(execString.indexOf('{') + 1, -1);
execBody = execBody.replace('REPLACE1', JSON.stringify(saveData));
return await vk.api.execute({ code: execBody });
}
// will be converted to string
static savePhotosExec() {
var saveData = REPLACE1;
var saveResults = [];
var i = 0, args, r;
while (i < saveData.length) {
args = saveData[i];
i = i + 1;
r = API.photos.save({
server: args.server,
photos_list: args.photos_list,
album_id: args.aid,
group_id: args.gid,
hash: args.hash,
caption: args.caption
});
saveResults.push(r[0]);
}
return saveResults;
}
}