This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk-fetch-posts.js
265 lines (250 loc) · 8.14 KB
/
vk-fetch-posts.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
/**
* A fairly simple command-line tool
* to fetch all posts from certain
* author(s) wall in VKontakte social
* network.
*
* APIs used:
* https://vk.com/pages?oid=-17680044&p=wall.get
* https://vk.com/pages?oid=-17680044&p=getProfiles
*
* Usage:
* $ npm install
* $ node vk-fetch-posts --authors 1,2,3 --group 4
*
* @type {exports}
*/
var https = require('https'),
mkdirp = require('mkdirp'),
request = require('request'),
program = require('commander'),
pjson = require('./package.json'),
FileQueue = require('filequeue'),
fs = require('fs');
var fq = new FileQueue(100);
var authIds = [];//"103279512,10522384,15666691,1732372,201188,2180211,2422348,3206615,3510939,35674186,40031,675490,88158188,987082";
var authors = {};
var groupId; //-14837503&
var source = './tmp/';
var result = './result/';
program
.version(pjson.version)
.option('-a, --authors [items]', 'A comma-separated list of authors ids', function (val) {
authIds = val.split(',');
var l = authIds.length;
while (l--) {
if (isNaN(parseInt(authIds[l]))) {
console.error('Wrong argument: ', authIds[l]);
process.exit(255);
}
}
return authIds;
})
.option('-g --group <item>', 'A group id to work with', function (val) {
if (isNaN(val)) {
console.error('Wrong argument: ', val);
process.exit(255);
}
else {
groupId = val;
}
})
.parse(process.argv);
if (!program.group) {
console.error("You haven't provided enough arguments.");
program.help();
}
mkdirp(result, mkdirCallback);
// All set, lets start downloading.
// TODO: Consider using Promises and create
// execution chain here, rather than
// calling functions from functions.
if (!fs.existsSync(source)) {
// Download only in case we don't have
// any source directory, since it is
// a fairly costly operation.
fetchPosts(0, undefined, 1);
mkdirp(source, mkdirCallback);
}
else {
start();
}
function mkdirCallback(err) {
if (err) {
console.error("Can't create directories. Exiting...");
throw err;
}
}
function fetchPosts(offset, totalPosts, count) {
https.get('https://api.vk.com/method/wall.get?owner_id=' + groupId + '&count=' + count + '&offset=' + offset + '&filter=owner', function (res) {
var responseString = '';
res.on('data', function (chunk) {
responseString += chunk;
});
res.on('end', function () {
if (!totalPosts) {
var responseJson = JSON.parse(responseString);
totalPosts = responseJson.response[0];
}
fq.writeFile(source + 'batch_' + offset + '.txt', responseString, function () {
console.log("Downloaded posts batch " + offset);
if (offset < totalPosts) {
fetchPosts(offset + count, totalPosts, 100);
}
else {
console.info("Downloaded everything, proceeding...");
start();
}
});
});
});
}
/**
* Starting point: fetch proper names for authors IDs
*/
function start() {
if (!authIds.length) {
processFiles();
}
else {
console.info("Fetching authors info...");
https.get("https://api.vk.com/method/getProfiles?user_ids=" + authIds.join(','), function (res) {
var responseString = '';
res.on('data', function (chunk) {
responseString += chunk;
});
res.on('end', function () {
var resJson = JSON.parse(responseString);
console.info(resJson);
for (var i = 0, l = resJson.response.length; i < l; i++) {
var author = resJson.response[i];
authors[ author.uid ] = author;
}
console.log("...done.");
processFiles();
});
}).on('error', function (e) {
console.log("Got error: " + e.message);
});
}
}
/**
* Process files in source directory
*/
function processFiles() {
console.log("Processing files in ", source, ".");
fq.readdir(source, function (err, files) {
if (err) throw err;
for (var i = 0, l = files.length; i < l; i++) {
processFile(source + files[ i ]);
}
});
}
/**
* Process a single file.
*
* @param file
*/
function processFile(/* String */ file) {
console.log("Reading file...", file);
fq.readFile(file, { flag: 'r'}, function (err, jsonString) {
var fileCont;
try {
fileCont = JSON.parse(jsonString);
}
catch (e) {
console.log("Failed to parse ", file, ", skipping...", e);
return;
}
for (var i = 1, l = fileCont.response.length; i < l; i++) {
processPost(fileCont.response[ i ]);
}
});
}
/**
* Process a post object.
*
* @param post
*/
function processPost(/* Object */ post) {
var id = post.id;
if (post.post_type != 'post') {
console.log('Post [' + id + '] is a re-post, skipping');
return;
}
var signerId = post.signer_id;
var text = post.text;
var authorInfo = authors[ signerId ];
var authorName = authorInfo ? authorInfo.first_name + " " + authorInfo.last_name : "Anonymous";
// Construct a nice looking date from unix time.
var date = post.date;
var prettyDate = new Date(0);
prettyDate.setUTCSeconds(date);
var year = prettyDate.getFullYear();
var month = prettyDate.getMonth() + 1;
if (month < 10) month = '0' + month;
var day = prettyDate.getDate();
if (day < 10) day = '0' + day;
var timeStamp = year + '-' + month + '-' + day;
// IMPORTANT: Directory and file structure for this post
var postFolder = result + authorName + '/' + timeStamp + '_' + id;
mkdirp(postFolder, function () {
fq.writeFile(postFolder + "/text.txt",
post.text + "\n Автор: " + authorName + "\n" +
"Vk link: https://vk.com/wall" + post.from_id + '_' + id,
function () {
if (post.attachments) {
processAttachments(post.attachments, postFolder);
}
}
);
});
}
/**
* Process post attachments.
*
* @param attachments
* @param postFolder
*/
function processAttachments(/* Object */ attachments, /* String */ postFolder) {
for (var i = 0, l = attachments.length; i < l; i++) {
var attachment = attachments[ i ];
var type = attachment.type;
var attachmentsDir = postFolder + "/attachments/";
if (type != 'photo' && type != 'link') return;
mkdirp(attachmentsDir, function (type, attachment, attachmentsDir, i) {
return (function (type, attachments, attachmentsDir, i) {
if (type == 'photo') {
var id = attachment.photo.pid;
var fileName = attachmentsDir + id;
var src = attachment.photo.src;
var ext = src.match('.jpg') ? '.jpg' : '.png';
download(src, fileName + ext, function () {
fq.writeFile(fileName + ".txt", attachment.photo.text, function () {
console.log("Downloaded: ", src);
});
});
}
else if (type == 'link') {
fq.writeFile(attachmentsDir + 'link_' + i + '.txt', JSON.stringify(attachment.link), function () {
console.log("Wrote link");
});
}
})(type, attachments, attachmentsDir, i);
});
}
}
/**
* Helper method to make a HEAD request
* to specified URI
*
* @param uri URI to call
* @param filename filename to use for downloaded
* @param callback A callback to execute on close
*/
function download(uri, filename, callback) {
request.head(uri, function (/*err, res, body*/) {
var filestream = fq.createWriteStream(filename);
request(uri).pipe(filestream).on('close', callback);
});
}