-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathandroid_facebook.py
584 lines (548 loc) · 30.1 KB
/
android_facebook.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#coding=utf-8
# @Author : xutao
# @File : ${android_facebook}.py
import clr
clr.AddReference('System.Core')
clr.AddReference('System.Xml.Linq')
clr.AddReference('System.Data.SQLite')
try:
clr.AddReference('model_im')
clr.AddReference("bcp_im")
except:
pass
del clr
import PA_runtime
from PA_runtime import *
import os
from System.Data.SQLite import *
import shutil
import System
from System.Xml.Linq import *
import json
from System.Xml.XPath import Extensions as XPathExtensions
import model_im
import bcp_im
import hashlib
def GetString(reader, idx):
return reader.GetString(idx) if not reader.IsDBNull(idx) else ""
def GetInt64(reader, idx):
return reader.GetInt64(idx) if not reader.IsDBNull(idx) else 0
def GetBlob(reader, idx):
return reader.GetValue(idx) if not reader.IsDBNull(idx) else None
def GetFloat(reader, idx):
return reader.GetFloat(idx) if not reader.IsDBNull(idx) else 0
def release_connection(reader, conn):
reader.Close()
conn.Close()
def md5(cache_path, node_path):
m = hashlib.md5()
m.update(node_path.encode(encoding = 'utf-8'))
db_path = cache_path + "\\" + m.hexdigest() + ".db"
return db_path
VERSION_APP_VALUE = 1
class Facebook(object):
def __init__(self, node, extractDeleted, extractSource):
self.root = node
self.extractDeleted = extractDeleted
self.extractSource = extractSource
self.cache = ds.OpenCachePath("Facebook")
self.account_id = None
self.account_name = None
self.facebook_db = model_im.IM()
self.contacts_dict = {}
def get_account_id(self):
user_node = self.root.Parent.GetByPath("/shared_prefs/acra_criticaldata_store.xml")
if user_node is None:
return
es = []
try:
user_node.Data.seek(0)
xml = XElement.Parse(user_node.read())
es = xml.Elements("string")
except Exception as e:
print(e)
#pass
for rec in es:
if rec.Attribute("name") and rec.Attribute("name").Value == "USER_ID":
user_id = rec.FirstNode.Value
self.account_id = "FACEBOOK:" + user_id
def get_friends(self):
friends_node = self.root.GetByPath("threads_db2")
if friends_node is None:
return
try:
db = SQLiteParser.Database.FromNode(friends_node, canceller)
if db is None and "thread_users" not in db.Tables:
return
tbs = SQLiteParser.TableSignature("thread_users")
if self.extractDeleted:
SQLiteParser.Tools.AddSignatureToTable(tbs, "user_key", SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "is_friend", SQLiteParser.FieldType.Int, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "commerce_page_type", SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "is_commerce", SQLiteParser.FieldType.Int, SQLiteParser.FieldConstraints.NotNull)
for rec in db.ReadTableRecords(tbs, self.extractDeleted, True):
if canceller.IsCancellationRequested:
return
if "profile_type" in rec and rec["profile_type"].Value == "user":
rec_id = rec["user_key"].Value
if rec_id == self.account_id:
account = model_im.Account()
account.source = friends_node.AbsolutePath
if rec.Deleted == DeletedState.Deleted:
account.deleted = 1
account.account_id = self.account_id
if "name" in rec and (not rec["name"].IsDBNull):
self.account_name = rec["name"].Value
account.nickname = rec["name"].Value
self.contacts_dict[rec_id] = rec["name"].Value
if "profile_pic_square" in rec and (not rec["profile_pic_square"].IsDBNull):
tmp = rec["profile_pic_square"].Value
tmp_json = json.loads(tmp)
tmp_dict = tmp_json[-1:][0]
account.photo = tmp_dict.get("url")
try:
self.facebook_db.db_insert_table_account(account)
except Exception as e:
print(e)
#pass
elif rec["is_friend"].Value == 1:
friend = model_im.Friend()
friend.account_id = self.account_id
friend.friend_id = rec_id
friend.source = friends_node.AbsolutePath
friend.type = model_im.FRIEND_TYPE_FRIEND
if rec.Deleted == DeletedState.Deleted:
friend.deleted = 1
if "name" in rec and (not rec["name"].IsDBNull):
friend.nickname = rec["name"].Value
self.contacts_dict[rec_id] = rec["name"].Value
if "profile_pic_square" in rec and (not rec["profile_pic_square"].IsDBNull):
tmp = rec["profile_pic_square"].Value
json_tmp = json.loads(tmp)
dict_tmp = json_tmp[-1:][0]
friend.photo = dict_tmp.get("url")
try:
self.facebook_db.db_insert_table_friend(friend)
except Exception as e:
print(e)
#pass
except Exception as e:
print(e)
#pass
self.facebook_db.db_commit()
def get_friends_chat(self):
friends_chat = self.root.GetByPath("threads_db2")
if friends_chat is None:
return
conn = System.Data.SQLite.SQLiteConnection("Data Source = {0}; ReadOnly = True".format(friends_chat.PathWithMountPoint))
try:
conn.Open()
cmd = System.Data.SQLite.SQLiteCommand(conn)
cmd.CommandText = '''
select messages.thread_key, text, sender, messages.timestamp_ms, messages.timestamp_sent_ms, shares, msg_type,messages.attachments from messages, folders where messages.thread_key = folders.thread_key and messages.thread_key like "ONE%" ORDER BY messages.timestamp_ms
'''
reader= cmd.ExecuteReader()
fs = self.root.FileSystem
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
message = model_im.Message()
message.source = friends_chat.AbsolutePath
message.account_id = self.account_id
send_info = GetString(reader, 2) if GetString(reader, 2) else None
msg_type = GetInt64(reader, 6) if GetInt64(reader, 6) else None
if send_info:
send_dic = json.loads(send_info)
if "name" in send_dic:
message.sender_name = send_dic.get("name")
if "user_key" in send_dic:
message.sender_id = send_dic.get("user_key")
if send_dic.get("user_key") == self.account_id:
message.is_sender = 1
thread_key = GetString(reader, 0)
talk_id = "FACEBOOK:" + thread_key[len("ONE_TO_ONE:"):len("ONE_TO_ONE:")+len("000000000000000")]
message.talker_id = talk_id
if talk_id in self.contacts_dict:
message.talker_name = self.contacts_dict.get(talk_id)
message.content = GetString(reader, 1)
if GetString(reader, 1):
message.type = 1
if GetInt64(reader, 4):
message.send_time = int(str(GetInt64(reader, 4))[:-3])
else:
message.send_time = int(str(GetInt64(reader, 3))[:-3])
message.talker_type = 1 # 好友聊天
attachment = GetString(reader, 7)
if attachment:
attachment_str = json.loads(attachment)
attachment_dict = attachment_str[0]
if "type" in attachment_dict:
tmp_type = attachment_dict["type"]
if tmp_type == "VIDEO":
message.type = 3
elif tmp_type == "PHOTO":
message.type = 2
elif tmp_type == "VIDEO":
message.type = 4
if "uri" in attachment_dict:
media_path = attachment_dict.get("uri")
if media_path.startswith("file:"):
lists = re.split("/", media_path)
media_name = lists[-1]
media_path_list = fs.Search(media_name)
for i in media_path_list:
message.media_path = i.AbsolutePath
message.content = media_name
else:
message.content = media_name + "(本地资源已被删除)"
elif media_path.startswith("http"):
message.media_path = media_path
elif "mime_type" in attachment_dict:
if attachment_dict["mime_type"] == "image/jpeg":
message.type = 2
elif attachment_dict["mime_type"] == "audio/mpeg":
message.type = 3
if "filename" in attachment_dict:
media_name = attachment_dict.get("filename")
media_path_list = fs.Search(media_name)
if media_path_list:
for i in media_path_list:
message.media_path = i.AbsolutePath
message.content = media_name
else:
message.content = media_name+ "(本地资源已被删除)"
if GetString(reader, 5):
share_list = json.loads(GetString(reader, 5))
share_dict = share_list[0]
if "media" in share_dict:
media_info = share_dict.get("media")
if "type" in media_info[0] and media_info[0]["type"] == "VIDEO":
message.type = 4
if "type" in media_info[0] and media_info[0]["type"] == "LINK":
message.type = 8
if "href" in share_dict:
message.media_path = share_dict["href"]
if "name" in share_dict:
message.content = share_dict.get("name")
try:
self.facebook_db.db_insert_table_message(message)
except Exception as e:
print(e)
#pass
elif send_info == "" and msg_type == -1: # 无效的信息
continue
except Exception as e:
pass
except Exception as e:
pass
#pass
release_connection(reader, conn)
self.facebook_db.db_commit()
def get_groups_chat(self):
groups_chat = self.root.GetByPath("threads_db2")
if groups_chat is None:
return
conn = System.Data.SQLite.SQLiteConnection("Data Source = {0}; ReadOnly = True".format(groups_chat.PathWithMountPoint))
try:
conn.Open()
cmd = System.Data.SQLite.SQLiteCommand(conn)
cmd.CommandText = '''
select messages.thread_key, text, sender, messages.timestamp_ms, messages.timestamp_sent_ms, shares, msg_type,messages.attachments from messages, folders where messages.thread_key = folders.thread_key and messages.thread_key like "GROUP%" ORDER BY messages.timestamp_ms
'''
reader= cmd.ExecuteReader()
fs = self.root.FileSystem
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
message = model_im.Message()
message.source = groups_chat.AbsolutePath
message.account_id = self.account_id
send_info = GetString(reader, 2) if GetString(reader, 2) else None
msg_type = GetInt64(reader, 6) if GetInt64(reader, 6) else None
if send_info:
send_dic = json.loads(send_info)
if "name" in send_dic:
message.sender_name = send_dic.get("name")
if "user_key" in send_dic:
message.sender_id = send_dic.get("user_key")
if send_dic.get("user_key") == self.account_id:
message.is_sender = 1
message.talker_id = GetString(reader, 0)
message.content = GetString(reader, 1)
if GetString(reader, 1):
message.type = 1
if GetInt64(reader, 4):
message.send_time = int(str(GetInt64(reader, 4))[:-3])
else:
message.send_time = int(str(GetInt64(reader, 3))[:-3])
message.talker_type = 2 # 群聊天
attachment = GetString(reader, 7)
if attachment:
attachment_str = json.loads(attachment)
attachment_dict = attachment_str[0]
if "type" in attachment_dict:
tmp_type = attachment_dict["type"]
if tmp_type == "VIDEO":
message.type = 3
elif tmp_type == "PHOTO":
message.type = 2
elif tmp_type == "VIDEO":
message.type = 4
if "uri" in attachment_dict:
media_path = attachment_dict.get("uri")
if media_path.startswith("file:"):
lists = re.split("/", media_path)
media_name = lists[-1]
media_path_list = fs.Search(media_name)
if media_path_list:
for i in media_path_list:
message.media_path = i.AbsolutePath
message.content = media_name
else:
message.content = media_name + "(本地资源已被删除)"
elif media_path.startswith("http"):
message.media_path = media_path
elif "mime_type" in attachment_dict:
if attachment_dict["mime_type"] == "image/jpeg":
message.type = 2
elif attachment_dict["mime_type"] == "audio/mpeg":
message.type = 3
if "filename" in attachment_dict:
media_name = attachment_dict.get("filename")
media_path_list = fs.Search(media_name)
if media_path_list:
for i in media_path_list:
message.media_path = i.AbsolutePath
message.content = media_name
else:
message.content = media_name + "(本地资源已被删除)"
if GetString(reader, 5):
share_list = json.loads(GetString(reader, 5))
share_dict = share_list[0]
if "media" in share_dict:
media_info = share_dict.get("media")
if "type" in media_info[0] and media_info[0]["type"] == "VIDEO":
message.type = 4
if "type" in media_info[0] and media_info[0]["type"] == "LINK":
message.type = 8
if "href" in share_dict:
message.media_path = share_dict["href"]
if "name" in share_dict:
message.content = share_dict.get("name")
try:
self.facebook_db.db_insert_table_message(message)
except Exception as e:
print(e)
#pass
elif send_info == "" and msg_type == -1: # 无效的信息
continue
except Exception as e:
pass
except Exception as e:
pass
#pass
release_connection(reader, conn)
self.facebook_db.db_commit()
def get_recover_friends_chat(self):
fmessage_node = self.root.GetByPath("threads_db2")
if fmessage_node is None:
return
db = SQLiteParser.Database.FromNode(fmessage_node, canceller)
if db is None and "messages" not in db.Tables:
return
tbs = SQLiteParser.TableSignature("messages")
SQLiteParser.Tools.AddSignatureToTable(tbs, "thread_key", SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "msg_type", SQLiteParser.FieldType.Int, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "msg_id", SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull)
fs = self.root.FileSystem
for rec in db.ReadTableDeletedRecords(tbs, False):
if canceller.IsCancellationRequested:
return
send_info = rec["sender"].Value if "sender" in rec else None
msg_type = rec["msg_type"].Value if "msg_type" in rec else None
if "thread_key" in rec and rec["thread_key"].Value.find("ONE_TO_ONE") != -1 and send_info: # 好友聊天
message = model_im.Message()
message.deleted = 1
message.account_id = self.account_id
message.talker_type = 1
message.msg_id = rec["msg_id"].Value if "msg_id" in rec else None
thread_key = rec["thread_key"].Value
talk_id = "FACEBOOK:" + thread_key[len("ONE_TO_ONE:"):len("ONE_TO_ONE:")+len("000000000000000")]
message.talker_id = talk_id
if talk_id in self.contacts_dict:
message.talker_name = self.contacts_dict.get(talk_id)
if "sender" in rec and (not rec["sender"].IsDBNull):
send_info = rec["sender"].Value
if send_info:
send_dict = json.loads(send_info)
if "name" in send_dict:
message.sender_name = send_dict.get("name")
if "user_key" in send_dict:
message.sender_id = send_dict.get("user_key")
if message.sender_id == self.account_id:
message.is_sender = 1
if "text" in rec and (not rec["text"].IsDBNull):
message.content = rec["text"].Value
message.type = 1
if "timestamp_sent_ms" in rec and (not rec["timestamp_sent_ms"].IsDBNull):
message.send_time = int(str(rec["timestamp_sent_ms"].Value)[:-3])
else:
if "timestamp_ms" in rec and (not rec["timestamp_ms"].IsDBNull):
message.send_time = int(str(rec["timestamp_ms"].Value)[:-3])
if "pending_send_media_attachment" in rec and (not rec["pending_send_media_attachment"].IsDBNull):
attachment = rec["pending_send_media_attachment"].Value
if attachment:
attachment_str = json.loads(attachment)
attachment_dict = attachment_str[0]
if "type" in attachment_dict:
tmp_type = attachment_dict["type"]
if tmp_type == "VIDEO":
message.type = 3
elif tmp_type == "PHOTO":
message.type = 2
elif tmp_type == "VIDEO":
message.type = 4
if "uri" in attachment_dict:
media_path = attachment_dict.get("uri")
if media_path.startswith("file:"):
lists = re.split("/", media_path)
media_name = lists[-1]
media_path_list = fs.Search(media_name)
for i in media_path_list:
message.media_path = i.AbsolutePath
elif media_path.startswith("http"):
message.media_path = media_path
if "shares" in rec and (not rec["shares"].IsDBNull):
share_list = json.loads(rec["shares"].Value)
share_dict = share_list[0]
if "media" in share_dict:
media_info = share_dict.get("media")
if "type" in media_info[0] and media_info[0]["type"] == "VIDEO":
message.type = 4
if "type" in media_info[0] and media_info[0]["type"] == "LINK":
message.type = 8
if "href" in share_dict:
message.media_path = share_dict["href"]
if "name" in share_dict:
message.content = share_dict.get("name")
try:
self.facebook_db.db_insert_table_message(message)
except Exception as e:
print(e)
elif send_info == "" and msg_type == -1: # 无效的信息
continue
self.facebook_db.db_commit()
def get_recover_groups_chat(self):
gmessage_node = self.root.GetByPath("threads_db2")
if gmessage_node is None:
return
db = SQLiteParser.Database.FromNode(gmessage_node, canceller)
if db is None and "messages" not in db.Tables:
return
tbs = SQLiteParser.TableSignature("messages")
SQLiteParser.Tools.AddSignatureToTable(tbs, "thread_key", SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "msg_type", SQLiteParser.FieldType.Int, SQLiteParser.FieldConstraints.NotNull)
SQLiteParser.Tools.AddSignatureToTable(tbs, "msg_id", SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull)
fs = self.root.FileSystem
for rec in db.ReadTableDeletedRecords(tbs, False):
if canceller.IsCancellationRequested:
return
send_info = rec["sender"].Value if "sender" in rec else None
msg_type = rec["msg_type"].Value if "msg_type" in rec else None
if "thread_key" in rec and rec["thread_key"].Value.find("GROUP") != -1 and send_info: # 群聊天
message = model_im.Message()
message.deleted = 1
message.account_id = self.account_id
message.talker_type = 2
message.msg_id = rec["msg_id"].Value if "msg_id" in rec else None
thread_key = rec["thread_key"].Value
message.talker_id = thread_key
if "sender" in rec and (not rec["sender"].IsDBNull):
send_info = rec["sender"].Value
if send_info:
send_dict = json.loads(send_dict)
if "name" in send_dict:
message.sender_name = send_dict.get("name")
if "user_key" in send_dict:
message.sender_id = send_dict.get("user_key")
if message.sender_id == self.account_id:
message.is_sender = 1
if "text" in rec and (not recp["Text"].IsDBNull):
message.content = rec["text"].Value
message.type = 1
if "timestamp_sent_ms" in rec and (not rec["timestamp_sent_ms"].IsDBNull):
message.send_time = int(str(rec["timestamp_sent_ms"].Value)[:-3])
else:
if "timestamp_ms" in rec and (not rec["timestamp_ms"].IsDBNull):
message.send_time = int(str(rec["timestamp_ms"].Value)[:-3])
if "pending_send_media_attachment" in rec and (not rec["pending_send_media_attachment"].IsDBNull):
attachment = rec["pending_send_media_attachment"].Value
if attachment:
attachment_str = json.loads(attachment)
attachment_dict = attachment_str[0]
if "type" in attachment_dict:
tmp_type = attachment_dict["type"]
if tmp_type == "VIDEO":
message.type = 3
elif tmp_type == "PHOTO":
message.type = 2
elif tmp_type == "VIDEO":
message.type = 4
if "uri" in attachment_dict:
media_path = attachment_dict.get("uri")
if media_path.startswith("file:"):
lists = re.split("/", media_path)
media_name = lists[-1]
media_path_list = fs.Search(media_name)
for i in media_path_list:
message.media_path = i.AbsolutePath
elif media_path.startswith("http"):
message.media_path = media_path
if "shares" in rec and (not rec["shares"].IsDBNull):
share_list = json.loads(rec["shares"].Value)
share_dict = share_list[0]
if "media" in share_dict:
media_info = share_dict.get("media")
if "type" in media_info[0] and media_info[0]["type"] == "VIDEO":
message.type = 4
if "type" in media_info[0] and media_info[0]["type"] == "LINK":
message.type = 8
if "href" in share_dict:
message.media_path = share_dict["href"]
if "name" in share_dict:
message.content = share_dict.get("name")
try:
self.facebook_db.db_insert_table_message(message)
except Exception as e:
print(e)
elif send_info == "" and msg_type == -1: # 无效的信息
continue
self.facebook_db.db_commit()
def parse(self):
db_path = md5(self.cache, self.root.AbsolutePath)
if self.facebook_db.need_parse(db_path, VERSION_APP_VALUE):
self.facebook_db.db_create(db_path)
self.get_account_id()
self.get_friends()
self.get_friends_chat()
self.get_groups_chat()
self.get_recover_friends_chat()
self.get_recover_groups_chat()
self.facebook_db.db_close()
if not canceller.IsCancellationRequested:
self.facebook_db.db_insert_table_version(model_im.VERSION_KEY_DB, model_im.VERSION_VALUE_DB)
self.facebook_db.db_insert_table_version(model_im.VERSION_KEY_APP, VERSION_APP_VALUE)
generate = model_im.GenerateModel(db_path)
results = generate.get_models()
tmp_dir = ds.OpenCachePath("tmp")
PA_runtime.save_cache_path(bcp_im.CONTACT_ACCOUNT_TYPE_IM_FACEBOOK, db_path, tmp_dir)
return results
def analyze_facebook(node, extractDeleted, extractSource):
pr = ParserResults()
results = Facebook(node, extractDeleted, extractSource).parse()
if results:
pr.Models.AddRange(results)
pr.Build("Facebook")
return pr