forked from Maximus220/becreepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (127 loc) · 4.59 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
//Init readline
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askQuestion(question) {
return new Promise((resolve, reject) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
//create folder ./json if doesn't exists
const fs = require('fs');
const jsonPath = "json/";
if (!fs.existsSync(jsonPath)){
fs.mkdirSync(jsonPath);
}
//test if creds.json exists
const credPath = "json/cred.json";
var cred = null;
if(fs.existsSync(credPath)){
try{
cred = JSON.parse(fs.readFileSync(credPath));
}catch(e){}
}
//Imports
const { sendConfirmation, checkCode, refreshToken, loadBearer, getFriends } = require("./requests.js");
//Program
async function main(){
if(!cred){
cred={};
let phoneNumber = await askQuestion("Type your phone number : ");
sendConfirmation(phoneNumber).then(async (res)=>{
cred.sessionInfo = res.sessionInfo;
cred.phoneNumber = phoneNumber;
let code = await askQuestion("Type the code you received : ");
checkCode(cred.sessionInfo, code).then((res)=>{
cred.idToken = res.idToken;
cred.refreshToken = res.refreshToken;
cred.localId = res.localId;
executeChecks();
});
});
}else{
executeChecks();
// try{
// downloadFriends(cred.bearer);
// }catch(e){
// executeChecks();
// }
}
}
main();
function executeChecks(){
refreshToken(cred.refreshToken).then((res1)=>{
cred.accessTokenGoogle = res1.access_token;
loadBearer(cred.accessTokenGoogle).then((res2)=>{
cred.bearer = res2.access_token;
fs.writeFileSync(credPath, JSON.stringify(cred));
downloadFriends(cred.bearer);
})
});
}
const friendFolder = "friends/"
if (!fs.existsSync(friendFolder)){
fs.mkdirSync(friendFolder);
}
function downloadFriends(bearer){
getFriends(bearer).then((res)=>{
res.friendsPosts.forEach((p)=>{
//create folder for each user
let folder = friendFolder+p.user.username;
if (!fs.existsSync(folder)){
fs.mkdirSync(folder);
}
//create file for each post
fs.writeFileSync(folder+"/"+p.momentId+".json", JSON.stringify(p));
//download each images
p.posts.forEach((post)=>{
//download post.primary.url and post.secondary.url
download(post.primary.url, folder+"/"+post.id+"_primary.webp", ()=>{
console.log("downloaded "+post.id+"_primary.webp")
});
download(post.secondary.url, folder+"/"+post.id+"_secondary.webp", ()=>{
console.log("downloaded "+post.id+"_secondary.webp")
});
fs.writeFileSync(folder+"/"+post.id+".json", JSON.stringify(post));
//Realmojis
if(post.realMojis && post.realMojis.length>0){
console.log("downloading "+post.realMojis.length+" realmojis")
post.realMojis.forEach((realmoji)=>{
let userFolder = friendFolder+realmoji.user.username;
if (!fs.existsSync(userFolder)){
fs.mkdirSync(userFolder);
}
download(realmoji.media.url, userFolder+"/realmoji_"+realmoji.type+"_"+realmoji.id+".webp", ()=>{
console.log("downloaded "+realmoji.user.username+"_"+realmoji.type+" realmoji")
});
//downaldo file with extension in url
let url = realmoji.user.profilePicture.url;
let ext = url.substring(url.lastIndexOf("."));
download(url, userFolder+"/pfp"+ext, ()=>{
console.log("downloaded "+userFolder+"/pfp"+ext)
});
});
}
});
console.log("downloaded "+p.user.username);
})
console.log("done");
});
}
const https = require('https');
function download(url, path, cb){
var file = fs.createWriteStream(path);
https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb);
});
}).on('error', function(err) {
fs.unlink(dest);
if (cb) cb(err.message);
});
}