-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgifUploader.js
137 lines (118 loc) · 3.45 KB
/
gifUploader.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
const VkApi = require('./VkApi');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const lib = require('./lib');
const gifPath = './gifs/';
const _token = '8ad59c39e12dd54444b130ace24f2110b7bc1db4def6de09c77851e7e1675dfe16eff2ce9919d5e3a8b51';
const captchaAPIKey = '2f54bb2ffb6a092f725a35366deed8f2';
const apiUrl = 'http://new.poster.dev/api/';
const taskUploadGif = async (title) => {
const vkApi = new VkApi(_token);
const uploadUrl = await vkApi.getuploadUrl();
const resultSendingFile = await vkApi.sendFile({
file: `${gifPath}${title}.gif`,
url : uploadUrl,
onProgress: function (progress) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Uploaded: ${progress.percent}% | size: ${progress.mbSent} / ${progress.mbSize} Mb | speed: ${progress.speed} Mb/sec |`);
}
});
let resSaveFile = await vkApi.saveFile({
uploadFile : resultSendingFile,
title : title
});
let jsonRes = JSON.parse(resSaveFile);
const jsonErr = jsonRes['error'];
if (jsonErr) {
if (jsonErr['error_code'] !== 14) {
throw jsonErr;
}
// @TODO: при неправльной разгаданой капчи нужна рекурсия
console.log('поймали капчу');
const captchaAnswer = await sendCaptcha(jsonErr['captcha_img']);
const resSaveFile = await vkApi.saveFile({
uploadFile : resultSendingFile,
title : title,
captcha: {
sid : jsonErr['captcha_sid'],
answer : captchaAnswer
}
});
jsonRes = JSON.parse(resSaveFile);
}
const savedFile = jsonRes['response'][0];
const apiResult = await lib.rp({
method : 'post',
uri : apiUrl + 'Gif.add',
form: {
doc_id : savedFile['did'],
owner_id : savedFile['owner_id'],
title : title,
url : savedFile['url'],
thumb : savedFile['thumb']
}
});
};
const sendCaptcha = async (image) => {
const captchaImg = await lib.rp({
method : 'get',
encoding : 'base64',
uri : image,
});
var formData = {
key : captchaAPIKey,
body : captchaImg,
method : 'base64',
json : 1
};
const requestApiCaptcha = await lib.rp({
method : 'post',
uri : 'http://rucaptcha.com/in.php',
formData : formData
});
await Promise.delay(5000);
const captchaRes = await lib.rp({
method : 'post',
uri : 'http://rucaptcha.com/res.php',
form: {
key : captchaAPIKey,
action : 'get',
id : JSON.parse(requestApiCaptcha)['request'],
json : 1
}
});
return JSON.parse(captchaRes)['request'];
};
const loopTask = async (gif) => {
try {
await taskUploadGif(gif);
} catch (error) {
if (error.response && error.response.statusCode === 504) {
console.log("Загрузка привела к 504, попробуем еще раз...");
Promise.delay(1500);
return loopTask(gif);
}
throw error;
}
};
(async () => {
try {
const files = await fs.readdirAsync(gifPath);
const gifRegExp = /\.gif$/;
const gifs = files
.filter(fileName => gifRegExp.test(fileName))
.map( fileName => fileName.replace(gifRegExp, ''));
const gifLength = gifs.length;
for ([index, gif] of gifs.entries()) {
console.log(`Загружаем ("${gif}"): ${index + 1} / ${gifLength}`);
await loopTask(gif);
console.log('\n__________');
}
} catch (error) {
if (error.response) {
return console.log('status> ', error.response.statusCode);
}
console.log('error! ', error);
}
})();