-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
280 lines (214 loc) · 7.78 KB
/
parse.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
# Script accepts XML files as backups of SMS provided by Titanium Backup
# Script also takes contacts.db file as a source for contacts name.
# Ideally it is also extracted from Titanium Backup of Contacts.
print("\n Booting...\n \n")
# Import modules
import os
import json
from bs4 import BeautifulSoup
import sqlite3
from sqlite3 import Error
import datetime, time
import dateutil.parser
# Use phone contacts, based on contacts.db provided by TitaniumBackup
def parse_contacts_db(db_file):
try:
conn = sqlite3.connect(db_file)
print(" (successfully read)")
return conn
except Error as e:
print(" (" + str(e) + ")\n")
return None
def initDB():
for file in os.listdir( os.getcwd() ):
if file.endswith('.db'):
db = file
break
if not 'db' in locals():
print(" No contacts database file found \n")
return None
print(" Found contacts database:", db, end="")
# try database connection
global conn
conn = parse_contacts_db(db)
# Time converter
def parseTime(t):
pass
return int( time.mktime( dateutil.parser.parse(t).timetuple() ) )
# The core of the script. simply wrapped up in a function due cleaner code
def parseXML():
# Find available XML files
files = []
for file in os.listdir(os.getcwd()):
if file.endswith(".xml"):
files.append(file)
print("\n", len(files), "XML files have been found.")
# Init variables
global messages, messagesOut, filesCounter, totalCounter, skipCounter, sizeXML, numbersCounter
messages = []
messagesOut = []
filesCounter, totalCounter, skipCounter, sizeXML, numbersCounter = (0,) * 5
# Parse each file
for file in files:
# Increment counter
filesCounter += 1
msgTmp = []
# Read the file
try:
theFile = open(file, 'r', encoding="utf8")
tmp = BeautifulSoup(theFile, 'xml')
except UnicodeDecodeError as e:
print(" There is a problem with file encoding")
print(" Error:", e)
exit()
sizeXML += os.path.getsize(file)
# Find all threads
threads = tmp.find_all("thread")
print("\n Parsing XML #" + str(filesCounter), " Length:", len(threads), "")
# Parse each thread
for index, thread in enumerate(threads[25:]):
# Temporary var for parsing thread
display_name, raw_contact_id = '', ''
number = thread['address'].strip().replace(' ', '')
# Min match is the last 7 digits of the number in reversed sequenced,
# it is used as more reliable way to find numbers in Android db.
# It will also be used here to filter for contact name in db and later for duplicates.
min_match = str(number[::-1][:7])
# Cancel if not a proper person number (to clean some of the service/operator info threads)
if len(number) < 9:
skipCounter += 1
continue
msg = {
'name' : '',
'number': number,
'min_match' : min_match,
'conversations': []
}
# Try to find a name for a contact
if 'conn' in globals():
# print(' Looking for contact name')
try:
cur = conn.cursor()
# Using full match
# query = "SELECT raw_contact_id FROM phone_lookup WHERE normalized_number = '" + str(number) + "'"
# Using min match,( uses Reversed last 7 digits of the number)
query = "SELECT raw_contact_id FROM phone_lookup WHERE min_match = '" + min_match + "'"
# print(" Querying:", query)
cur.execute(query)
raw_contact_id = cur.fetchall()
except Error as e:
print("\n An Error Has Occurred during first phone lookup:", e,)
if 'raw_contact_id' in locals() and raw_contact_id:
try:
query = "SELECT display_name FROM raw_contacts WHERE _id = '" + str( raw_contact_id[0][0] ) + "'"
# print(" Querying:", query)
cur.execute(query)
display_name = cur.fetchall()[0][0]
msg['name'] = display_name
numbersCounter += 1
except Error as e:
print("\n An Error Has Occurred during second phone lookup:", e, "\n")
else:
# print(' No contact name found')
pass
# print(" Currently parsing SMS for", str(display_name), "(" + str(number) + ")")
# Find all SMS within current thread
smsList = thread.find_all('sms')
# Cancel if there is no SMS
if len(smsList) > 0:
# Iterate through sms and store in thread var
for index, sms in enumerate(smsList):
totalCounter += 1
msg['conversations'].append({
'type': sms['msgBox'],
'body': sms.contents[0],
'encoding': sms['encoding'],
'timestamp': int(parseTime(sms['date']))
})
# Done parsing thread, sort and push to temp file var
# msg['conversations'] = sorted( msg['conversations'], key=lambda k: k['date'] )
msgTmp.append( msg )
else:
# Track empty threads
skipCounter += 1
# Store parsed file to global messages
messages.append(msgTmp)
# Close working file
theFile.close()
# Search function
def search(key, value, dicts):
# return next( (item for item in dicts if item[key] == value), False )
for index, element in enumerate(dicts):
if element[key] == value:
return index
else:
continue
return False
# Clean duplicates
def merge(input, output):
# Another stats var to track merged threads
global nameMatch
nameMatch = 0
print("\n Merging... \n")
for index, colection in enumerate(input):
print(" XML #" + str(index+1), end=" | ")
for thread in colection:
# Search for threads with the same number
checkNum = search("number", thread['number'], output)
# Search for threads with the same contact name, avoid empty ones
if len(thread['name']) == 0:
checkName = False
else:
checkName = search("name", thread['name'], output)
# Conditionally treat search results
if checkNum is not False:
# Number match found
nameMatch += 1
# Add current thread conversations to a found one
for sms in thread['conversations']:
# But avoid messages that are already in there, by timestamp
tmpTry = search("timestamp", sms['timestamp'], output[checkNum]['conversations'])
if tmpTry is False:
output[checkNum]['conversations'].append(sms)
elif checkName is not False:
# Contact name match found
nameMatch += 1
# Add current thread conversations to a found one
for sms in thread['conversations']:
# But avoid messages that are already in there, by timestamp
tmpTry = search("timestamp", sms['timestamp'], output[checkName]['conversations'])
if tmpTry is False:
output[checkName]['conversations'].append(sms)
else:
output.append(thread)
# Just in case, sort it
output[checkName]['conversations'].sort(key=lambda item:item['timestamp'], reverse=True)
print('Done')
def hr():
print(" \n ______________________________________________________________ \n")
# Look for contacts
initDB()
hr()
# Look and parse XML SMSs
parseXML()
hr()
# Merge all parsed threads and possible duplicates
merge(messages, messagesOut)
hr()
# Write info as JSON to a data.json file
dataFile = open("data.json", "w")
dataFile.write(json.dumps(messagesOut))
# dataFile.write(json.dumps(messagesOut, indent=2))
dataFile.close()
# Calc file size stats
sizeData = os.path.getsize("data.json")
diff = sizeXML/sizeData
print("\n Successfully exported to 'data.json' (" + str(round(sizeData/1024/1024, 2)) + " MB) \n")
# Finish up N show some stats
print(" Fun fact, your output is", round(diff, 1), "times smaller!")
hr()
print("\n", len(messages), "files have been parsed containing", totalCounter, "messages \n")
print("", skipCounter, "empty threads have been skipped. \n")
print("", numbersCounter, "numbers recognized \'N", nameMatch, "contacts merged.")
hr()
print("\n Wrapping up with", len(messagesOut), "nicely formated threads. Done! \n\n")