-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo.py
139 lines (118 loc) · 3.95 KB
/
do.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
import os
import shutil
import sqlite3
from tqdm import tqdm
import csv
def find_name_from_csv(phone):
def get_list_of_phones(string):
s = string.split(' ::: ')
for i in enumerate(s):
st = i[1]
st = st.replace('-', '')
st = st.replace(' ', '')
s[i[0]] = st[-10:]
return s
with open("contacts.csv", mode="r") as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
else:
for i in range(1, 6):
list_of_phones = get_list_of_phones(row[f'Phone {i} - Value'])
if phone in list_of_phones:
return row['Given Name']
line_count += 1
conn = sqlite3.connect('msgstore.db')
path_to_media = "Media/"
cursor = conn.execute('''
select raw_string
from jid
where server = "s.whatsapp.net"
''')
people = []
for row in cursor:
people.append(row[0])
population = len(people)
# people = people[0:50] # remove this
for person in tqdm(enumerate(people), total=len(people)):
#image
cursor = conn.execute('''
select
_id,
media_mime_type,
substr(
substr(thumb_image,instr(thumb_image,'IMG'),60)
,instr(substr(thumb_image,instr(thumb_image,'IMG'),23),'IMG')
,23)
from `messages`
where
`key_remote_jid` like '%{}%'
and `media_mime_type` like '%image%';
'''.format(person[1]))
images = []
for row in cursor:
try:
images.append(row[2].decode('ascii'))
except:
pass
#video
cursor = conn.execute('''
select
_id,
media_mime_type,
substr(
substr(thumb_image,instr(thumb_image,'VID'),60)
,instr(substr(thumb_image,instr(thumb_image,'VID'),23),'VID')
,23)
from `messages`
where
`key_remote_jid` like '%{}%'
and `media_mime_type` like '%video%';
'''.format(person[1]))
videos = []
for row in cursor:
try:
videos.append(row[2].decode('ascii'))
except:
pass
if len(images) + len(videos)> 0:
try:
name = find_name_from_csv(person[1].replace('@s.whatsapp.net', '').split('91')[1])
except:
pass
if name is None:
name = person[1].strip('@s.whatsapp.net')
try:
os.mkdir(os.path.join(path_to_media, "sorted", name))
except OSError as e:
# print ("Creation of the directory failed \n {}".format(e.strerror))
pass
for image in images:
path_to_file = os.path.join(path_to_media, "WhatsApp Images", image)
# print("Debug: path is {}".format(path_to_image))
try:
is_file = os.path.isfile(path_to_file)
if is_file:
try:
shutil.copyfile(path_to_file, os.path.join(path_to_media, "sorted", name, image))
# print("{} copied in {}".format(image, person[1]))
except:
print ("Copy failed for {}".format(image))
except:
print("couldn't find file")
for video in videos:
path_to_file = os.path.join(path_to_media, "WhatsApp Video", video)
# print("Debug: path is {}".format(path_to_image))
try:
is_file = os.path.isfile(path_to_file)
if is_file:
try:
shutil.copyfile(path_to_file, os.path.join(path_to_media, "sorted", name, video))
# print("{} copied in {}".format(video, person[1]))
except:
print ("Copy failed for {}".format(video))
except:
print("couldn't find file")
conn.close()