-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyze.py
executable file
·149 lines (118 loc) · 4.36 KB
/
analyze.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
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pip3 install matplotlib wxpy jieba wordcloud
from wxpy import *
import matplotlib.pyplot as plot
from matplotlib.font_manager import FontManager, FontProperties
import re
import jieba
from wordcloud import WordCloud
cnFontPath = '/System/Library/Fonts/PingFang.ttc'
cnFont = FontProperties(fname=cnFontPath)
bot = Bot(console_qr=True, cache_path=True)
xiaoi = XiaoI('t0T13ypOyooD', 'peJl3US6B7W0xRr6WxgR')
jiuxiaGroup = ensure_one(bot.groups().search('桃园酒侠'))
IS_BOT = False
def setBot(isBot):
global IS_BOT
IS_BOT = isBot
def getBot():
return IS_BOT
@bot.register(except_self=False)
def botHandler(msg):
if msg.text == 'bot':
msg.chat.send('开启%s机器人模式' % bot.self.name)
setBot(True)
elif msg.text == 'endbot':
msg.chat.send('退出%s机器人模式' % bot.self.name)
setBot(False)
elif getBot():
xiaoi.do_reply(msg)
elif msg.text.find('@'+bot.self.name) >=0 and msg.text.find('help') >= 0:
msg.chat.send("'bot' - 开启%s机器人模式\n'endbot' - 退出%s机器人模式" % (bot.self.name,bot.self.name))
# @bot.register(jiuxiaGroup,except_self=False)
# def receiveGroupMessage(msg):
# if msg.text == 'bot':
# jiuxiaGroup.send('开启肥龙机器人模式')
# setBot(True)
#
# elif msg.text == 'endbot':
# jiuxiaGroup.send('退出肥龙机器人模式')
# setBot(False)
#
# elif getBot():
# xiaoi.do_reply(msg)
#
# elif msg.text.find('@'+bot.self.name) >=0 and msg.text.find('help') >= 0:
# jiuxiaGroup.send("'bot' - 开启肥龙机器人模式\n'endbot' - 退出肥龙机器人模式")
def sendGroup(image, toGroup):
groups = bot.groups().search(toGroup)
if len(groups) > 0:
groups[0].send_image(image)
else:
print('Not Found %s' % toGroup)
def sendFriend(image, toFriend):
friends = bot.groups().search(toFriend)
if len(friends) > 0:
friends[0].send_image(image)
else:
print('Not Found %s' % toFriend)
def sexRatio():
ratio = [0, 0, 0]
for friend in bot.friends():
ratio[friend.sex] += 1
other, male, female = tuple(map(lambda r: r / float(len(bot.friends())), ratio))
print(other, male, female)
plot.figure(1, figsize=[6, 6])
plot.title('The proportion of male and female friends')
plot.pie([other, male, female],
explode=[0, 0.1, 0],
labels=['other', 'male', 'female'],
autopct='%1.2f%%',
pctdistance=0.8,
shadow=True)
sexDistImg = 'sex.png'
plot.savefig(sexDistImg)
plot.close('all')
return sexDistImg
def provinceDist():
dist = {}
for friend in bot.friends():
province = friend.province if friend.province.strip() else 'other'
if province in dist:
dist[province] += 1
else:
dist[province] = 1
plot.figure(1, figsize=[10, 6])
plot.title('\"%s\"的微信朋友地区分布统计' % bot.self.name, fontproperties=cnFont)
plot.xlabel('城市', fontproperties=cnFont)
plot.ylabel('分布人数', fontproperties=cnFont)
x = range(0, len(dist.keys()))
plot.bar(left=x,
height=list(dist.values()),
align='center',
)
plot.xticks(x, list(dist.keys()), fontproperties=cnFont, rotation=90)
for (x, key) in enumerate(dist.keys()):
plot.text(x - 0.25, dist[key] + 1.5, '%d' % dist[key], fontproperties=cnFont, rotation=90)
provinceDistImg = 'province.png'
plot.savefig(provinceDistImg)
plot.close('all')
return provinceDistImg
def signatureWordCloud():
signatureText ="".join(list(map(lambda f: re.compile('<[^>]+>').sub("",f.signature), bot.friends())))
space_split_words = " ".join(jieba.cut(signatureText, cut_all=True))
gen_word_cloud = WordCloud(background_color='white', max_words = 2000, max_font_size=40, random_state=42, font_path=cnFontPath).generate(space_split_words)
word_file = "wordcloud.png"
gen_word_cloud.to_file(word_file)
return word_file
def analyze():
# 发送到群组
# sendGroup(provinceDist(),'桃园酒侠')
# 发给自己的文件小助手
bot.file_helper.send_image(sexRatio())
bot.file_helper.send_image(provinceDist())
bot.file_helper.send_image(signatureWordCloud())
if __name__ == '__main__':
# analyze()
embed()