-
Notifications
You must be signed in to change notification settings - Fork 7
/
get_morning.py
139 lines (127 loc) · 6.21 KB
/
get_morning.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
import os
import json
import datetime
from .charge import get_img,random_choice
# 重写构造json类
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj,datetime.datetime):
return obj.strftime("%Y-%m-%d %H:%M:%S")
else:
return json.JSONEncoder.default(self,obj)
# 判断早安时间
def judge_mor_time(early_time_tmp, late_time_tmp, now_time):
early_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + f' {early_time_tmp}:00:00', '%Y-%m-%d %H:%M:%S')
late_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + f' {late_time_tmp}:00:00', '%Y-%m-%d %H:%M:%S')
if not now_time >= early_time or not now_time <= late_time:
return False
return True
# 判断多次早安
def judge_have_mor(data, user_id, now_time, interval):
get_up_time = datetime.datetime.strptime(data[str(user_id)]['get_up_time'], '%Y-%m-%d %H:%M:%S')
# 上次起床时间和现在时间相差不超过f'{interval}'小时
if get_up_time + datetime.timedelta(hours = int(interval)) > now_time:
return True
return False
# 判断超级亢奋
def judge_super_get_up(data, user_id, now_time, interval):
sleep_time = datetime.datetime.strptime(data[str(user_id)]['sleep_time'], '%Y-%m-%d %H:%M:%S')
# 上次睡觉时间和现在时间相差不超过f'{interval}'小时
if sleep_time + datetime.timedelta(hours = int(interval)) > now_time:
return True
return False
# 进行早安并更新数据
def morning_and_update(_current_dir, data, user_id, now_time):
# 起床并写数据
sleep_time = datetime.datetime.strptime(data[str(user_id)]['sleep_time'], '%Y-%m-%d %H:%M:%S')
in_sleep = now_time - sleep_time
secs = in_sleep.total_seconds()
day = secs // (3600 * 24)
hour = (secs - day * 3600 * 24) // 3600
minute = (secs - day * 3600 * 24 - hour * 3600) // 60
second = secs - day * 3600 * 24 - hour * 3600 - minute * 60
# 睡觉时间小于24小时就同时给出睡眠时长
in_sleep_tmp = 0
if day == 0:
in_sleep_tmp = str(int(hour)) + '时' + str(int(minute)) + '分' + str(int(second)) + '秒'
data[str(user_id)]['get_up_time'] = now_time
data[str(user_id)]['morning_count'] = int(data[str(user_id)]['morning_count']) + 1
# 判断是今天第几个起床的
data['today_count']['morning'] = int(data['today_count']['morning']) + 1
with open(_current_dir, "w", encoding="UTF-8") as f:
f.write(json.dumps(data, ensure_ascii=False, indent=4, cls=DateEncoder))
return data['today_count']['morning'], in_sleep_tmp,secs
# 返回早安信息
def get_morning_msg(group_id, user_id, sex_str):
# 读取配置文件
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
file = open(current_dir, 'r', encoding = 'UTF-8')
config = json.load(file)
# 读取自定义回复文件
_current_dir = os.path.join(os.path.dirname(__file__), 'word.json')
file= open(_current_dir, 'r', encoding = 'UTF-8')
word_config= json.load(file)
word_data = word_config["moring"]
# 读取早安晚安数据
_current_dir = os.path.join(os.path.dirname(__file__), 'data', f'{group_id}.json')
file = open(_current_dir, 'r', encoding = 'UTF-8')
data = json.load(file)
# 若开启规定时间早安,则判断该时间是否允许早安
now_time = datetime.datetime.now()
if config['morning']['get_up_intime']['enable']:
early_time_tmp = config['morning']['get_up_intime']['early_time']
late_time_tmp = config['morning']['get_up_intime']['late_time']
if not judge_mor_time(early_time_tmp, late_time_tmp, now_time):
word = f'现在不能早安哦,可以早安的时间为{early_time_tmp}时到{late_time_tmp}时'
img = get_img(False)
msg = f"{img}\n{word}"
return msg
# 当数据里有过这个人的信息就判断:
if str(user_id) in list(data.keys()):
# 若关闭连续多次早安,则判断在设定时间内是否多次早安
if not config['morning']['multi_get_up']['enable'] and data[str(user_id)]['get_up_time'] != 0:
interval = config['morning']['multi_get_up']['interval']
if judge_have_mor(data, user_id, now_time, interval):
word = f'{interval}小时内你已经早安过了哦'
img = get_img(False)
msg = f"{img}\n{word}"
return msg
# 若关闭超级亢奋,则判断睡眠时长是否小于设定时间
if not config['morning']['super_get_up']['enable']:
interval = config['morning']['super_get_up']['interval']
if judge_super_get_up(data, user_id, now_time, interval):
word = random_choice(word_data["sleep_little"])
img = get_img(False)
msg = f"{img}\n{word}"
return msg
# 若没有说明他还没睡过觉呢
else:
img = get_img(False)
word = random_choice(word_data["no_sleep"])
msg = f"{img}\n{word}"
return msg
# 当前面条件均符合的时候,允许早安
num, in_sleep,secs= morning_and_update(_current_dir, data, user_id, now_time)
now = int(datetime.datetime.now().strftime("%H"))
if now < 6:
time_result = random_choice(word_data["moring_early"])
img = get_img(False)
elif now >12:
time_result = random_choice(word_data["moring_late"])
img = get_img(False)
else:
time_result = random_choice(word_data["moring_normal"])
img = get_img(True)
if in_sleep == 0:
word = f'{time_result}你是今天第{num}个起床的{sex_str}'
msg = f"{img}\n{word}"
else:
if secs < 3600*4:
time_result += random_choice(word_data["sleep_little"])
img = get_img(False)
elif secs >3600*12:
time_result += random_choice(word_data["sleep_much"])
img = get_img(False)
word = f'{time_result}你的睡眠时长为{in_sleep}。\n你是今天第{num}个起床的{sex_str}'
msg = f"{img}\n{word}"
return msg