1
1
'''Reads in addresses from DB stored at path, or backup,
2
2
to label phone numbers.'''
3
- from helpers .utils import filterDF
3
+ from helpers .utils import filter_based_on_col
4
4
from os .path import expanduser as eu
5
5
import os
6
6
import pandas as pd
14
14
MO_PTH = '31bb7ba8914766d4ba40d6dfb6113c8b614be442'
15
15
MO_BASE = eu ('~/Library/Application Support/MobileSync/Backup/' )
16
16
17
- def extractContacts (path ):
17
+ def extract_contacts (path ):
18
18
'''Get Contact Data from PHONENUMBER, RECORD Tables. As in icloud_query.py'''
19
19
try :
20
20
ad_db = sqlite3 .connect (path )
@@ -30,60 +30,63 @@ def extractContacts(path):
30
30
print "Non-fatal DB error ON path: " , path
31
31
return {}
32
32
33
- def extractBackupContacts (path ):
34
- '''makes {number: name} dict from iphone backup '''
35
- try :
36
- db = sqlite3 .connect (path )
37
- sql = 'SELECT c15Phone, c0First, c1Last, c6Organization from ABPersonFullTextSearch_content'
38
- jn = pd .read_sql (sql , db )
39
33
40
- jn = jn .applymap (lambda x : '' if x == None else x )
41
- clean = lambda x : filter (lambda y : '0' <= c <= '9' , x )[- 17 :- 7 ]
42
- clist = {x [0 ]: x [1 ] + ' ' + x [2 ] for x in
43
- zip (jn .c15Phone .apply (clean ), jn .c0First , jn .c1Last )}
44
- return clist , True # always return true?
45
- # want better except
46
- except Exception :
47
- print "Error in extractBackupContacts with path: " , path
48
- return {}, False
34
+ def extract_backup_contacts (path ):
35
+ '''makes {number: name} dict from iphone backup '''
36
+ try :
37
+ db = sqlite3 .connect (path )
38
+ sql = 'SELECT c15Phone, c0First, c1Last, c6Organization from ABPersonFullTextSearch_content'
39
+ jn = pd .read_sql (sql , db )
49
40
41
+ jn = jn .applymap (lambda x : '' if x == None else x )
42
+ clean = lambda x : filter (lambda y : '0' <= c <= '9' , x )[- 17 :- 7 ]
43
+ clist = {x [0 ]: x [1 ] + ' ' + x [2 ] for x in
44
+ zip (jn .c15Phone .apply (clean ), jn .c0First , jn .c1Last )}
45
+ return clist , True # always return true?
46
+ # want better except
47
+ except Exception :
48
+ print "Error in extractBackupContacts with path: " , path
49
+ return {}, False
50
50
51
- def groupNames (msg , clist ):
52
- '''Currently unused attempt to aggregate group chats.'''
53
- chats = filterDF (msg , 'chat_id' , lambda x : x .startswith ('chat' ))
54
- gb = chats .groupby ('chat_id' )
55
- tmp = gb .id .agg (lambda x : list (set (x )))
56
- tmp = dict (zip (tmp .index , tmp .values ))
57
- def findName (cid ):
58
- cid = cid .replace ('+1' ,'' )
59
- try :
60
- return clist [cid ].split (' ' )[0 ]
61
- except KeyError :
62
- return cid .rstrip ()
63
- return { k : ',' .join ([findName (x [1 :].lstrip ('1' )) for x in v ]) for k ,v in
51
+
52
+ def name_groupchat (msg , clist ):
53
+ '''Currently unused attempt to aggregate group chats.'''
54
+ chats = filter_based_on_col (msg , 'chat_id' , lambda x : x .startswith ('chat' ))
55
+ gb = chats .groupby ('chat_id' )
56
+ tmp = gb .id .agg (lambda x : list (set (x )))
57
+ tmp = dict (zip (tmp .index , tmp .values ))
58
+ def findName (cid ):
59
+ cid = cid .replace ('+1' ,'' )
60
+ try :
61
+ return clist [cid ].split (' ' )[0 ]
62
+ except KeyError :
63
+ return cid .rstrip ()
64
+ return { k : ',' .join ([findName (x [1 :].lstrip ('1' )) for x in v ]) for k ,v in
64
65
tmp .iteritems ()}
65
66
67
+
66
68
def addresses (msg = []):
67
69
'''create the {number: name} dictionary from contacts app, or phone backup.'''
68
70
success = False # so we only call extractBackupContacts once
69
- contact_list = extractContacts (COMP_PATH )
71
+ contact_list = extract_contacts (COMP_PATH )
70
72
paths = filter (lambda x : MO_PTH in x [2 ], os .walk (MO_BASE ))
71
73
backups = [os .path .join (MO_BASE , x [0 ], MO_PTH ) for x in paths ]
72
74
if os .path .exists (SRCS ):
73
75
backups .extend ([os .path .join (SRCS ,mid ,ENDING ) for mid in os .listdir (SRCS )])
74
76
for bu in backups :
75
77
if not success and MO_PTH in bu :
76
- new_cdict , success = extractBackupContacts (bu )
78
+ new_cdict , success = extract_backup_contacts (bu )
77
79
contact_list .update (new_cdict )
78
80
else :
79
- contact_list .update (extractContacts (bu ))
81
+ contact_list .update (extract_contacts (bu ))
80
82
if not contact_list :
81
83
print "Contacts: checked" , COMP_PATH , backups
82
84
print "NO CONTACTS FOUND"
83
85
#contact_list = groupNames(msg, contact_list)
84
86
return contact_list
85
87
86
- def groupbyContact (msg ):
88
+
89
+ def agg_by_contact (msg ):
87
90
'''Group conversations by contact, and calculate summary stats.
88
91
The data that underlies the scatter plot.'''
89
92
msg ['snt_chars' ] = msg ['is_sent' ] * msg ['msg_len' ]
0 commit comments