-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
167 lines (146 loc) · 5.31 KB
/
utils.py
File metadata and controls
167 lines (146 loc) · 5.31 KB
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
import json
from datetime import datetime, timedelta
import random
import os
def load_json(file_name) -> dict:
path = os.path.join(os.path.dirname(__file__), file_name)
if not os.path.exists(path):
return {}
with open(path, encoding='utf8') as f:
data = json.load(f)
return data
def save_json(file_name, args):
path = os.path.join(os.path.dirname(__file__), file_name)
with open(path, 'w', encoding='utf8') as f:
json.dump(args , f, ensure_ascii=False, indent=2)
async def get_luogu_newest_news():
now = datetime.now()
year = now.year
month = now.month
return await get_luogu_news_condition(year = year, month = month)
async def get_luogu_news_condition(year:int = 0, month:int = 0):
news = load_json("luogu_news.json")
ret = [new for new in news.values() if (not year or new['year'] == year) and (not month or new['month'] == month)]
return ret
async def get_luogu_random_news():
news = load_json("luogu_news.json")
return news[random.choice(list(news.keys()))]
async def get_contest(file_name, is_ok):
contests = load_json(file_name)
result = []
for contest in contests:
start_time = datetime.strptime(contest['start_time'], "%Y-%m-%d %H:%M")
end_time = datetime.strptime(contest['end_time'], "%Y-%m-%d %H:%M")
link = contest['link']
if is_ok(start_time, end_time, link):
result.append(contest)
return result
async def get_upcoming_contest(file_name):
now = datetime.now()
if isinstance(file_name, str):
return await get_contest(file_name, lambda start, end, link: now < start)
else:
result = []
for file in file_name:
result.extend(await get_contest(file, lambda start, end, link: now < start))
return result
async def get_now_contest(file_name):
now = datetime.now()
if isinstance(file_name, str):
return await get_contest(file_name, lambda start, end, link: now >= start and now < end)
else:
result = []
for file in file_name:
result.extend(await get_contest(file, lambda start, end, link: now >= start and now < end))
return result
async def get_today_contest(file_name):
now = datetime.now()
today = now - timedelta(hours=now.hour, minutes=now.minute, seconds=now.second)
tomorrow = today + timedelta(days=1)
if isinstance(file_name, str):
return await get_contest(file_name, lambda start, end, link: start >= today and end > now and start < tomorrow)
else:
result = []
for file in file_name:
result.extend(await get_contest(file, lambda start, end, link: start >= today and end > now and start < tomorrow))
return result
async def get_tomorrow_contest(file_name):
now = datetime.now()
tomorrow = now + timedelta(days=1) - timedelta(hours=now.hour, minutes=now.minute, seconds=now.second)
ttomorrow = tomorrow + timedelta(days=1)
if isinstance(file_name, str):
return await get_contest(file_name, lambda start, end, link: start >= tomorrow and start < ttomorrow)
else:
result = []
for file in file_name:
result.extend(await get_contest(file, lambda start, end, link: start >= tomorrow and start < ttomorrow))
return result
def get_luogu_news_text(news):
ret = ""
if not isinstance(news, list):
news = [news]
last_year = 0
last_month = 0
for new in news:
prefix = "" if new['year'] == last_year and new['month'] == last_month else f"{new['year']}年 {new['month']}月"
text = f'''
{prefix}
{new['title']}
{new['url']}
'''.strip()
ret += text + "\n\n"
last_year = new['year']
last_month = new['month']
if not ret:
ret = "无"
return ret.strip()
def getText(msg):
text = ""
for info in msg:
name = info['name']
start_time = info['start_time']
end_time = info['end_time']
link = info['link']
text += f"比赛名称:{name}"
text += f"\n开始时间:{start_time}"
if info['end_time']:
text += f"\n结束时间:{end_time}"
text += f"\n比赛链接:{link}\n\n"
if text == "":
text = "无"
return text
def get_contest_remind(name, start_time, end_time, link):
text = f'''比赛通知!
比赛:{name}
开始时间:{start_time}
结束时间:{end_time}
比赛链接:{link}
'''.strip()
return text
def get_problem_remind(id, name, date, difficulty, url, content):
text = f'''Leetcode {date} 每日一题!
题目:{id}.{name}
难度:{difficulty}
链接:{url}
'''.strip()
return text
def get_problem_full(id, name, date, difficulty, url, content):
text = f'''Leetcode {date} 每日一题!
题目:{id}.{name}
难度:{difficulty}
链接:{url}
内容:{content}
'''.strip()
return text
def render_forward_msg(msg_list: list, uid=244115379, name='cf搜寻者'):
forward_msg = []
for msg in msg_list:
forward_msg.append({
"type": "node",
"data": {
"name": str(name),
"uin": str(uid),
"content": msg
}
})
return forward_msg