-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanse_contacts.py
129 lines (112 loc) · 2.79 KB
/
cleanse_contacts.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
import csv
import os
from driver import (
db_msg,
)
from config import (
CONTACT_EXPORT_PATH,
)
EXPORT_HEADERS = {
'groups': [
'UserName', # starting with @@
'NickName',
'MemberCount',
# 'self',
# 'MemberList',
# Flags
'HeadImgUpdateFlag',
'ContactFlag',
'ContactType',
'VerifyFlag',
'AttrStatus',
'SnsFlag',
'Uin',
'Statues',
'HeadImgUrl',
# Owner related
'IsOwner',
'ChatRoomOwner',
'EncryChatRoomId',
'PYQuanPin',
'PYInitial',
],
'friends': [
'UserName', # starting with @
'NickName',
'RemarkName',
'KeyWord',
'Sex',
'Province',
'City',
'Signature',
# Flags
'Uin',
'HeadImgFlag',
'ContactFlag',
'ContactType',
'VerifyFlag',
'AttrStatus',
'WebWxPluginSwitch',
'StarFriend',
'SnsFlag',
'HeadImgUrl',
'PYQuanPin',
'PYInitial',
'RemarkPYInitial',
'RemarkPYQuanPin',
],
'mps': [
'UserName', # starting with @
'NickName',
'Province',
'City',
'KeyWord',
'Signature',
# Flags
'Uin',
'HeadImgFlag',
'ContactFlag',
'VerifyFlag',
'AttrStatus',
'HeadImgUrl',
'PYQuanPin',
'PYInitial',
],
}
def dump_contacts():
exists = set()
for i in db_msg.contact_cleansed.find():
exists.add(i['_id'])
for doc in db_msg.contacts.find():
for cate in EXPORT_HEADERS.keys():
# dump to CSV files
# dump_category(
# doc['myself_name'], cate, doc['data'][cate], doc['fetched_day'])
# update in MongoDB
data = []
for i in doc['data'][cate]:
_id = i['UserName']
if _id not in exists:
exists.add(_id)
i['_id'] = _id
data.append(i)
if data:
db_msg.contact_cleansed.insert_many(data)
def dump_category(owner, name, data, day):
# # dump all the headers to pick up valueable ones.
# headers = set()
# for i in data:
# headers.update(i.keys())
headers = EXPORT_HEADERS[name]
filename = os.path.join(
CONTACT_EXPORT_PATH,
'%s-%s-%s.csv' % (owner, day, name)
)
if not os.path.exists(CONTACT_EXPORT_PATH):
os.makedirs(CONTACT_EXPORT_PATH)
with open(filename, 'w', encoding='utf-8') as f_csv:
dict_writer = csv.DictWriter(f_csv, headers, extrasaction='ignore')
dict_writer.writeheader()
dict_writer.writerows(data)
if __name__ == '__main__':
dump_contacts()