-
Notifications
You must be signed in to change notification settings - Fork 4
/
sync_status_readme_backup.py
301 lines (253 loc) · 11.5 KB
/
sync_status_readme_backup.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
import os
import re
from datetime import datetime, timedelta
import pytz
import logging
import time
# 设置日志
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# 设置UTC时区
utc_tz = pytz.UTC
# 定义日期范围(从6月24日到7月14日)
start_date = datetime(2024, 6, 24, tzinfo=utc_tz)
end_date = datetime(2024, 7, 14, tzinfo=utc_tz)
date_range = [start_date + timedelta(days=x)
for x in range((end_date - start_date).days + 1)]
def get_user_timezone(file_content):
# 尝试从 YAML 前置元数据中获取时区
yaml_match = re.search(r'---\s*\ntimezone:\s*(\S+)\s*\n---', file_content)
if yaml_match:
timezone_str = yaml_match.group(1)
try:
return pytz.timezone(timezone_str)
except pytz.exceptions.UnknownTimeZoneError:
logging.warning(
f"Unknown timezone: {timezone_str}. Using default Asia/Shanghai.")
# 如果没找到或无效,返回默认时区(中国标准时间)
return pytz.timezone('Asia/Shanghai')
def check_md_content(file_content, date, user_tz):
try:
# 查找标记之间的内容
start_marker = "<!-- Content_START -->"
end_marker = "<!-- Content_END -->"
start_index = file_content.find(start_marker)
end_index = file_content.find(end_marker)
if start_index == -1 or end_index == -1:
logging.warning("EICL1st markers not found in the file")
return False
# 提取标记之间的内容
content = file_content[start_index +
len(start_marker):end_index].strip()
# 转换日期到用户时区
local_date = date.astimezone(user_tz).replace(
hour=0, minute=0, second=0, microsecond=0)
# 在提取的内容中查找日期
# date_patterns = [
# r'###\s*' + local_date.strftime("%Y.%m.%d"),
# r'###\s*' + local_date.strftime("%Y.%-m.%-d"),
# r'###\s*' + local_date.strftime("%-m.%-d"),
# r'###\s*' + local_date.strftime("%Y/%m/%d"),
# r'###\s*' + local_date.strftime("%-m/%-d")
# ]
date_patterns = [
r'###\s*' + local_date.strftime("%Y.%m.%d"),
r'###\s*' + local_date.strftime("%Y.%m.%d").replace('.0', '.'),
r'###\s*' +
local_date.strftime("%m.%d").lstrip('0').replace('.0', '.'),
r'###\s*' + local_date.strftime("%Y/%m/%d"),
r'###\s*' +
local_date.strftime("%m/%d").lstrip('0').replace('/0', '/')
]
combined_pattern = '|'.join(date_patterns)
current_date_match = re.search(combined_pattern, content)
if not current_date_match:
logging.info(
f"No match found for date {local_date.strftime('%Y-%m-%d')}")
return False
start_pos = current_date_match.end()
next_date_pattern = r'###\s*(\d{4}\.)?(\d{1,2}[\.\/]\d{1,2})'
next_date_match = re.search(next_date_pattern, content[start_pos:])
if next_date_match:
end_pos = start_pos + next_date_match.start()
date_content = content[start_pos:end_pos]
else:
date_content = content[start_pos:]
date_content = re.sub(r'\s', '', date_content)
logging.info(
f"Content length for {local_date.strftime('%Y-%m-%d')}: {len(date_content)}")
return len(date_content) > 10
except Exception as e:
logging.error(f"Error in check_md_content: {str(e)}")
return False
def get_user_study_status(nickname):
user_status = {}
file_name = f"{nickname}_EICL1st.md"
try:
with open(file_name, 'r', encoding='utf-8') as file:
file_content = file.read()
# 获取用户时区
user_tz = get_user_timezone(file_content)
logging.info(
f"File content length for {nickname}: {len(file_content)}")
current_date = datetime.now(user_tz).replace(
hour=0, minute=0, second=0, microsecond=0)
for date in date_range:
local_date = date.astimezone(user_tz).replace(
hour=0, minute=0, second=0, microsecond=0)
if local_date > current_date:
user_status[date] = " " # 未来的日期显示为空白
elif local_date == current_date:
user_status[date] = "✅" if check_md_content(
file_content, date, user_tz) else " " # 当天有内容标记✅,否则空白
else:
user_status[date] = "✅" if check_md_content(
file_content, date, user_tz) else "⭕️"
logging.info(f"Successfully processed file for user: {nickname}")
except FileNotFoundError:
logging.error(f"Error: Could not find file {file_name}")
user_status = {date: "⭕️" for date in date_range}
except Exception as e:
logging.error(
f"Unexpected error processing file for {nickname}: {str(e)}")
user_status = {date: "⭕️" for date in date_range}
return user_status
def check_weekly_status(user_status, date, user_tz):
try:
local_date = date.astimezone(user_tz).replace(hour=0, minute=0, second=0, microsecond=0)
week_start = (local_date - timedelta(days=local_date.weekday()))
week_dates = [week_start + timedelta(days=x) for x in range(7)]
current_date = datetime.now(user_tz).replace(hour=0, minute=0, second=0, microsecond=0)
week_dates = [d for d in week_dates if d.astimezone(utc_tz).date() in [date.date() for date in date_range] and d <= min(local_date, current_date)]
missing_days = 0
for d in week_dates:
date_key = datetime.combine(d.astimezone(utc_tz).date(), datetime.min.time()).replace(tzinfo=utc_tz)
status = user_status.get(date_key, "⭕️")
if status == "⭕️":
missing_days += 1
if d == local_date and d <= current_date:
if missing_days > 2:
return "❌"
else:
return status
if missing_days > 2 and d < local_date:
return "❌"
if local_date > current_date:
return " "
return user_status.get(datetime.combine(date.date(), datetime.min.time()).replace(tzinfo=utc_tz), "⭕️")
except Exception as e:
logging.error(f"Error in check_weekly_status: {str(e)}")
return "⭕️"
def check_overall_status(user_status, date, user_tz):
try:
local_date = date.astimezone(user_tz).replace(
hour=0, minute=0, second=0, microsecond=0)
current_date = datetime.now(user_tz).replace(
hour=0, minute=0, second=0, microsecond=0)
# For today and tomorrow, use timezone-aware logic
if local_date <= current_date + timedelta(days=1):
return check_weekly_status(user_status, date, user_tz)
# For other dates, use UTC-based logic
week_start = (date - timedelta(days=date.weekday()))
week_dates = [week_start + timedelta(days=x) for x in range(7)]
week_dates = [d for d in week_dates if d.date() in [date.date() for date in date_range]
and d <= date]
missing_days = 0
for d in week_dates:
date_key = datetime.combine(d.date(), datetime.min.time()).replace(tzinfo=utc_tz)
status = user_status.get(date_key, "⭕️")
if status == "⭕️":
missing_days += 1
if missing_days > 2:
return "❌" # Mark as 'X' on the third missing day
return user_status.get(date, "⭕️")
except Exception as e:
logging.error(f"Error in check_overall_status: {str(e)}")
return "⭕️"
def get_all_user_files():
return [f.split('_')[0] for f in os.listdir('.') if f.endswith('_EICL1st.md')]
def update_readme(content, start_marker, end_marker):
try:
start_index = content.find(start_marker)
end_index = content.find(end_marker)
if start_index == -1 or end_index == -1:
logging.error(
"Error: Couldn't find the table markers in README.md")
return content
table_content = content[start_index +
len(start_marker):end_index].strip()
table_rows = table_content.split('\n')[2:] # 跳过表头和分隔行
new_table = [
f'{start_marker}\n',
'| EICL1st· Name | ' +
' | '.join(date.strftime("%m.%d").lstrip('0')
for date in date_range) + ' |\n',
'| ------------- | ' +
' | '.join(['----' for _ in date_range]) + ' |\n'
]
existing_users = set()
for row in table_rows:
match = re.match(r'\|\s*([^|]+)\s*\|', row)
if match:
display_name = match.group(1).strip()
existing_users.add(display_name)
user_status = get_user_study_status(display_name)
with open(f"{display_name}_EICL1st.md", 'r', encoding='utf-8') as file:
file_content = file.read()
user_tz = get_user_timezone(file_content)
new_row = f"| {display_name} |"
is_eliminated = False
for date in date_range:
if is_eliminated:
new_row += " |" # 淘汰后的日期保持空白
else:
status = check_weekly_status(
user_status, date, user_tz)
if status == "❌":
is_eliminated = True
new_row += f" {status} |"
new_table.append(new_row + '\n')
else:
logging.warning(f"Skipping invalid row: {row}")
# 添加新用户
all_users = set(get_all_user_files())
new_users = all_users - existing_users
for user in new_users:
user_status = get_user_study_status(user)
with open(f"{user}_EICL1st.md", 'r', encoding='utf-8') as file:
file_content = file.read()
user_tz = get_user_timezone(file_content)
new_row = f"| {user} |"
is_eliminated = False
for date in date_range:
if is_eliminated:
new_row += " |" # 淘汰后的日期保持空白
else:
status = check_weekly_status(user_status, date, user_tz)
if status == "❌":
is_eliminated = True
new_row += f" {status} |"
new_table.append(new_row + '\n')
logging.info(f"Added new user: {user}")
new_table.append(f'{end_marker}\n')
return (
content[:start_index] +
''.join(new_table) +
content[end_index + len(end_marker):]
)
except Exception as e:
logging.error(f"Error in update_readme: {str(e)}")
return content
def main():
try:
with open('README.md', 'r', encoding='utf-8') as file:
content = file.read()
new_content = update_readme(
content, "<!-- START_COMMIT_TABLE -->", "<!-- END_COMMIT_TABLE -->")
with open('README.md', 'w', encoding='utf-8') as file:
file.write(new_content)
logging.info("README.md has been successfully updated.")
except Exception as e:
logging.error(f"An error occurred in main function: {str(e)}")
if __name__ == "__main__":
main()