1616 get_bot_info ,
1717 get_calendar_info ,
1818)
19+ from everyday_wechat .control .rubbish .atoolbox_rubbish import (
20+ get_atoolbox_rubbish
21+ )
22+
1923from everyday_wechat .utils .db_helper import (
2024 find_perpetual_calendar ,
2125 find_user_city ,
2226 find_weather ,
2327 udpate_user_city ,
2428 udpate_weather ,
25- update_perpetual_calendar
29+ update_perpetual_calendar ,
30+ find_rubbish ,
31+ update_rubbish
2632)
27- # import pysnooper
2833
29- at_compile = re .compile (r'(@.*?\s{1}).*?' )
34+
35+
36+ at_compile = r'(@.*?\s{1,}).*?'
3037tomorrow_compile = r'明[日天]'
31- calendar_complie = r'\s*(?:日|万年)历\s*'
32- weather_compile = r'^(?:\s*天气\s*(\S+?)|\s*(\S*?)\s*天气\s*)$'
33- help_complie = r'^(?:帮忙|帮助|help)\s*$'
34- punct_complie = r'[^a-zA-z\u4e00-\u9fa5]+$' #去除句子最后面的标点
3538
36- common_msg = '@{ated_name}\u2005 {text}'
37- weather_error_msg = '@{ated_name}\u2005 \n 未找到『{city}』城市的相关信息'
39+ punct_complie = r'[^a-zA-z0-9\u4e00-\u9fa5]+$' # 去除句子最后面的标点
40+ help_complie = r'^(?:0|帮忙|帮助|help)\s*$'
41+
42+ weather_compile = r'^(?:\s*(?:1|天气|weather).*?|.*?(?:天气|weather)\s*)$'
43+ weather_clean_compile = r'天气|1|weather|\s'
44+ calendar_complie = r'^\s*(?:2|日历|万年历)'
45+ calendar_date_compile = r'^\s*(19|2[01]\d{2})[\-\s年]+(0?[1-9]|1[012])[\-\/\s月]+(0?[1-9]|[12][0-9]|3[01])[\s日]*$'
46+ rubbish_complie = r'^\s*(?:3|垃圾|rubbish)'
3847
48+ common_msg = '@{ated_name}\u2005 \n {text}'
49+ weather_error_msg = '@{ated_name}\u2005 \n 未找到『{city}』城市的天气信息'
3950
40- help_group_content = """
51+ rubbish_normal = '@{ated_name}\u2005 \n 【查询结果】:『{name}』属于{_type}'
52+ rubbish_other = '@{ated_name}\u2005 \n 【查询结果】:『{name}』无记录\n 【推荐查询】:{other}'
53+ rubbish_nothing = '@{ated_name}\u2005 \n 【查询结果】:『{name}』无记录'
54+
55+ help_group_content = """@{ated_name}
4156群助手功能:
42- 1.输入:天气+城市名;
43- 2.输入:万年历;
57+ 1.输入:天气+城市名(可空)。例如:天气北京
58+ 2.输入:日历+日期(格式:yyyy-MM-dd 可空)。例如:日历2019-07-03
59+ 3.输入:垃圾+名称。例如:3猫粮
4460更多功能:请输入 help/帮助。
4561"""
4662
63+
64+ # import pysnooper
4765# @pysnooper.snoop()
4866def handle_group_helper (msg ):
4967 """
5068 处理群消息
5169 :param msg:
5270 :return:
5371 """
72+
5473 conf = config .get ('group_helper_conf' )
5574 if not conf .get ('is_open' ):
5675 return
@@ -60,9 +79,9 @@ def handle_group_helper(msg):
6079 if conf .get ('is_at' ) and not msg .isAt :
6180 return
6281
63- uuid = msg .fromUserName # 群 uid
64- ated_uuid = msg .actualUserName # 艾特你的用户的uuid
65- ated_name = msg .actualNickName # 艾特你的人的群里的名称
82+ uuid = msg .fromUserName # 群 uid
83+ ated_uuid = msg .actualUserName # 艾特你的用户的uuid
84+ ated_name = msg .actualNickName # 艾特你的人的群里的名称
6685
6786 is_all = conf .get ('is_all' , False )
6887 user_uuids = conf .get ('group_black_uuids' ) if is_all else conf .get ('group_white_uuids' )
@@ -74,12 +93,13 @@ def handle_group_helper(msg):
7493 if not is_all and uuid not in user_uuids :
7594 return
7695 # 去掉 at 标记
77- text = at_compile .sub ('' , text )
96+ text = re .sub (at_compile , '' , text )
7897
7998 # 如果是帮助
80- helps = re .findall (help_complie ,text ,re .I )
99+ helps = re .findall (help_complie , text , re .I )
81100 if helps :
82- itchat .send (help_group_content , uuid )
101+ retext = help_group_content .format (ated_name = ated_name )
102+ itchat .send (retext , uuid )
83103 return
84104
85105 # 是否是明天,用于日历,天气,星座查询
@@ -91,76 +111,119 @@ def handle_group_helper(msg):
91111 is_tomorrow = False
92112 htext = text
93113
94- htext = re .sub (punct_complie , '' , htext ) # 去句末的标点
114+ htext = re .sub (punct_complie , '' , htext ) # 去句末的标点
95115
96116 # 已开启天气查询,并包括天气关键词
97117 if conf .get ('is_weather' ):
98- citys = re .findall (weather_compile , htext )
99- if citys :
100- for x in citys [0 ]:
101- city = x
102- if city :
103- break
104- else :
118+ if re .findall (weather_compile , htext , re .I ):
119+ city = re .sub (weather_clean_compile , '' , text )
120+
121+ if not city : # 如果只是输入城市名
122+ # 从缓存数据库找最后一次查询的城市名
105123 city = find_user_city (ated_uuid )
106- if not city :
107- city = get_city_by_uuid (ated_uuid )
108- if city :
109- _date = datetime .now ().strftime ('%Y-%m-%d' )
110- weather_info = find_weather (_date , city )
111- if weather_info :
112- retext = common_msg .format (ated_name = ated_name , text = weather_info )
113- itchat .send (retext , uuid )
114- return
124+ if not city : # 缓存数据库没有保存,通过用户的资料查城市
125+ city = get_city_by_uuid (ated_uuid )
126+ if not city :
127+ retext = '请输入城市名'
128+ itchat .send (retext , uuid )
129+ return
115130
116- weather_info = get_weather_info (city )
117- if weather_info :
118- # print(ated_name, city, retext)
119- retext = common_msg .format (ated_name = ated_name , text = weather_info )
120- itchat .send (retext , uuid )
131+ _date = datetime .now ().strftime ('%Y-%m-%d' )
132+ weather_info = find_weather (_date , city )
133+ if weather_info :
134+ retext = common_msg .format (ated_name = ated_name , text = weather_info )
135+ itchat .send (retext , uuid )
136+ return
121137
122- data = {
123- '_date' : _date ,
124- 'city_name' : city ,
125- 'weather_info' : weather_info ,
126- 'userid' : ated_uuid ,
127- 'last_time' : datetime .now ()
128- }
129- udpate_weather (data )
130- # userid,city_name,last_time,group_name udpate_weather_city
131- data2 = {
132- 'userid' : ated_uuid ,
133- 'city_name' : city ,
134- 'last_time' : datetime .now ()
135- }
136- udpate_user_city (data2 )
137- return
138- else :
139- weather_error_msg = '@{ated_name}\u2005 \n 未找到『{city}』城市的相关信息'
140- retext = weather_error_msg .format (ated_name = ated_name , city = city )
141- itchat .send (retext , uuid )
142- return
138+ weather_info = get_weather_info (city )
139+ if weather_info :
140+ # print(ated_name, city, retext)
141+ retext = common_msg .format (ated_name = ated_name , text = weather_info )
142+ itchat .send (retext , uuid )
143+
144+ data = {
145+ '_date' : _date ,
146+ 'city_name' : city ,
147+ 'weather_info' : weather_info ,
148+ 'userid' : ated_uuid ,
149+ 'last_time' : datetime .now ()
150+ }
151+ udpate_weather (data )
152+ # userid,city_name,last_time,group_name udpate_weather_city
153+ data2 = {
154+ 'userid' : ated_uuid ,
155+ 'city_name' : city ,
156+ 'last_time' : datetime .now ()
157+ }
158+ udpate_user_city (data2 )
159+ return
160+ else :
161+ retext = weather_error_msg .format (ated_name = ated_name , city = city )
162+ itchat .send (retext , uuid )
163+ return
143164 return
144165
145166 # 已开启日历,并包含日历
146167 if conf .get ('is_calendar' ):
147- if re .findall (calendar_complie , htext ):
148- _date = datetime .now ().strftime ('%Y-%m-%d' )
168+ if re .findall (calendar_complie , htext , re .I ):
169+
170+ calendar_text = re .sub (calendar_complie , '' , htext )
171+ if calendar_text : # 日历后面填上日期了
172+ dates = re .findall (calendar_date_compile , calendar_text )
173+ if not dates :
174+ retext = '请输入正确的日期'
175+ itchat .send (retext , uuid )
176+ return
177+
178+ _date = '{}-{:0>2}-{:0>2}' .format (* dates [0 ]) # 用于保存数据库
179+ rt_date = '{}{:0>2}{:0>2}' .format (* dates [0 ]) # 用于查询日历
180+ else : # 日历 后面没有日期,则默认使用今日。
181+ _date = datetime .now ().strftime ('%Y-%m-%d' )
182+ rt_date = datetime .now ().strftime ('%Y%m%d' )
183+
184+ # 从 数据库缓存中记取内容
149185 cale_info = find_perpetual_calendar (_date )
150186 if cale_info :
151187 retext = common_msg .format (ated_name = ated_name , text = cale_info )
152188 itchat .send (retext , uuid )
153189 return
154190
155- rt_date = datetime . now (). strftime ( '%Y%m%d' )
191+ # 取网络数据
156192 cale_info = get_rtcalendar (rt_date )
157193 if cale_info :
158194 retext = common_msg .format (ated_name = ated_name , text = cale_info )
159195 itchat .send (retext , uuid )
160- update_perpetual_calendar (_date , cale_info )
196+ update_perpetual_calendar (_date , cale_info ) # 保存数据到数据库
161197 return
162198 return
163199
200+ if conf .get ('is_rubbish' ):
201+ if re .findall (rubbish_complie , htext , re .I ):
202+ key = re .sub (rubbish_complie , '' , htext ).strip ()
203+ if not key :
204+ retext = '请填写垃圾名称'
205+ itchat .send (retext , uuid )
206+ return
207+
208+ _type = find_rubbish (key )
209+ if _type :
210+ retext = rubbish_normal .format (ated_name = ated_name , name = key , _type = _type )
211+ itchat .send (retext , uuid )
212+ return
213+ _type , return_list , other = get_atoolbox_rubbish (key )
214+ if _type :
215+ retext = rubbish_normal .format (ated_name = ated_name , name = key , _type = _type )
216+ itchat .send_msg (retext , uuid )
217+ elif other :
218+ retext = rubbish_other .format (ated_name = ated_name , name = key , other = other )
219+ itchat .send_msg (retext , uuid )
220+ else :
221+ retext = rubbish_nothing .format (ated_name = ated_name , name = key )
222+ itchat .send_msg (retext , uuid )
223+ if return_list :
224+ update_rubbish (return_list )
225+ return
226+
164227 # 其他结果都没有匹配到,走自动回复的路
165228 if conf .get ('is_auto_reply' ):
166229 reply_text = get_bot_info (text , ated_uuid ) # 获取自动回复
0 commit comments