-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage_windows.py
328 lines (278 loc) · 12.8 KB
/
Message_windows.py
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
from fbchat import Client, log
from getpass import getpass
from datetime import datetime
import sys, os, urllib, time, socket, shutil, requests
from glob import glob
from zipfile import ZipFile
socket.setdefaulttimeout(60)
reload(sys)
sys.setdefaultencoding("utf-8")
ending = '</div></div>'
username = str(raw_input("Username: "))
password = getpass()
client = Client(username, password)
zipping = str(raw_input("Want to save your data as a .Zip file y/n?: "))
uid = client.uid
USER = client.fetchUserInfo(client.uid)[client.uid]
self = USER.name
ID = []
NAME = []
docs = ['docx', 'doc', 'pdf', 'pptx', 'txt', 'xlsx']
media = ['mp3', 'mp4', 'aac', 'webm', 'avi', '3gp']
gen = ['jpg', 'png']
def download_file(add, name):
request = requests.get(add, timeout=60, stream=True)
#Open the output file and make sure we write in binary mode
flag = 0
with open(name, 'wb') as fh:
# Walk through the request response in chunks of 1024 * 1024 bytes, so 1MiB
for chunk in request.iter_content(1024 * 1024):
# Write the chunk to the file
flag += 1
if flag > 10:
Log_file.write("This file is bigger than 10MB so download it if you want-- " + add + '\n\n')
break
fh.write(chunk)
def make_zip():
file = open('instruction.txt', 'w')
file.write("Use your facebook password to decrypt Fb_Data.zip file")
file.close()
files = glob("Data\\*\\*\\*")
files += glob("Data\\*\\*")
files += glob("Data\\*")
zipfile = ZipFile("Fb_Data.zip", 'w')
for file in files:
if os.path.isfile(file):
zipfile.write(file)
zipfile.close()
shutil.rmtree("Data")
def do_rest(thread):
check = 0
data = str(thread).split(" ")
id = data[len(data)-1].split('(')[1].split(')')[0]
other = data[1]
name = str(data[1])
if len(data) == 4:
other = data[1] + " " + data[2]
name = str(data[1]) + '_' + str(data[2])
if len(data) == 5:
other = data[1] + " " + data[2] + " " + data[3]
name = data[1] + '_' + data[2] + '_' + data[3]
if len(data) == 6:
other = data[1] + " " + data[2] + " " + data[3] + " " + data[4]
name = data[1] + '_' + data[2] + '_' + data[3] + '_' + data[4]
starting = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>' + other + '- Messages</title> <link rel="stylesheet" href="..\\..\\style.css" type="text/css" /></head><body> <div class="contents"><h1>' + other +'</h1> <div class="thread"> Total number of messages = ' + str(thread.message_count)
Testing = Path_check(other)
folder_name = "Data\\" + other
log_file = folder_name+"\\" + name + ".txt"
filename = folder_name+"\\html\\" + name + ".htm"
file = open(filename, 'wb')
Log_file = open(log_file, 'wb')
file.write(starting)
flag = 1000
num = 0
timestamp = int(19800 + time.time())*1000
if str(data[0]) != '<GROUP':
ID.append(id)
NAME.append(other)
check = 1
while( flag > 999):
messages = client.fetchThreadMessages(thread_id=id, limit=1000, before=timestamp)
timestamp = messages[len(messages)-1].timestamp
for message in messages:
try:
if check == 0:
if message.author not in ID:
USER = client.fetchUserInfo(message.author)[message.author]
other = USER.name
ID.append(message.author)
NAME.append(other)
else:
for i in range(len(ID)):
if message.author == ID[i]:
other = NAME[i]
break
if message.extensible_attachment:
if message.extensible_attachment['story_attachment']['media']:
if message.extensible_attachment['story_attachment']['media']['is_playable']:
add = message.extensible_attachment['story_attachment']['media']['playable_url']
Filename = folder_name + "\\shares\\" + str(message.timestamp) + '.mp4'
if add is not None:
try:
download_file(add, Filename)
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <video width="500" controls> <source src="..\\..\\..\\' + Filename + '" type="video/mp4"></p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <video width="500" controls> <source src="..\\..\\..\\' + Filename + '" type="video/mp4"></p> \n' )
except:
Log_file.write("Getting some error now on url -: " + add + '\n\n')
else:
Log_file.write("Look at this separately--" + str(message.extensible_attachment) + '\n\n')
elif message.attachments:
for attachment in message.attachments:
# For Image
time.sleep(.1)
Filename = attachment['filename']
if Filename.split("-")[0] == 'image':
add = attachment['large_preview']['uri']
name = folder_name +"\\images\\"+ attachment['filename']+'.' +attachment['original_extension']
try:
download_file(add, name)
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ name +'"> <img src="..\\..\\..\\'+ name + '" alt="Folder" width="500" > </a></p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ name +'"> <img src="..\\..\\..\\'+ name + '" alt="Folder" width="500" > </a></p> \n' )
except:
Log_file.write( "Getting some error now on url -: " + add + '\n\n')
elif len(Filename.split(".")) > 1 and Filename.split(".")[len(Filename.split("."))-1] in docs:
add = attachment['url']
test = urllib.urlopen(add)
temp = test.read().split('replace("')[1]
temp = temp.split('");</script>')[0]
temp = temp.replace("\\","")
Temp = Filename
Filename = folder_name + "\\docs\\" + Filename
try:
download_file(temp, Filename)
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ Filename +'">' + Temp + '</a></p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ Filename +'">' + Temp + '</a></p> \n' )
except:
Log_file.write( "Getting some error now on url -: " + temp + '\n\n')
elif len(Filename.split(".")) > 1 and Filename.split(".")[len(Filename.split("."))-1] in media:
try:
add = attachment['playable_url']
Filename = folder_name + "\\media\\" + Filename
download_file(add, Filename)
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <video width="500" controls> <source src="..\\..\\..\\' + Filename + '" type="video/mp4"></p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <video width="500" controls> <source src="..\\..\\..\\' + Filename + '" type="video/mp4"></p> \n' )
except:
Log_file.write( "Getting some error now on url -: " + add + '\n\n')
elif Filename.split("-")[0] == 'gif':
try:
add = attachment['animated_image']['uri']
Filename = folder_name + "\\media\\" + Filename
download_file(add, Filename)
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ name +'"> <img src="..\\..\\..\\'+ name + '" alt="Folder" width="500" > </a></p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ name +'"> <img src="..\\..\\..\\'+ name + '" alt="Folder" width="500" > </a></p> \n' )
except:
Log_file.write( "Getting some error now on url -: " + add + '\n\n')
else:
add = attachment['url']
test = urllib.urlopen(add)
temp = test.read().split('replace("')[1]
temp = temp.split('");</script>')[0]
temp = temp.replace("\\","")
Temp = Filename
Filename = folder_name + "\\Random\\" + Filename
try:
download_file(temp, Filename)
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ Filename +'">' + Temp + '</a></p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p> <a href="..\\..\\..\\'+ Filename +'">' + Temp + '</a></p> \n' )
except:
Log_file.write( "Getting some error now on url -: " + temp + '\n\n')
elif message.text is not None and message.sticker is None:
if message.author == uid:
file.write('<div class="message"><div class="message_header"><span class="user">' + self + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p>' + message.text + ' </p> \n' )
else:
file.write('<div class="message"><div class="message_header"><span class="user">' + other + ' </span><span class="meta"> ')
file.write(str(datetime.fromtimestamp(float(int(message.timestamp)/1000))))
file.write('</span></div></div><p>' + message.text + ' </p> \n' )
except:
pass
flag = len(messages)
num += flag
print num, " messages had been downloaded from today till - ",datetime.utcfromtimestamp(float(timestamp)/1000).strftime('%d-%m-%Y')
file.write(ending)
file.close()
def Path_check(name):
path = "Data"
if not os.path.exists(path):
os.mkdir(path)
source = 'Resources\\style.css'
target = 'Data'
try:
shutil.copy(source, target)
except IOError as e:
print("Unable to copy file. %s" % e)
except:
Log_file.write(("Unexpected error:" + str(sys.exc_info())))
name = path + '\\' +name
path = name
if not os.path.exists(path):
os.mkdir(path)
path = name + "\\docs"
if not os.path.exists(path):
os.mkdir(path)
path = name + "\\html"
if not os.path.exists(path):
os.mkdir(path)
path = name + "\\images"
if not os.path.exists(path):
os.mkdir(path)
path = name + "\\media"
if not os.path.exists(path):
os.mkdir(path)
path = name + "\\shares"
if not os.path.exists(path):
os.mkdir(path)
path = name + "\\Random"
if not os.path.exists(path):
os.mkdir(path)
return True
username = str(raw_input("want to download messages from a specific friend type(y/n): "))
if username.lower() == 'y':
names = str(raw_input("Name of that friends separated by a comma like - satyendra pandey, Narendra pandey--: "))
names = names.split(',')
for name in names:
thread = client.searchForThreads(name)[0]
do_rest(thread)
if zipping.lower() == 'y':
make_zip()
else:
num = int(raw_input("Number of friends from top of your chatlist:"))
if num < 20:
threads = client.fetchThreadList(limit = num)
else:
threads = client.fetchThreadList(offset = 17, limit = 3)
num = (num-20)/20
for i in range(num):
offset = 20*(i+1)
threads += client.fetchThreadList(offset = offset, limit= 20)
for thread in threads:
do_rest(thread)
if zipping.lower() == 'y':
make_zip()