This repository was archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
158 lines (149 loc) · 3.28 KB
/
Copy pathmain.js
File metadata and controls
158 lines (149 loc) · 3.28 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
const readline = require("readline");
const fs = require("fs");
const rl = readline.createInterface(
{
input: process.stdin,
output: process.stdout
});
const login = require("facebook-chat-api");
console.log("");
if(fs.existsSync("appstate.json"))
{
console.log("Logging in with cookies...");
login({appState: JSON.parse(fs.readFileSync("appstate.json", "utf8"))}, (err, api) =>
{
api.setOptions({logLevel: "silent"});
if(err) return console.error(err);
fs.writeFileSync("appstate.json", JSON.stringify(api.getAppState()));
logged(api);
});
}else
{
rl.question("Enter email: ", (user) =>
{
rl.question("Enter password: ", (pass) =>
{
console.log("Logging in with email...");
login({email: user, password: pass}, (err, api) =>
{
if(err)
{
switch (err.error)
{
case 'login-approval':
rl.question("\nEnter code: ", (code) =>
{
err.continue(code);
});
return;
default:
rl.close();
return console.error(err.error);
}
}
api.setOptions({logLevel: "silent"});
fs.writeFileSync("appstate.json", JSON.stringify(api.getAppState()));
logged(api);
});
});
});
}
function logged(api)
{
console.log("Logged in!");
console.log("\nChoose the thread you want to dump\nEnter nothing to load next 20 threads");
fetchThreads(api, null, 1);
}
function fetchThreads(api, timestamp, i)
{
var start = i;
api.getThreadList(20, timestamp, [], function(err, info)
{
if(err) return console.error(err);
for(var thread of info)
{
var name = thread.name;
if(name === null)
{
name = "";
for(var user of thread.participants)
{
name += user.name + ", ";
}
name = name.substring(0, name.length - 2);
thread.name = name;
}
console.log(i+". "+name);
i++;
}
rl.question("\nEnter thread id: ", (id) =>
{
var thread = info[id-start];
if(id.length == 0 || thread === undefined)
{
fetchThreads(api, parseInt(info[info.length-1].lastMessageTimestamp), i);
}else
{
dump(api, thread);
}
});
});
}
async function dump(api, thread)
{
console.log("\nDumping "+thread.name);
console.log("Message count: "+thread.messageCount);
var myID = api.getCurrentUserID();
var timestamp = null;
var i = 0;
var arr = [];
while(true)
{
var history = await fetchHistory(api, thread, timestamp);
var oldtimestamp = timestamp;
timestamp = parseInt(history[0].timestamp);
if(oldtimestamp != null)
{
history = history.slice(0, history.length-1);
}
if(history.length == 0)
{
break;
}
history.reverse();
for(h of history)
{
var obj = {};
var id = h.senderID;
if(id == myID)
{
id = "you";
}
obj.date = h.timestamp;
obj.id = id;
obj.body = h.body;
obj.attachments = h.attachments;
arr.push(obj);
}
i+=history.length;
console.log("Fetching... "+i+"/"+thread.messageCount);
}
console.log("Done!");
if(!fs.existsSync("dump"))
{
fs.mkdirSync("dump");
}
fs.writeFileSync("dump/"+thread.name.split(" ").join("_").toLowerCase()+".json", JSON.stringify(arr));
rl.close();
}
async function fetchHistory(api, thread, timestamp)
{
return new Promise((ret) =>
{
api.getThreadHistory(thread.threadID, 500, timestamp, (err, history) =>
{
if(err) return console.error(err);
ret(history);
});
});
}