-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsync_status_readme.py
More file actions
589 lines (510 loc) · 24.4 KB
/
sync_status_readme.py
File metadata and controls
589 lines (510 loc) · 24.4 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import os
import subprocess
import re
from datetime import datetime, timedelta
import pytz
import logging
# Constants
START_DATE = datetime.fromisoformat(os.environ['START_DATE']).replace(tzinfo=pytz.UTC)
END_DATE = datetime.fromisoformat(os.environ['END_DATE']).replace(tzinfo=pytz.UTC)
DEFAULT_TIMEZONE = 'Asia/Shanghai'
FILE_SUFFIX = '.md'
README_FILE = 'README.md'
FIELD_NAME = 'Name'
NOTES_DIR = 'notes'
Content_START_MARKER = "<!-- Content_START -->"
Content_END_MARKER = "<!-- Content_END -->"
TABLE_START_MARKER = "<!-- START_COMMIT_TABLE -->"
TABLE_END_MARKER = "<!-- END_COMMIT_TABLE -->"
GITHUB_REPOSITORY_OWNER = os.environ.get('GITHUB_REPOSITORY_OWNER')
GITHUB_REPOSITORY = os.environ.get('GITHUB_REPOSITORY')
STATS_START_MARKER = "<!-- STATISTICALDATA_START -->"
STATS_END_MARKER = "<!-- STATISTICALDATA_END -->"
# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
def print_env():
print(f"""
START_DATE: {START_DATE}
END_DATE: {END_DATE}
DEFAULT_TIMEZONE: {DEFAULT_TIMEZONE}
FILE_SUFFIX: {FILE_SUFFIX}
README_FILE: {README_FILE}
FIELD_NAME: {FIELD_NAME}
NOTES_DIR: {NOTES_DIR}
Content_START_MARKER: {Content_START_MARKER}
Content_END_MARKER: {Content_END_MARKER}
TABLE_START_MARKER: {TABLE_START_MARKER}
TABLE_END_MARKER: {TABLE_END_MARKER}
""")
def print_variables(*args, **kwargs):
def format_value(value):
if isinstance(value, str) and ('\n' in value or '\r' in value):
return f'"""\n{value}\n"""'
return repr(value)
variables = {}
for arg in args:
if isinstance(arg, dict):
variables.update(arg)
else:
variables[arg] = eval(arg)
variables.update(kwargs)
for name, value in variables.items():
print(f"{name}: {format_value(value)}")
def get_date_range():
return [START_DATE + timedelta(days=x) for x in range((END_DATE - START_DATE).days + 1)]
def get_user_timezone(file_content):
yaml_match = re.search(r'---\s*\ntimezone:\s*([^\n]+)\s*\n---', file_content)
if yaml_match:
timezone_str = yaml_match.group(1).strip()
# 1) Try IANA timezone names directly (e.g., "Asia/Kolkata")
try:
return pytz.timezone(timezone_str)
except pytz.exceptions.UnknownTimeZoneError:
pass
# 2) Support UTC/GMT fixed offsets like UTC+5:30, UTC-3, UTC+0530, GMT+9, etc.
s = timezone_str.strip().upper().replace("UTC ", "UTC").replace("GMT ", "GMT")
# Special case: plain UTC/GMT
if s in ("UTC", "GMT", "Z"):
return pytz.UTC
# Match formats: UTC+H, UTC+HH, UTC+H:MM, UTC+HH:MM, UTC+HHMM (and GMT variants)
m = re.match(r'^(?:UTC|GMT)\s*([+-])\s*(\d{1,2})(?::?(\d{2}))?$', s)
if m:
sign = 1 if m.group(1) == '+' else -1
hours = int(m.group(2))
minutes = int(m.group(3)) if m.group(3) else 0
total_minutes = sign * (hours * 60 + minutes)
return pytz.FixedOffset(total_minutes)
# Match decimal hours like UTC+5.5 or UTC-3.75
m = re.match(r'^(?:UTC|GMT)\s*([+-])\s*(\d{1,2})\.(\d+)$', s)
if m:
sign = 1 if m.group(1) == '+' else -1
hours = int(m.group(2))
frac = float('0.' + m.group(3))
minutes = int(round(frac * 60))
total_minutes = sign * (hours * 60 + minutes)
return pytz.FixedOffset(total_minutes)
logging.warning(f"Invalid timezone format: {timezone_str}. Using default {DEFAULT_TIMEZONE}.")
return pytz.timezone(DEFAULT_TIMEZONE)
return pytz.timezone(DEFAULT_TIMEZONE)
def extract_content_between_markers(file_content):
start_index = file_content.find(Content_START_MARKER)
end_index = file_content.find(Content_END_MARKER)
if start_index == -1 or end_index == -1:
logging.warning("Content_START_MARKER markers not found in the file")
return ""
return file_content[start_index + len(Content_START_MARKER):end_index].strip()
def find_date_in_content(content, local_date):
date_patterns = [
r'#\s*' + local_date.strftime("%Y.%m.%d"),
r'##\s*' + local_date.strftime("%Y.%m.%d"),
r'###\s*' + local_date.strftime("%Y.%m.%d"),
r'#\s*' + local_date.strftime("%Y.%m.%d").replace('.0', '.'),
r'##\s*' + local_date.strftime("%Y.%m.%d").replace('.0', '.'),
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("%m.%d").lstrip('0').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("%Y/%m/%d"),
r'###\s*' + local_date.strftime("%Y/%m/%d"),
r'#\s*' + local_date.strftime("%m/%d").lstrip('0').replace('/0', '/'),
r'##\s*' + local_date.strftime("%m/%d").lstrip('0').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("%Y-%m-%d"),
r'###\s*' + local_date.strftime("%Y-%m-%d"),
r'#\s*' + local_date.strftime("%m.%d").zfill(5),
r'##\s*' + local_date.strftime("%m.%d").zfill(5),
r'###\s*' + local_date.strftime("%m.%d").zfill(5)
]
combined_pattern = '|'.join(date_patterns)
return re.search(combined_pattern, content)
def get_content_for_date(content, start_pos):
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:
return content[start_pos:start_pos + next_date_match.start()]
return content[start_pos:]
def get_local_day_bounds_for_label(label_date, user_tz):
"""
Given a program label date (UTC tz-aware datetime) and a user's timezone,
compute the start and end of that calendar day in the user's local time.
Example: label 2024-10-17 and Asia/Shanghai ->
local_start = 2024-10-17 00:00+08:00, local_end = 2024-10-18 00:00+08:00.
"""
try:
local_start_naive = datetime(label_date.year, label_date.month, label_date.day, 0, 0, 0)
local_start = user_tz.localize(local_start_naive)
except Exception:
# Fallback: construct with tzinfo if localize is unavailable
local_start = datetime(label_date.year, label_date.month, label_date.day, 0, 0, 0, tzinfo=user_tz)
local_end = local_start + timedelta(days=1)
return local_start, local_end
def check_md_content(file_content, date, user_tz):
"""
修复后的内容检查函数 - 直接使用 UTC 日期匹配
"""
try:
content = extract_content_between_markers(file_content)
# Use the label date directly for matching (the table header uses the same
# calendar date regardless of timezone).
label_date = date.replace(hour=0, minute=0, second=0, microsecond=0)
current_date_match = find_date_in_content(content, label_date)
if not current_date_match:
logging.info(f"No match found for date {label_date.strftime('%Y-%m-%d')}")
return False
date_content = get_content_for_date(content, current_date_match.end())
date_content = re.sub(r'\s', '', date_content)
logging.info(f"Content length for {label_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, _ = get_user_file_path(nickname)
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)} user_tz: {user_tz}")
now_local = datetime.now(user_tz)
for date in get_date_range():
# Keyed by UTC midnight for consistency across the script
start_utc = date.replace(hour=0, minute=0, second=0, microsecond=0)
# Use the user's local calendar day boundaries for the label date
start_local, end_local = get_local_day_bounds_for_label(date, user_tz)
if now_local < start_local:
# Future day for this user
user_status[start_utc] = " "
elif now_local >= end_local:
# Past day in user's local time
user_status[start_utc] = "✅" if check_md_content(file_content, date, user_tz) else "⭕️"
else:
# In-progress (today for this user): show ✅ if already posted, else blank
user_status[start_utc] = "✅" 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 get_date_range()}
except Exception as e:
logging.error(f"Unexpected error processing file for {nickname}: {str(e)}")
user_status = {date: "⭕️" for date in get_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(pytz.UTC).date() in [
date.date() for date in get_date_range()] and d <= min(local_date, current_date)]
missing_days = sum(1 for d in week_dates if user_status.get(datetime.combine(
d.astimezone(pytz.UTC).date(), datetime.min.time()).replace(tzinfo=pytz.UTC), "⭕️") == "⭕️")
if local_date == current_date and missing_days > 2:
return "❌"
elif local_date < current_date and missing_days > 2:
return "❌"
elif local_date > current_date:
return " "
else:
return user_status.get(datetime.combine(date.date(), datetime.min.time()).replace(tzinfo=pytz.UTC), "⭕️")
except Exception as e:
logging.error(f"Error in check_weekly_status: {str(e)}")
return "⭕️"
def get_all_user_files():
exclude_prefixes = ('template', 'readme')
users = {}
if os.path.isdir(NOTES_DIR):
for f in os.listdir(NOTES_DIR):
if not f.lower().endswith(FILE_SUFFIX.lower()):
continue
if f.lower().startswith(exclude_prefixes):
continue
users[f[:-len(FILE_SUFFIX)]] = os.path.join(NOTES_DIR, f)
for f in os.listdir('.'):
if not f.lower().endswith(FILE_SUFFIX.lower()):
continue
if f.lower().startswith(exclude_prefixes):
continue
user = f[:-len(FILE_SUFFIX)]
if user not in users:
users[user] = f
return list(users.keys())
def get_user_file_path(nickname: str):
notes_path = os.path.join(NOTES_DIR, f"{nickname}{FILE_SUFFIX}")
root_path = f"{nickname}{FILE_SUFFIX}"
if os.path.exists(notes_path):
return notes_path, True
if os.path.exists(root_path):
return root_path, False
return notes_path, True
def extract_name_from_row(row):
match = re.match(r'\|\s*\[([^\]]+)\]\([^)]+\)\s*\|', row)
if match:
return match.group(1).strip()
else:
parts = row.split('|')
if len(parts) > 1:
return parts[1].strip()
return None
def extract_allowed_leave_quota(content, default_quota=2):
"""
从 README 内容中解析“请假规则”下的“每周请假 X 次”并返回 X 作为每周可请假次数。
若未解析到,返回默认值 default_quota(保持向后兼容,默认为 2)。
"""
try:
# 定位到“## 请假规则”标题所在的段落
header_pattern = re.compile(r"^##\s*请假规则\s*$", re.MULTILINE)
header_match = header_pattern.search(content)
if not header_match:
logging.info("请假规则标题未找到,使用默认请假次数")
return default_quota
section_start = header_match.end()
# 找到下一个以“## ”开头的标题,作为本节结束位置
next_header_pattern = re.compile(r"^##\s+", re.MULTILINE)
next_header_match = next_header_pattern.search(content, section_start)
section_end = next_header_match.start() if next_header_match else len(content)
section_text = content[section_start:section_end]
# 解析“每周请假 3 次”中的数字,允许可选空格
quota_match = re.search(r"每周请假\s*([0-9]+)\s*次", section_text)
if quota_match:
quota = int(quota_match.group(1))
logging.info(f"解析到每周请假次数: {quota}")
return quota
logging.info("未在请假规则中解析到具体次数,使用默认请假次数")
return default_quota
except Exception as e:
logging.error(f"解析请假规则失败: {str(e)},使用默认请假次数")
return default_quota
def update_readme(content):
try:
start_index = content.find(TABLE_START_MARKER)
end_index = content.find(TABLE_END_MARKER)
if start_index == -1 or end_index == -1:
logging.error("Error: Couldn't find the table markers in README.md")
return content
# 读取README中的请假次数配置
allowed_leave_quota = extract_allowed_leave_quota(content, default_quota=2)
new_table = [
f'{TABLE_START_MARKER}\n',
f'| {FIELD_NAME} | ' + ' | '.join(date.strftime("%m.%d").lstrip('0')
for date in get_date_range()) + ' |\n',
'| ------------- | ' + ' | '.join(['----' for _ in get_date_range()]) + ' |\n'
]
existing_users = set()
table_rows = content[start_index + len(TABLE_START_MARKER):end_index].strip().split('\n')[2:]
for row in table_rows:
user_name = extract_name_from_row(row)
if user_name:
existing_users.add(user_name)
new_table.append(generate_user_row(user_name, allowed_leave_quota))
else:
logging.warning(f"Skipping invalid row: {row}")
new_users = set(get_all_user_files()) - existing_users
for user in new_users:
if user.strip():
new_table.append(generate_user_row(user, allowed_leave_quota))
logging.info(f"Added new user: {user}")
else:
logging.warning(f"Skipping empty user: '{user}'")
new_table.append(f'{TABLE_END_MARKER}\n')
return content[:start_index] + ''.join(new_table) + content[end_index + len(TABLE_END_MARKER):]
except Exception as e:
logging.error(f"Error in update_readme: {str(e)}")
return content
def generate_user_row(user, allowed_leave_quota):
user_status = get_user_study_status(user)
owner, repo = get_repo_info()
file_path, in_notes = get_user_file_path(user)
if owner and repo:
if in_notes:
repo_url = f"https://github.com/{owner}/{repo}/blob/main/{NOTES_DIR}/{user}{FILE_SUFFIX}"
else:
repo_url = f"https://github.com/{owner}/{repo}/blob/main/{user}{FILE_SUFFIX}"
else:
# Fallback to local if repo info is unavailable
repo_url = f"{NOTES_DIR}/{user}{FILE_SUFFIX}" if in_notes else f"{user}{FILE_SUFFIX}"
# replace the username with a markdown link
user_link = f"[{user}]({repo_url})"
new_row = f"| {user_link} |"
is_eliminated = False
try:
with open(file_path, 'r', encoding='utf-8') as file:
file_content = file.read()
except FileNotFoundError:
logging.error(f"Error: Could not find file {file_path}")
return "| " + user_link + " | " + " ⭕️ |" * len(get_date_range()) + "\n"
user_tz = get_user_timezone(file_content)
now_local = datetime.now(user_tz)
date_range = get_date_range()
for i, date in enumerate(date_range):
# UTC key for this program label day
start_utc = date.astimezone(pytz.UTC).replace(hour=0, minute=0, second=0, microsecond=0)
# Local day boundaries for this label date
start_local, end_local = get_local_day_bounds_for_label(date, user_tz)
# 如果已经被淘汰,后续所有天数都显示空
if is_eliminated:
new_row += " |"
continue
# 如果是未来的日期,显示空
if now_local < start_local:
new_row += " |"
continue
# 计算当前日期属于第几个7天周期
days_from_start = (start_utc.date() - START_DATE.date()).days
week_number = days_from_start // 7
# 计算当前周期的开始和结束日期
cycle_start_day = week_number * 7
cycle_end_day = min(cycle_start_day + 6, len(date_range) - 1)
# 统计当前周期内到今天为止的缺席天数
absent_count = 0
for day_idx in range(cycle_start_day, min(cycle_end_day + 1, i + 1)):
if day_idx < len(date_range):
check_start_utc = date_range[day_idx].astimezone(pytz.UTC).replace(hour=0, minute=0, second=0, microsecond=0)
# End of the label day in user's local timezone
_, check_end_local = get_local_day_bounds_for_label(date_range[day_idx], user_tz)
# Only count days that have fully ended in user's local time
if now_local >= check_end_local:
status = user_status.get(check_start_utc, "⭕️")
if status == "⭕️":
absent_count += 1
# 获取当前日期的状态
current_status = user_status.get(start_utc, "⭕️")
# 如果当前周期缺席超过配置的请假次数,标记为失败
if absent_count > allowed_leave_quota:
is_eliminated = True
new_row += " ❌ |"
else:
new_row += f" {current_status} |"
return new_row + '\n'
def get_repo_info():
if 'GITHUB_REPOSITORY' in os.environ:
full_repo = os.environ['GITHUB_REPOSITORY']
owner, repo = full_repo.split('/')
else:
try:
remote_url = subprocess.check_output(
['git', 'config', '--get', 'remote.origin.url']).decode('utf-8').strip()
if remote_url.startswith('https://github.com/'):
owner, repo = remote_url.split('/')[-2:]
elif remote_url.startswith('git@github.com:'):
owner, repo = remote_url.split(':')[-1].split('/')
else:
raise ValueError("Unsupported remote URL format")
repo = re.sub(r'\.git$', '', repo)
except subprocess.CalledProcessError:
logging.error("Failed to get repository information from git config")
return None, None
return owner, repo
def calculate_statistics(content):
start_index = content.find(STATS_START_MARKER)
end_index = content.find(STATS_END_MARKER)
if start_index == -1 or end_index == -1:
logging.error("Error: Couldn't find the stats markers in README.md")
return None
stats_content = content[start_index + len(STATS_START_MARKER):end_index].strip()
stats = {
"total_participants": 0,
"eliminated_participants": 0,
"completed_participants": 0,
"perfect_attendance_users": [],
"completed_users": []
}
total_match = re.search(r"- 总参与人数:\s*(\d+)", stats_content)
if total_match:
stats["total_participants"] = int(total_match.group(1))
completed_match = re.search(r"- 完成人数:\s*(\d+)", stats_content)
if completed_match:
stats["completed_participants"] = int(completed_match.group(1))
completed_users_match = re.search(r"- 完成用户:\s*([\w\s,]+)", stats_content)
if completed_users_match:
stats["completed_users"] = [x.strip() for x in completed_users_match.group(1).split(',') if x.strip()]
perfect_attendance_users_match = re.search(r"- 全勤用户:\s*([\w\s,]+)", stats_content)
if perfect_attendance_users_match:
stats["perfect_attendance_users"] = [
x.strip() for x in perfect_attendance_users_match.group(1).split(',') if x.strip()]
eliminated_match = re.search(r"- 淘汰人数:\s*(\d+)", stats_content)
if eliminated_match:
stats["eliminated_participants"] = int(eliminated_match.group(1))
# 移除 Fork人数 统计解析
return stats
def update_statistics(content, stats):
start_index = content.find(STATS_START_MARKER)
end_index = content.find(STATS_END_MARKER)
if start_index == -1 or end_index == -1:
logging.error("Error: Couldn't find the stats markers in README.md")
return content
stats_text = f"""{STATS_START_MARKER}
## 统计数据
- 总参与人数: {stats["total_participants"]}
- 完成人数: {stats["completed_participants"]}
- 完成用户: {', '.join(stats['completed_users'])}
- 全勤用户: {', '.join(stats['perfect_attendance_users'])}
- 淘汰人数: {stats["eliminated_participants"]}
- 淘汰率: {stats["total_participants"] and stats["eliminated_participants"] / stats["total_participants"]:.2%}
{STATS_END_MARKER}"""
return content[:start_index] + stats_text + content[end_index + len(STATS_END_MARKER):]
def update_statistics_after_end(content, user_files):
"""在活动结束后更新统计数据"""
current_time = datetime.now(pytz.UTC)
stats = {
"total_participants": len(user_files),
"eliminated_participants": 0,
"completed_participants": 0,
"perfect_attendance_users": [],
"completed_users": []
}
# 从表格中提取用户状态
start_index = content.find(TABLE_START_MARKER)
end_index = content.find(TABLE_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(TABLE_START_MARKER):end_index].strip()
table_rows = table_content.split('\n')[2:] # 跳过表头和分隔行
for row in table_rows:
user_name = extract_name_from_row(row)
if not user_name:
continue
# 检查用户是否被淘汰(行中是否包含 ❌)
is_eliminated = "❌" in row
if is_eliminated:
stats["eliminated_participants"] += 1
else:
# 如果用户没有被淘汰,则认为已完成
stats["completed_users"].append(user_name)
stats["completed_participants"] += 1
# 检查是否全勤(行中只有 ✅ 或空格,没有 ⭕️)
if "⭕️" not in row:
check_marks = row.count("✅")
if check_marks > 0: # 至少有一个打卡记录
stats["perfect_attendance_users"].append(user_name)
return update_statistics(content, stats)
def main():
try:
with open(README_FILE, 'r', encoding='utf-8') as file:
content = file.read()
except FileNotFoundError:
logging.error(f"Error: Could not find file {README_FILE}")
return
current_time = datetime.now(pytz.UTC)
user_files = get_all_user_files()
# 无论如何先更新表格
content = update_readme(content)
# 如果当前时间超过结束日期,计算并更新统计数据
if current_time > END_DATE:
content = update_statistics_after_end(content, user_files)
logging.info("Activity has ended. Final statistics have been calculated and updated.")
else:
# 在活动期间,仍然更新现有统计数据
stats = calculate_statistics(content)
if stats:
content = update_statistics(content, stats)
logging.info(f"Updated {README_FILE} - Activity still in progress")
with open(README_FILE, 'w', encoding='utf-8') as file:
file.write(content)
logging.info(f"Successfully updated {README_FILE}")
if __name__ == "__main__":
main()