-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallHistoryDownload.py
More file actions
174 lines (117 loc) · 4.39 KB
/
CallHistoryDownload.py
File metadata and controls
174 lines (117 loc) · 4.39 KB
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
#!/usr/bin/env python3
#
# Released as open source by NCC Group Plc - http://www.nccgroup.com/
#
# Developed by Sultan Qasim Khan, Sultan.QasimKhan@nccgroup.trust
#
# http://www.github.com/nccgroup/nOBEX
#
# Released under GPLv3, a full copy of which can be found in COPYING.
#
import os, struct, sys
from xml.etree import ElementTree
from xml.dom import minidom
from nOBEX import headers, responses
from nOBEX.common import OBEXError
from nOBEX.xml_helper import parse_xml
from clients.pbap import PBAPClient
import dbus
user_login = os.getlogin()
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object('org.ofono', '/'),
'org.ofono.Manager')
modems = manager.GetModems()
modem = modems[0][0]
modem_mac= modem.split("_",1)[1].replace("_", ":" )
print("Using modem %s" % modem_mac)
print("create_dir")
dest_dir = str("/home/" + user_login + "/.config/CallControl/" + modem_mac + "/")
os.system('mkdir -p ' + dest_dir)
print(dest_dir)
contacts_fetched = str(dest_dir + "call_history.vcf")
contacts_edited = str(dest_dir + "call_history_edited.txt")
contacts_sorted = str(dest_dir + "call_history_sorted.txt")
def dump_xml(element, file_name):
rough_string = ElementTree.tostring(element, 'utf-8')
reparsed = minidom.parseString(rough_string)
pretty_string = reparsed.toprettyxml()
with open(file_name, 'w') as fd:
fd.write('<?xml version="1.0"?>\n<!DOCTYPE vcard-listing SYSTEM "vcard-listing.dtd">\n')
fd.write(pretty_string[23:]) # skip xml declaration
def get_file(c, src_path, dest_path, verbose=True, folder_name=None, book=False):
if verbose:
if folder_name is not None:
print("Fetching %s/%s" % (folder_name, src_path))
else:
print("Fetching %s" % src_path)
if book:
mimetype = b'x-bt/phonebook'
else:
mimetype = b'x-bt/vcard'
hdrs, card = c.get(src_path, header_list=[headers.Type(mimetype)])
with open(dest_path, 'wb') as f:
f.write(card)
def dump_dir(c, src_path, dest_path):
src_path = src_path.strip("/")
try:
os.makedirs(dest_path)
except OSError as e:
pass
src_path_l = src_path.split("/")
for path in src_path_l[0:-1]:
c.setpath(path)
hdrs, cards = c.get(src_path_l[-1], header_list=[headers.Type(b'x-bt/vcard-listing')])
for i in range(len(src_path_l)-1):
c.setpath(to_parent=True)
if len(cards) == 0:
print("WARNING: %s is empty, skipping", src_path)
return
names = []
root = parse_xml(cards)
dump_xml(root, "/".join([dest_path, "listing.xml"]))
for card in root.findall("card"):
names.append(card.attrib["handle"])
c.setpath(src_path)
# get all the files
for name in names:
fname = "/".join([dest_path, name])
try:
get_file(c, name, fname, folder_name=src_path)
except OBEXError as e:
print("Failed to fetch", fname, e)
# return to the root directory
depth = len([f for f in src_path.split("/") if len(f)])
for i in range(depth):
c.setpath(to_parent=True)
def main():
prefix = ""
device_address = modem_mac
c = PBAPClient(device_address)
c.connect()
c.setpath(prefix + "telecom")
get_file(c, "cch.vcf", dest_dir+prefix+"call_history.vcf",
folder_name=prefix+"telecom", book=True)
c.disconnect()
list_of_lists = []
with open(contacts_fetched, 'r') as data, open(contacts_edited, 'w') as outfile:
name = ''
number = ''
if data:
for line in data:
if line.startswith('TEL'):
number = line.split(':')[1].rstrip()
print (number)
if line.startswith('FN;') or line.startswith('FN:'):
name = line.split(':')[1].strip(' ;:\n')
print (name)
if line.startswith('X-IRMC'):
direction = line.split(';')[1].split(':')[0]
time = line.split(':')[1].strip('\n')
print ("%s: %s: %s"%(name, number, direction))
export_str = (time + "& " + direction + ": " + number + "; " + name )
outfile.write(export_str + "\n")
name = number = ''
print("Done!")
return 0
def start():
main()