-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (61 loc) · 1.94 KB
/
main.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
from datetime import datetime
import re
import sys
from time import mktime
header = '''
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="sms.xsl"?>
'''
smses = []
template = '''
<sms protocol="0"
address="{tel}"
date="{timestamp}"
type="{type}"
subject="null"
body="{body}"
toa="null"
sc_toa="null"
service_center="null"
read="1"
status="-1"
locked="0"
readable_date="{readable_date}"
contact_name="(Unknown)"
/>'''
with open(sys.argv[1]) as input:
message_in_progress = False
accum_text = date = direction = from_ = to_ = ''
for line in input:
line = line.strip(' ')
if not re.search(r'\+$', line):
if message_in_progress:
message_in_progress = False
# print('date=%s dir=%s from=%s to=%s' % (date, direction, from_, to_))
# print(accum_text)
smses.append(
template.format(
body=accum_text,
type=1 if direction == 'INBOX' else 2,
readable_date=date,
tel=from_ if direction == 'INBOX' else to_,
timestamp=int(mktime(datetime.strptime(re.sub('\..*$', '', date), '%Y-%m-%d %H:%M:%S').timetuple())*1000)
)
)
continue
data = re.split(r'\s*\|\s*', line)
if not data:
continue
if data[0] and not message_in_progress:
message_in_progress = True
date, direction, from_, to_ = data[:-1]
accum_text = ''
if message_in_progress:
text = data[-1]
accum_text += re.sub(r'(\\r)?\s*\+$', '', text)
with open(sys.argv[2], 'w') as output:
output.write(header)
output.write('<smses count="%d">' % len(smses))
for sms in smses:
output.write(sms)
output.write('</smses>')