-
Notifications
You must be signed in to change notification settings - Fork 39
/
days.py
174 lines (158 loc) · 6.69 KB
/
days.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
'''
Author: Thund1r [email protected]
Date: 2022-09-22 14:26:19
LastEditTime: 2023-01-05 22:29:34
Description: 日期数据
Copyright (c) 2022 by Thund1r [email protected], All Rights Reserved.
'''
# -*- coding: utf8 -*-
import config
from datetime import datetime, date, timedelta
from zhdate import ZhDate
import random
nowdatetime = (datetime.utcnow() + timedelta(hours=8))
targetday_list = config.get_list("targetday")
targetname_list = config.get_list("targetname")
beginday_list = config.get_list("beginday")
beginname_list = config.get_list("beginname")
call = config.get("call")
# 计算纪念日
def get_remain(target_day, target_name):
ndt = nowdatetime
today = date(ndt.year, ndt.month, ndt.day)
this_year = datetime.now().year
target_day_year = target_day.split("-")[0]
if target_day_year[0] == "n":
lunar_mouth = int(target_day.split("-")[1])
lunar_day = int(target_day.split("-")[2])
last_date = ZhDate(this_year-1, lunar_mouth,
lunar_day).to_datetime().date()
this_date = ZhDate(this_year, lunar_mouth,
lunar_day).to_datetime().date()
if today <= last_date:
this_date = last_date
else:
solar_month = int(target_day.split("-")[1])
solar_day = int(target_day.split("-")[2])
this_date = date(this_year, solar_month, solar_day)
if today == this_date:
remain_day = 0
remain_tip = f"🌟 {target_name}就是今天啦!"
elif today > this_date:
if target_day_year[0] == "n":
lunar_next_date = ZhDate(
(this_year + 1), lunar_mouth, lunar_day).to_datetime().date()
next_date = date(
lunar_next_date.year, lunar_next_date.month, lunar_next_date.day)
else:
next_date = date(
(this_year + 1), solar_month, solar_day)
remain_day = int(str(next_date.__sub__(today)).split(" ")[0])
remain_tip = f"🗓️ 距离{target_name}还有 {remain_day} 天"
else:
next_date = this_date
remain_day = int(str(next_date.__sub__(today)).split(" ")[0])
remain_tip = f"🗓️ 距离{target_name}还有 {remain_day} 天"
return (remain_tip, remain_day)
# 计算单日
def get_duration(begin_day, begin_name):
ndt = nowdatetime
today = date(ndt.year, ndt.month, ndt.day)
begin_day_year = begin_day.split("-")[0]
if begin_day_year[0] == "n":
lunar_year = int(begin_day_year[1:])
lunar_mouth = int(begin_day.split("-")[1])
lunar_day = int(begin_day.split("-")[2])
begin_date = ZhDate(lunar_year, lunar_mouth,
lunar_day).to_datetime().date()
else:
solar_year = int(begin_day.split("-")[0])
solar_month = int(begin_day.split("-")[1])
solar_day = int(begin_day.split("-")[2])
begin_date = date(solar_year, solar_month, solar_day)
if today == begin_date:
duration_day = 0
duration_tip = f"🌟 {begin_name}就是今天啦!"
elif today > begin_date:
duration_day = int(str(today.__sub__(begin_date)).split(" ")[0])
duration_tip = f"🗓️ {begin_name}已经 {duration_day} 天"
else:
duration_day = int(str(begin_date.__sub__(today)).split(" ")[0])
duration_tip = f"🗓️ 距离{begin_name}还有 {duration_day} 天"
return (duration_tip, duration_day)
# 获取第一个元素
def get_elemzero(elem):
return elem[0]
# 获取第二个元素
def get_elemone(elem):
return elem[1]
# 获取所有日期提醒数据
def get_map_days():
days_list = []
days_tip = ""
target_res = ""
if targetday_list or targetname_list:
if len(targetday_list) == len(targetname_list):
try:
target_res = list(
map(get_remain, targetday_list, targetname_list))
days_list.extend(target_res)
except Exception as e:
print("获取纪念日数据错误,请检查纪念日targetname与targetday填写是否正确", e)
return None
else:
print("获取纪念日数据错误,请检查纪念日targetname与targetday数量是否相等")
else:
print("未配置纪念日")
begin_res = ""
if beginday_list or beginname_list:
if len(beginday_list) == len(beginname_list):
try:
begin_res = list(
map(get_duration, beginday_list, beginname_list))
days_list.extend(begin_res)
except Exception as e:
print("获取单日数据错误,请检查单日beginname与beginday填写是否正确", e)
return None
else:
print("获取单日数据错误,检查单日beginname与beginday数量是否相等")
else:
print("未配置单日")
days_list = list(filter(None, days_list))
if days_list:
days_list.sort(key=get_elemone)
res = list(map(get_elemzero, days_list))
days_tip = "\n".join(res)
return days_tip
# 获取今天
def get_today():
ndt = nowdatetime
d = ndt.strftime("%Y{y}%m{m}%d{d}").format(y='年', m='月', d='日')
w = int(ndt.strftime("%w"))
week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
today_date = f"{d} {week_list[w]} "
now_time = ndt.strftime("%H:%M:%S")
time_tip = ""
if "00:00:00" <= now_time < "06:00:00":
time_tip = "凌晨好"
elif "06:00:00" <= now_time < "09:00:00":
time_tip = "早上好"
elif "09:00:00" <= now_time < "12:00:00":
time_tip = "上午好"
elif "12:00:00" <= now_time < "13:00:00":
time_tip = "中午好"
elif "13:00:00" <= now_time < "18:00:00":
time_tip = "下午好"
elif "18:00:00" <= now_time < "23:59:59":
time_tip = "晚上好"
time_tip = f"{time_tip} ~ {get_emoticon()}"
today_tip = call + time_tip if call else time_tip
return {
"today_date": today_date,
"today_tip": today_tip
}
# 获取随机颜文字
def get_emoticon():
emoticon_list = ["( ̄▽ ̄)~*", "(~ ̄▽ ̄)~", "︿( ̄︶ ̄)︿", "~( ̄▽ ̄)~*", "(o゚▽゚)o", "ヾ(✿゚▽゚)ノ", "٩(๑❛ᴗ❛๑)۶", "ヾ(◍°∇°◍)ノ゙", "ヾ(๑╹◡╹)ノ", "(๑´ㅂ`๑)", "(*´゚∀゚`)ノ", "(´▽`)ノ", "ヾ(●´∀`●)",
"(。◕ˇ∀ˇ◕)", "(≖ᴗ≖)✧", "(◕ᴗ◕✿)", "(❁´◡`❁)*✲゚*", "(๑¯∀¯๑)", "(*´・v・)", "(づ。◕ᴗᴗ◕。)づ", "o(* ̄▽ ̄*)o", "(`・ω・´)", "( • ̀ω•́ )✧", "ヾ(=・ω・=)o", "( ̄3 ̄)a", "(灬°ω°灬)", "ヾ(•ω•`。)", "。◕ᴗ◕。"]
return random.choice(emoticon_list)