Skip to content

Commit b96db2b

Browse files
authored
Merge pull request millylee#29 from millylee/feature/account-name
配置支持可选的 name 字段
2 parents f9e168e + 6c9423c commit b96db2b

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

.github/workflows/checkin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66

77
jobs:
88
checkin:
9-
runs-on: windows-latest
9+
runs-on: windows-2025
1010
environment: production
1111
env:
1212
PYTHONIOENCODING: utf-8

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@
4747

4848
### 4. 多账号配置格式
4949

50-
支持单个与多个
50+
支持单个与多个账号配置,可选 `name` 字段用于自定义账号显示名称:
5151

5252
```json
5353
[
5454
{
55+
"name": "我的主账号",
5556
"cookies": {
5657
"session": "account1_session_value"
5758
},
5859
"api_user": "account1_api_user_id"
5960
},
6061
{
62+
"name": "备用账号",
6163
"cookies": {
6264
"session": "account2_session_value"
6365
},
@@ -66,6 +68,13 @@
6668
]
6769
```
6870

71+
**字段说明**
72+
- `cookies` (必需):用于身份验证的 cookies 数据
73+
- `api_user` (必需):用于请求头的 new-api-user 参数
74+
- `name` (可选):自定义账号显示名称,用于通知和日志中标识账号
75+
76+
如果未提供 `name` 字段,会使用 `Account 1``Account 2` 等默认名称。
77+
6978
接下来获取 cookies 与 api_user 的值。
7079

7180
通过 F12 工具,切到 Application 面板,拿到 session 的值,最好重新登录下,该值 1 个月有效期,但有可能提前失效,失效后报 401 错误,到时请再重新获取。

checkin.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def load_accounts():
4444
if 'cookies' not in account or 'api_user' not in account:
4545
print(f'ERROR: Account {i + 1} missing required fields (cookies, api_user)')
4646
return None
47+
# 如果有 name 字段,确保它不是空字符串
48+
if 'name' in account and not account['name']:
49+
print(f'ERROR: Account {i + 1} name field cannot be empty')
50+
return None
4751

4852
return accounts_data
4953
except Exception as e:
@@ -73,10 +77,17 @@ def save_balance_hash(balance_hash):
7377

7478
def generate_balance_hash(balances):
7579
"""生成余额数据的hash"""
76-
balance_json = json.dumps(balances, sort_keys=True, separators=(',', ':'))
80+
# 将包含 quota 和 used 的结构转换为简单的 quota 值用于 hash 计算
81+
simple_balances = {k: v['quota'] for k, v in balances.items()} if balances else {}
82+
balance_json = json.dumps(simple_balances, sort_keys=True, separators=(',', ':'))
7783
return hashlib.sha256(balance_json.encode('utf-8')).hexdigest()[:16]
7884

7985

86+
def get_account_display_name(account_info, account_index):
87+
"""获取账号显示名称"""
88+
return account_info.get('name', f'Account {account_index + 1}')
89+
90+
8091
def parse_cookies(cookies_data):
8192
"""解析 cookies 数据"""
8293
if isinstance(cookies_data, dict):
@@ -180,7 +191,7 @@ def get_user_info(client, headers):
180191

181192
async def check_in_account(account_info, account_index):
182193
"""为单个账号执行签到操作"""
183-
account_name = f'Account {account_index + 1}'
194+
account_name = get_account_display_name(account_info, account_index)
184195
print(f'\n[PROCESSING] Starting to process {account_name}')
185196

186197
# 解析账号配置
@@ -308,27 +319,31 @@ async def main():
308319
if not success:
309320
should_notify_this_account = True
310321
need_notify = True
311-
print(f'[NOTIFY] Account {i + 1} failed, will send notification')
322+
account_name = get_account_display_name(account, i)
323+
print(f'[NOTIFY] {account_name} failed, will send notification')
312324

313325
# 收集余额数据
314326
if user_info and user_info.get('success'):
315327
current_quota = user_info['quota']
316-
current_balances[account_key] = current_quota
328+
current_used = user_info['used_quota']
329+
current_balances[account_key] = {'quota': current_quota, 'used': current_used}
317330

318331
# 只有需要通知的账号才收集内容
319332
if should_notify_this_account:
333+
account_name = get_account_display_name(account, i)
320334
status = '[SUCCESS]' if success else '[FAIL]'
321-
account_result = f'{status} Account {i + 1}'
335+
account_result = f'{status} {account_name}'
322336
if user_info and user_info.get('success'):
323337
account_result += f'\n{user_info["display"]}'
324338
elif user_info:
325339
account_result += f'\n{user_info.get("error", "Unknown error")}'
326340
notification_content.append(account_result)
327341

328342
except Exception as e:
329-
print(f'[FAILED] Account {i + 1} processing exception: {e}')
343+
account_name = get_account_display_name(account, i)
344+
print(f'[FAILED] {account_name} processing exception: {e}')
330345
need_notify = True # 异常也需要通知
331-
notification_content.append(f'[FAIL] Account {i + 1} exception: {str(e)[:50]}...')
346+
notification_content.append(f'[FAIL] {account_name} exception: {str(e)[:50]}...')
332347

333348
# 检查余额变化
334349
current_balance_hash = generate_balance_hash(current_balances) if current_balances else None
@@ -351,11 +366,12 @@ async def main():
351366
for i, account in enumerate(accounts):
352367
account_key = f'account_{i + 1}'
353368
if account_key in current_balances:
369+
account_name = get_account_display_name(account, i)
354370
# 只添加成功获取余额的账号,且避免重复添加
355-
account_result = f'[BALANCE] Account {i + 1}'
356-
account_result += f'\n:money: Current balance: ${current_balances[account_key]}'
371+
account_result = f'[BALANCE] {account_name}'
372+
account_result += f'\n:money: Current balance: ${current_balances[account_key]["quota"]}, Used: ${current_balances[account_key]["used"]}'
357373
# 检查是否已经在通知内容中(避免重复)
358-
if not any(f'Account {i + 1}' in item for item in notification_content):
374+
if not any(account_name in item for item in notification_content):
359375
notification_content.append(account_result)
360376

361377
# 保存当前余额hash

0 commit comments

Comments
 (0)