-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
331 lines (288 loc) · 15.7 KB
/
main.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
const { Client } = require('libfb');
const client = new Client()
const path = require("path");
/* stdout function */
// stdoutStruct : status, progress, data, code, errMessage
function stdout(stdoutStruct){
if(stdoutStruct.status == "start") {
console.log(`Start Collect Data`);
} else if (stdoutStruct.status == "loading") {
console.log(`progress=${stdoutStruct.progress}`);
} else if (stdoutStruct.status == "end") {
console.log(`data=${encodeURIComponent(stdoutStruct.data)}`);
} else if (stdoutStruct.status == "error") {
console.log(`status=${stdoutStruct.status}&message=${encodeURIComponent(stdoutStruct.errMessage)}&code=${stdoutStruct.code}`);
}
}
/* files download */
var fs = require('fs-extra');
var fetch = require('node-fetch');
/* Make folder if not exist*/
function makeFolder(dir) {
if(!fs.existsSync(dir)){
fs.mkdirSync(dir, { recursive: true });
}
}
/* Download a file to disk*/
function downloadFile(fileUrl, destPath) {
if (!fileUrl) return Promise.reject(new Error('Invalid fileUrl'));
if (!destPath) return Promise.reject(new Error('Invalid destPath'));
return new Promise(function(resolve, reject) {
fetch(fileUrl).then(function(res) {
var fileStream = fs.createWriteStream(destPath);
res.body.on('error', reject);
fileStream.on('finish', resolve);
res.body.pipe(fileStream);
});
});
}
/* Here defines the structure of message */
// |finalJSON| structure (which is top-level JSON)
function finalJSONStruct(){
var result = new Object();
result.service = "facebook_messenger";
result.chatrooms = new Array();
return result;
}
// |chatroom| structure
function chatroomStruct(){
var cr = new Object();
cr.chatroom_name = "";
cr.chatroom_icon = "";
cr.last_message = "";
cr.last_message_time = "";
cr.chats = new Array();
return cr;
}
// |chats| structure
function chatStruct(chat_id="", is_read=null, is_owner=null, sender="", message_type="", original_type="", message_content="", message_time=""){
var ch = new Object();
ch.chat_id = chat_id;
ch.is_read = is_read;
ch.is_owner = is_owner;
ch.sender = sender;
ch.message_type = message_type;
ch.original_type = original_type;
ch.message_content = message_content;
ch.message_time = message_time;
return ch;
}
stdout({
'status': 'start'
});
// Usage: node main.js ID PW PATH (splited with whitespace)
if(process.argv.length != 5){
var errorMessage = "USAGE: node main.js <ID> <PW> <PATH>";
stdout({
'status': 'error',
'errorMessage': errorMessage,
'code': 100 // Error
});
} else{
/*id, password, db path etc*/
var id = process.argv[2] ;
var password = process.argv[3];
var attachmentPath = process.argv[4]; // It needs escaping special charset
const attachmentsPath = path.join(attachmentPath, 'attachments');
const jsonPath = path.join(attachmentPath, 'json');
makeFolder(attachmentsPath);
makeFolder(jsonPath);
/* Facebook login and get Data */
client.login(id, password).then( async () => {
session = await client.getSession();
uid = session.tokens.uid
usrInfo = await client.getUserInfo(uid);
const userinfoPath = path.join(jsonPath, "userInfo.json");
fs.writeFileSync(userinfoPath, JSON.stringify(usrInfo,null,2));
// If login failed, exit.
if(!client.loggedIn){
var errorMessage = "Login Failed";
stdout({
'status': 'error',
'message': errorMessage,
'code': 101 // Login Error
});
} else {
stdout({
'status': "loading",
'progress': 10
});
list = await client.getThreadList(10000); // Account Information
fs.writeFileSync(userinfoPath, JSON.stringify(list,null,2));
// Here is |finalJSON| structure
var finalJSON = finalJSONStruct();
thrCnt = list.length; // 대화방 갯수
for(var i=0; i< thrCnt; i++){
data = await client.getThreadInfo(list[i].id); // Thread ID
fs.appendFileSync(path.join(jsonPath, "data.json"), JSON.stringify(data,null,2) + ",\n");
particNum = data.participants.length; /*about participants name*/
let numCallbackRuns = 0;
var particNms = '' // Chat partitcipants
data.participants.forEach(element => {
if(numCallbackRuns == 0){
particNms = element.name
}else{
particNms += ', ' + element.name
}
numCallbackRuns++
});
msgBox = await client.getMessages(list[i].id, 10000); /*call facebook messages*/
fs.appendFileSync(path.join(jsonPath, "msgBox.json"), JSON.stringify(msgBox,null,2) + ",\n");
// Here is |chatroom| structure
var chatroom = chatroomStruct();
var conCnt = msgBox.length; // messages count
for (var k=0; k <conCnt; k++){
var athrInfo = await client.getUserInfo(msgBox[k].authorId);
if(athrInfo.name == ""){
athrInfo.name = "Deleted Accounts";
}
fs.appendFileSync(path.join(jsonPath, "athrInfo.json"), JSON.stringify(athrInfo,null,2) + ",\n");
function formatDateToCustomFormat(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
const date = new Date(msgBox[k].timestamp);
const formattedDate = formatDateToCustomFormat(date);
//const timestamp = new Date(msgBox[k].timestamp).toISOString(); //date format ISO 8601
const timestamp = formattedDate //YYY-MM-DD HH:MM:SS
const threadid = msgBox[k].threadId; // Thread Id
/* Message Type */
attachGbn = msgBox[k].fileAttachments.length;
mediaGbn = msgBox[k].mediaAttachments.length;
if(attachGbn == 0 && mediaGbn == 0){ // Message Type: Message
messageBox = msgBox[k].message.replace("'","''");
// Here is |Chat| structure
var chat = chatStruct(
id=msgBox[k].id,
is_read=null,
is_owner=(usrInfo.id == athrInfo.id),
sender = athrInfo.name,
message_type = "text",
original_type = "text",
message_content = messageBox,
message_time = timestamp
);
chatroom.chats.push(chat);
/*db.run("INSERT INTO MS_FB_ChatData (id, sender, time, content, content_type, attachment, attachment_path, app)" +
"VALUES ('"+list[i].id+"', '"+name+"', '"+timestamp+"', '"+messageBox+"','messages',NULL, NULL,'FB_Messenger')");*/
if(k==conCnt-1){ // Insert last message information to |Chatroom| structure
chatroom.chatroom_id = list[i].id;
chatroom.chatroom_name = `${particNms}(${particNum})`; // Participants Name
chatroom.chatroom_icon = null;
chatroom.last_message = messageBox;
chatroom.last_message_time = timestamp;
/*db.run("INSERT INTO MS_FB_ChatRoom (id, participants, Num, LastSender, LastTime, LastContent, LastType)" +
"VALUES ('"+threadid+"', '"+particNms+"', '"+particNum+"', '"+name+"','"+timestamp+"','"+messageBox+"','message')");*/
}
}else{ // Message Type: attachments (mediaAttachments)
for(var b=0; b < msgBox[k].mediaAttachments.length; b++){
if(msgBox[k].mediaAttachments[b] != undefined){
mediaMsg = msgBox[k].mediaAttachments[b].message
originType = msgBox[k].mediaAttachments[b].type
// Handling deleted message
if(originType == "UnavailableXMA") {
mediaMsg = "[삭제된 메시지]";
mediaType = null;
}
// Here is |Chat| structure
var chat = chatStruct(
id=msgBox[k].id,
is_read=null,
is_owner=(usrInfo.id == athrInfo.id),
sender = athrInfo.name,
message_type = mediaType,
original_type = originType,
message_content = mediaMsg,
message_time = timestamp
);
chatroom.chats.push(chat);
/* db.run("INSERT INTO MS_FB_ChatData (id, sender, time, content, content_type, attachment, attachment_path, app)" +
"VALUES ('"+list[i].id+"', '"+name+"', '"+timestamp+"', '"+mediaMsg+"', '"+mediaType+"', NULL, NULL, 'FB_Messenger')");*/
}
if(k==conCnt-1){ // Insert last message information to |Chatroom| structure
chatroom.chatroom_id = list[i].id;
chatroom.chatroom_name = `${particNms}(${particNum})`; // 참가자 이름(참가자 숫자)
chatroom.chatroom_icon = null;
chatroom.last_message = mediaMsg;
chatroom.last_message_time = timestamp;
/* db.run("INSERT INTO MS_FB_ChatRoom (id, participants, Num, LastSender, LastTime, LastContent, LastType)" +
"VALUES ('"+threadid+"', '"+particNms+"', '"+particNum+"', '"+name+"','"+timestamp+"','"+mediaMsg+"','"+mediaType+"')"); */
}
}
for (var a=0; a< msgBox[k].fileAttachments.length; a++){ // Message Type: attachments (fileAttachments)
if(msgBox[k].fileAttachments[a] != undefined){
attachBox = await client.getAttachmentInfo(msgBox[k].id, msgBox[k].fileAttachments[b].id) // 추가
//var fileUrl = msgBox[k].fileAttachments[b].url;
//var fileNm = msgBox[k].fileAttachments[b].filename;
var fileUrl = attachBox.redirect_uri;
var fileNm = attachBox.filename;
if(fileUrl != undefined){
if(!fileNm.includes('.')){
var extension = '.' + msgBox[k].fileAttachments[b].mimeType.split('/')[1];
fileNm += extension;
}
var downloadPath = path.join(attachmentsPath, fileNm);
try{
var downloadPromise = downloadFile(fileUrl, downloadPath);
} catch {
var warningMessage = `failed attachments: ${msgBox[k].fileAttachments[b]}`;
console.log(`warning=${warningMessage}`);
}
}
if(msgBox[k].fileAttachments[a].mimeType == "image/gif") {
mediaType = "sticker"
}else if(msgBox[k].fileAttachments[a].mimeType == "image/jpeg"){
mediaType = "picture"
}else{
mediaType = "attach"
}
// Here is |Chat| structure
var chat = chatStruct(
id=msgBox[k].id,
is_read=null,
is_owner=(usrInfo.id == athrInfo.id),
sender = athrInfo.name,
message_type = mediaType,
original_type = msgBox[k].fileAttachments[a].mimeType,
message_content = downloadPath,
message_time = timestamp
);
chatroom.chats.push(chat);
/* db.run("INSERT INTO MS_FB_ChatData (id, sender, time, content, content_type, attachment, attachment_path, app)" +
"VALUES ('"+list[i].id+"', '"+name+"', '"+timestamp+"', '"+messageBox+"','"+msgBox[k].fileAttachments[a].mimeType+"',
'"+msgBox[k].fileAttachments[a].filename+"','"+msgBox[k].fileAttachments[a].url+"','FB_Messenger')");*/
if(k==conCnt-1){ // Insert last message information to |Chatroom| structure
chatroom.chatroom_id = list[i].id;
chatroom.chatroom_name = `${particNms}(${particNum})`; // 참가자 이름(참가자 숫자)
chatroom.chatroom_icon = null;
chatroom.last_message = downloadPath;
chatroom.last_message_time = timestamp;
/* db.run("INSERT INTO MS_FB_ChatRoom (id, participants, Num, LastSender, LastTime, LastContent, LastType)" +
"VALUES ('"+threadid+"', '"+particNms+"', '"+particNum+"', '"+name+"','"+timestamp+"','fileAttachments',
'"+msgBox[k].fileAttachments[a].mimeType+"')");*/
}
}
}
}
}
// Insert |chatroom| in the finalJSON.chatrooms
finalJSON.chatrooms.push(chatroom);
var progress = 10 + 90 * ( (i + 1) / thrCnt);
stdout({
'progress': Math.round(progress+'%')
});
}
stdout({
'data': JSON.stringify(finalJSON)
});
setTimeout(()=>{
process.exit(6);
}, 5000)
}
});
}