-
Notifications
You must be signed in to change notification settings - Fork 4
/
MemberDistribution.py
123 lines (101 loc) · 3.16 KB
/
MemberDistribution.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
from ProcessInterface import ProcessInterface
from itchat.content import *
from util import *
import itchat
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as pp
class MemberDistribution(ProcessInterface):
def __init__(self):
self.imgDir = 'Temp'
def process(self, msg, type):
if type != TEXT:
return
content = msg['Content']
# if not content.startswith('/v'):
# return
gid = get_gid_by_name('田馥甄官方粉丝交流群')
# gid = get_group_id(msg)
room = itchat.update_chatroom(gid, detailedMember=True)
# ower = room['MemberList'][0]['UserName']
# if msg['ActualUserName'] != ower:
# return
genderSize = [0,0,0] #[male, female, unknow]
ulist = []
province = {}
for p in room['MemberList']:
if p['Sex'] == 1:
genderSize[0] = genderSize[0] + 1
elif p['Sex'] == 2:
genderSize[1] = genderSize[1] + 1
else:
ulist.append(p['NickName'])
genderSize[2] = genderSize[2] + 1
pro = p['Province'] or '未知/国外'
if pro in province:
province[pro][0] = province[pro][0] + 1
province[pro][1].append(p['NickName'])
else:
province[pro] = [1, [p['NickName']]]
proMsg = ''
if content != '/v':
try:
pro = content.split()[1]
if pro in province:
proList = ','.join(province[pro][1])
proMsg = f'{pro}地区共{province[pro][0]}人:“{proList}”'
except Exception as e:
logging.error(e)
pp.figure(figsize=(7, 14))
p1 = pp.subplot(2, 1, 1)
labels = ['男', '女', '未知']
colors = ['lightskyblue', 'pink', 'gray']
if genderSize[2] == 0:
labels.pop()
genderSize.pop()
res = p1.pie(genderSize, labels=labels, colors=colors, labeldistance=1.1, autopct='%3.1f%%', startangle=90)
for font in res[1]:
font.set_fontproperties(FontProp)
p1.axis('equal')
# p1.set_title('群成员男女比例')
# fn1 = generateTmpFileName(self.imgDir)
# pp.savefig(fn1)
# pp.clf()
p2 = pp.subplot(2, 1, 2)
province = sorted(province.items(), key=lambda d: d[1][0], reverse=True)
num = min(10, len(province))
label2 = []
vals2 = []
for i in range(num):
label2.append(province[i][0])
vals2.append(province[i][1][0])
if len(province) > 10:
sum = 0
for p in province[10:]:
sum = sum + p[1][0]
label2.append('其他')
vals2.append(sum)
res2 = p2.pie(vals2, labels=label2, labeldistance=1.1, autopct='%3.1f%%', pctdistance=0.8, startangle=90)
for font in res2[1]:
font.set_fontproperties(FontProp)
p2.axis('equal')
# pp.figure(figsize=(8, 0.4*len(province)))
# y_pos = np.arange(len(province))
# vals = list(province.values())
# ylabel = list(province.keys())
# pp.barh(y_pos, vals, align='center', height=0.4)
# pp.yticks(y_pos, ylabel, fontproperties=self.prop)
# pp.xlabel('人数', fontproperties=self.prop)
# pp.title('群成员省份分布')
fn = generateTmpFileName(self.imgDir)
pp.savefig(fn)
itchat.send_image(fn, gid)
if proMsg:
itchat.send(proMsg, gid)
#发送未设置性别的帐号
return
if len(ulist) == 1:
itchat.send(f'所以“{ulist[0]}”,你的性别是?', gid)
elif len(ulist) > 1:
ulist = ','.join(ulist)
itchat.send(f'所以“{ulist}”,你们的性别是?', gid)