-
Notifications
You must be signed in to change notification settings - Fork 8
/
chunsheng.py
391 lines (316 loc) · 16.4 KB
/
chunsheng.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# coding: utf-8
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
from wxpy import *
from requests import get
from requests import post
from platform import system
from random import choice
from threading import Thread
import configparser
import time
import datetime
from weatherapi import get_data, get_content
# 获取每日励志精句
def get_message():
r = get("http://open.iciba.com/dsapi/")
note = r.json()['note']
content = r.json()['content']
return note, content
# 发送消息给她
def send_message(your_message):
try:
# 对方的微信名称
my_friend = bot.friends().search(my_lady_wechat_name)[0]
# 发送消息给对方
my_friend.send(your_message)
except:
# 出问题时,发送信息到文件传输助手
bot.file_helper.send(u"守护" + my_lady_wechat_name + "出问题了,赶紧去看看咋回事~")
# 获取当天星期几
def get_week_day(date):
week_day_dict = {
0: '星期一',
1: '星期二',
2: '星期三',
3: '星期四',
4: '星期五',
5: '星期六',
6: '星期天',
}
day = date.weekday()
return week_day_dict[day]
def get_weather(py_name="china"):
xml_province = get_content(py_name)
city_weather = get_data(xml_province, flag=True)
weather = '上海今天' + city_weather['上海']['stateDetailed'] + ',' + city_weather['上海']['windState'] + ',最高温度' + \
city_weather['上海']['tem1'] + '度、最低温度' + city_weather['上海']['tem2'] + '度。\n\n'
return weather
# 周几提醒 now_week 排班提醒 scheduling_message 天气提醒 get_weather() 早安问候语 choice(str_list_good_jitang)
def remind_schedule(now_week, scheduling_message):
# 每天日程提醒
# now_week = get_week_day(datetime.datetime.now())
# message ="日程提醒:\n"
if now_week == "星期一":
remider = "今天是周一" + scheduling_message + "。\n\n" + get_weather(py_name="china") + choice(
str_list_good_jitang) + ",要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "星期一:%s" % time.ctime())
elif now_week == "星期二":
remider = "今天是周二" + scheduling_message + "。\n\n" + get_weather(py_name="china") + "下午有课,有没有安排时间去听课啊,要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "星期二:%s" % time.ctime())
elif now_week == "星期三":
remider = "今天是周三" + scheduling_message + "。\n\n" + get_weather(py_name="china") + choice(
str_list_good_jitang) + ",要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "周三:%s" % time.ctime())
elif now_week == "星期四":
remider = "周四到了" + scheduling_message + "。\n\n" + get_weather(
py_name="china") + choice(str_list_good_jitang) + ",要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "周四:%s" % time.ctime())
elif now_week == "星期五":
remider = "今天周五啦" + scheduling_message + "。\n\n" + get_weather(
py_name="china") + choice(str_list_good_jitang) + ",要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "周五:%s" % time.ctime())
elif now_week == "星期六":
remider = "今天周六啦" + scheduling_message + "。\n\n" + get_weather(
py_name="china") + choice(str_list_good_jitang) + ",要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "周六:%s" % time.ctime())
elif now_week == "星期天":
remider = "今天是本周的最后一天" + scheduling_message + "。\n\n" + get_weather(
py_name="china") + choice(str_list_good_jitang) + ",要加油哟\n\n"
print("提醒" + my_lady_wechat_name + ":" + remider + "周日:%s" % time.ctime())
return remider
def holiday_greetings(now_time):
# 节日问候语
festival_month = time.strftime('%m', time.localtime())
festival_day = time.strftime('%d', time.localtime())
if (festival_month == '02' and festival_day == '14' and now_time == "08:00"):
send_message(str_Valentine)
print("发送情人节祝福:%s" % time.ctime())
elif (festival_month == '03' and festival_day == '08' and now_time == "08:00"):
send_message(str_Women)
print("发送三八妇女节祝福:%s" % time.ctime())
elif (festival_month == '12' and festival_day == '24' and now_time == "00:00"):
send_message(str_Christmas_Eve)
print("发送平安夜祝福:%s" % time.ctime())
elif (festival_month == '12' and festival_day == '25' and now_time == "00:00"):
send_message(str_Christmas)
print("发送圣诞节祝福:%s" % time.ctime())
# 生日问候语
if (festival_month == birthday_month and festival_day == birthday_day and now_time == "00:00"):
send_message(str_birthday)
print("发送生日祝福:%s" % time.ctime())
# 每日早中晚睡前随机问候语
def time_reminder(now_time, now_week, scheduling_message):
# 待发送的内容,先置为空
message = ""
if (now_time == say_good_morning):
# 随机取一句问候语
message = choice(str_list_good_morning)
# 是否加上随机表情
if (flag_wx_emoj):
message = message + choice(str_list_emoj)
remider = remind_schedule(now_week, scheduling_message)
send_message(remider + message)
print("提醒" + my_lady_wechat_name + "早上起床:%s" % time.ctime())
elif (now_time == say_good_lunch):
if scheduling_message == ',你休息的第一天':
message = "昨晚夜班辛苦了,有没有睡着啊,睡醒了记得找我啊!n(*≧▽≦*)n"
else:
message = choice(str_list_good_lunch)
# 是否加上随机表情
if (flag_wx_emoj):
message = message + choice(str_list_emoj)
send_message(message)
print("提醒" + my_lady_wechat_name + "中午吃饭:%s" % time.ctime())
elif (now_time == say_good_dinner):
message = choice(str_list_good_dinner)
# 是否加上随机表情
if (flag_wx_emoj):
message = message + choice(str_list_emoj)
send_message(message)
print("提醒" + my_lady_wechat_name + "晚上吃饭:%s" % time.ctime())
elif (now_time == say_good_dream):
# 是否在结尾加上每日学英语
if (flag_learn_english):
note, content = get_message()
message = choice(str_list_good_dream) + "\n\n" + "顺便一起来学英语哦:\n" + "原文: " + content + "\n\n翻译: " + note
else:
message = choice(str_list_good_dream)
# 是否加上随机表情
if (flag_wx_emoj):
message = message + choice(str_list_emoj)
remider = ""
if now_week == "星期一":
remider = "明天是周二,记得去听课喔!\n\n"
send_message(remider + message)
print("提醒" + my_lady_wechat_name + "晚上睡觉:%s" % time.ctime())
# 设置上班开始日期 如:2019-5-19,则2019-5-20为白班,2019-5-21为夜班,
# 2019-5-22、2019-5-23为休息日,四天一个轮回,设置为2099时取消设置
def scheduling_reminder(delta_time):
for n in range(365):
if (int(delta_time) - n * 4) == 1:
scheduling_message = ',你上白班'
elif (int(delta_time) - n * 4) == 2:
scheduling_message = ',你上夜班'
elif (int(delta_time) - n * 4) == 3:
scheduling_message = ',你休息的第一天'
elif (int(delta_time) - n * 4) == 4:
scheduling_message = ',你休息的最后一天'
return scheduling_message
# 在规定时间内进行关心她操作
def start_care():
# 待发送的内容,先置为空
# message = ""
# 来个死循环,24小时关心她
while (True):
# 提示
print("守护" + my_lady_wechat_name + "中,时间:%s" % time.ctime())
now_week = get_week_day(datetime.datetime.now())
# 每天定时问候,早上起床,中午吃饭,晚上吃饭,晚上睡觉
# 如果是周一,则晚上睡前提醒,其他则早上提醒
# 获取时间,只获取时和分,对应的位置为倒数第13位到倒数第8位
now_time = time.ctime()[-13:-8]
original_time = time.mktime((2019, 5, 19, 0, 0, 0, 0, 0, 0))
is_rest = True
now_time_sec = time.time()
delta_time = (now_time_sec - original_time) / 84400
# 00:00 为初始化排班时间,一天只初始化一次
if is_rest:
if (now_time == '00:01' or now_time == '09:30'):
scheduling_message = scheduling_reminder(delta_time)
print("初始化排班信息:" + scheduling_message + ",时间:%s" % time.ctime())
if "scheduling_message" in locals().keys():
time_reminder(now_time, now_week, scheduling_message)
else:
time_reminder(now_time, now_week, scheduling_message=",早上好")
holiday_greetings(now_time)
# 每60秒检测一次
time.sleep(60)
if __name__ == "__main__":
# 若发现读取取配置文件出错,可以取消注释下面这行,一般在pycharm环境下才需要增加
# 设置当前文件所在的目录为当前工作路径
# chdir(sys.path[0])
# 启动微信机器人,自动根据操作系统执行不同的指令
# windows系统或macOS Sierra系统使用bot = Bot()
# linux系统或macOS Terminal系统使用bot = Bot(console_qr=2)
if ('Windows' in system()):
# Windows
bot = Bot()
elif ('Darwin' in system()):
# MacOSX
bot = Bot()
elif ('Linux' in system()):
# Linux
bot = Bot(console_qr=2, cache_path=True)
else:
# 自行确定
print("无法识别你的操作系统类型,请自己设置")
# 读取配置文件
cf = configparser.ConfigParser()
cf.read("./config.ini", encoding='UTF-8')
# 设置女友的微信名称,记住,不是微信ID也不是微信备注
# 你女友的微信名称,记住,不是微信ID也不是微信备注
my_lady_wechat_name = cf.get("configuration", "my_lady_wechat_name")
# 设置早上起床时间,中午吃饭时间,下午吃饭时间,晚上睡觉时间
say_good_morning = cf.get("configuration", "say_good_morning")
say_good_lunch = cf.get("configuration", "say_good_lunch")
say_good_dinner = cf.get("configuration", "say_good_dinner")
say_good_dream = cf.get("configuration", "say_good_dream")
# 设置女友生日信息
# 几月,注意补全数字,为两位数,比如6月必须写成06
birthday_month = cf.get("configuration", "birthday_month")
# 几号,注意补全数字,为两位数,比如6号必须写成08
birthday_day = cf.get("configuration", "birthday_day")
# 读取早安问候语,早上起床时间,中午吃饭时间,下午吃饭时间,晚上睡觉时间的随机提示语
# 一般这里的代码不要改动,需要增加提示语可以自己打开对应的文件修改
# 早上起床问候语列表,数据来源于新浪微博
# 晚上睡觉问候语列表,数据来源于新浪微博
str_list_good_jitang = ''
with open("./remind_sentence/sentence_xinlingjitang.txt", "r", encoding='UTF-8') as f:
str_list_good_jitang = f.readlines()
print(str_list_good_jitang)
str_list_good_morning = ''
with open("./remind_sentence/sentence_good_morning.txt", "r", encoding='UTF-8') as f:
str_list_good_morning = f.readlines()
print(str_list_good_morning)
# 中午吃饭问候语列表,数据来源于新浪微博
str_list_good_lunch = ''
with open("./remind_sentence/sentence_good_lunch.txt", "r", encoding='UTF-8') as f:
str_list_good_lunch = f.readlines()
print(str_list_good_lunch)
# 晚上吃饭问候语列表,数据来源于新浪微博
str_list_good_dinner = ''
with open("./remind_sentence/sentence_good_dinner.txt", "r", encoding='UTF-8') as f:
str_list_good_dinner = f.readlines()
print(str_list_good_dinner)
# 晚上睡觉问候语列表,数据来源于新浪微博
str_list_good_dream = ''
with open("./remind_sentence/sentence_good_dream.txt", "r", encoding='UTF-8') as f:
str_list_good_dream = f.readlines()
print(str_list_good_dream)
# 设置晚上睡觉问候语是否在原来的基础上再加上每日学英语精句
# False表示否 True表示是
if ((cf.get("configuration", "flag_learn_english")) == '1'):
flag_learn_english = True
else:
flag_learn_english = False
print(flag_learn_english)
# 设置所有问候语结束是否加上表情符号
# False表示否 True表示是
str_emoj = "(•‾̑⌣‾̑•)✧˖°----(๑´ڡ`๑)----(๑¯ิε ¯ิ๑)----(๑•́ ₃ •̀๑)----( ∙̆ .̯ ∙̆ )----(๑˘ ˘๑)----(●′ω`●)----(●・̆⍛・̆●)----ಥ_ಥ----_(:qゝ∠)----(´;ω;`)----( `)3')----Σ((( つ•̀ω•́)つ----╰(*´︶`*)╯----( ´´ิ∀´ิ` )----(´∩`。)----( ื▿ ื)----(。ŏ_ŏ)----( •ิ _ •ิ )----ヽ(*΄◞ิ౪◟ิ‵ *)----( ˘ ³˘)----(; ´_ゝ`)----(*ˉ﹃ˉ)----(◍'౪`◍)ノ゙----(。◝‿◜。)----(ಠ .̫.̫ ಠ)----(´◞⊖◟`)----(。≖ˇェˇ≖。)----(◕ܫ◕)----(`◕‸◕´+)----(▼ _ ▼)----( ◉ืൠ◉ื)----ㄟ(◑‿◐ )ㄏ----(●'◡'●)ノ♥----(。◕ˇ∀ˇ◕)----( ◔ ڼ ◔ )----( ´◔ ‸◔`)----(☍﹏⁰)----(♥◠‿◠)----ლ(╹◡╹ლ )----(๑꒪◞౪◟꒪๑)"
str_list_emoj = str_emoj.split('----')
if ((cf.get("configuration", "flag_wx_emoj")) == '1'):
flag_wx_emoj = True
else:
flag_wx_emoj = False
print(str_list_emoj)
# 设置节日祝福语
# 情人节祝福语
str_Valentine = cf.get("configuration", "str_Valentine")
print(str_Valentine)
# 三八妇女节祝福语
str_Women = cf.get("configuration", "str_Women")
print(str_Women)
# 平安夜祝福语
str_Christmas_Eve = cf.get("configuration", "str_Christmas_Eve")
print(str_Christmas_Eve)
# 圣诞节祝福语
str_Christmas = cf.get("configuration", "str_Christmas")
print(str_Christmas)
# 她生日的时候的祝福语
str_birthday = cf.get("configuration", "str_birthday")
print(str_birthday)
print(my_lady_wechat_name)
t1 = int(datetime.datetime.strptime('00', '%H').hour)
t2 = int(datetime.datetime.strptime('06', '%H').hour)
print("tuling start time at : " + str(t1)+" o'clock,"+ "and tuling end time at : " + str(t2)+" o'clock")
# 开始守护女友
t = Thread(target=start_care, name='start_care')
t.start()
# 接收女友消息监听器
# 女友微信名
my_girl_friend = bot.friends().search(my_lady_wechat_name)[0]
tuling = Tuling(api_key='340dad825e784010933c9e733ab53169')
@bot.register(chats=[User], msg_types=TEXT, except_self=True)
def print_others(msg):
t0 = int(datetime.datetime.fromtimestamp(time.time()).strftime('%H'))
print(msg)
if int(msg.text.find('春生')) != -1:
tuling.do_reply(msg)
elif ((t0 - t1) >= 0) & ((t0 - t2) <= 0):
tuling.do_reply(msg)
# 输出聊天内容
# 可采用snownlp或者jieba等进行分词、情感分析,由于打包后文件体积太大,故暂时不采用这种方式
# 仅仅是直接调用网络接口
# 做极其简单的情感分析
# 结果仅供参考,请勿完全相信
postData = {'data': msg.text}
response = post('https://bosonnlp.com/analysis/sentiment?analysisType=', data=postData)
data = response.text
# 情感评分指数(越接近1表示心情越好,越接近0表示心情越差)
now_mod_rank = (data.split(',')[0]).replace('[[', '')
print("来自" + my_lady_wechat_name + "的消息:%s\n当前情感得分:%s\n越接近1表示心情越好,越接近0表示心情越差,情感结果仅供参考,请勿完全相信!\n\n" % (
msg.text, now_mod_rank))
# 发送信息到文件传输助手
mood_message = u"来自" + my_lady_wechat_name + "的消息:" + msg.text + "\n当前情感得分:" + now_mod_rank + "\n越接近1表示心情越好,越接近0表示心情越差,情感结果仅供参考,请勿完全相信!\n\n"
bot.file_helper.send(mood_message)