Skip to content

Commit 9f33888

Browse files
authored
Merge pull request #27 from iamzhz/main
添加:在 Linux 中将 access token 保存到文件 ~/.config/tchMaterial-parser/data.json 中
2 parents 91e8497 + 8e28f19 commit 9f33888

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ https://basic.smartedu.cn/tchMaterial/detail?contentType=assets_document&content
9999
### 2. Access Token 保存在哪里?💾
100100

101101
- **Windows 操作系统**:Token 会存储在**注册表** `HKEY_CURRENT_USER\Software\tchMaterial-parser` 项中的 `AccessToken` 值。
102-
- **Linux/macOS 等操作系统**:Token 仅在运行时临时存储于内存,不会自动保存,程序重启后需重新输入,目前我们正在努力改进该功能。
102+
- **Linux 操作系统**: Token 会存储在 `~/.config/tchMaterial-parser/data.json` 的文件中。
103+
- **macOS 等操作系统**:Token 仅在运行时临时存储于内存,不会自动保存,程序重启后需重新输入,目前我们正在努力改进该功能。
103104

104105
### 3. Token 会不会泄露?🔐
105106

src/tchMaterial-parser.pyw

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import os, platform
1111
from functools import partial
1212
import base64, tempfile
1313
import threading, requests, psutil
14+
import json
1415

1516
os_name = platform.system() # 获取操作系统类型
1617
if os_name == "Windows": # 如果是 Windows 操作系统,导入 Windows 相关库
@@ -261,10 +262,13 @@ def open_access_token_window():
261262
# 重新启用“下载”按钮,并提示用户
262263
download_btn.config(state="normal")
263264

264-
# 在 Windows 上额外提示存储位置
265+
# 在 Windows & Linux 上额外提示存储位置
265266
if os_name == "Windows":
266267
reg_pos = "HKEY_CURRENT_USER\\Software\\tchMaterial-parser\\AccessToken"
267268
messagebox.showinfo("提示", f"Access Token 已保存!\n已写入注册表:{reg_pos}")
269+
elif os_name == "Linux":
270+
file_path = "~/.config/tchMaterial-parser/data.json"
271+
messagebox.showinfo("提示", f"Access Token 已保存!\n已写入文件:{file_path}")
268272
else:
269273
messagebox.showinfo("提示", "Access Token 已保存!")
270274

@@ -442,6 +446,28 @@ def load_access_token_from_registry():
442446
except:
443447
pass # 读取失败则不做处理
444448

449+
# 尝试从Linux系统的 ~/.config/tchMaterial-parser/data.json 文件加载 access_token
450+
def load_access_token_on_linux():
451+
global access_token
452+
try:
453+
# 构建文件路径
454+
target_file = os.path.join(
455+
os.path.expanduser("~"),
456+
".config",
457+
"tchMaterial-parser",
458+
"data.json"
459+
)
460+
# 检查文件是否存在
461+
if not os.path.exists(target_file):
462+
return # 文件不存在不做处理
463+
# 读取JSON文件
464+
with open(target_file, 'r') as f:
465+
data = json.load(f)
466+
# 提取 access_token 字段
467+
access_token = data["access_token"]
468+
except:
469+
pass
470+
445471
# 将access_token写入注册表
446472
def save_access_token_to_registry(token: str):
447473
if os_name == "Windows":
@@ -451,15 +477,44 @@ def save_access_token_to_registry(token: str):
451477
except:
452478
pass
453479

480+
# 将access_token保存到 Linux 系统的 ~/.config/tchMaterial-parser/data.json 文件中
481+
def save_access_token_on_linux(token: str):
482+
try:
483+
# 获取用户主目录路径
484+
home_dir = os.path.expanduser("~")
485+
# 构建目标目录和文件路径
486+
target_dir = os.path.join(
487+
home_dir,
488+
".config", # 新增的目录层级
489+
"tchMaterial-parser"
490+
)
491+
target_file = os.path.join(target_dir, "data.json")
492+
# 创建目录(如果不存在)
493+
os.makedirs(target_dir, exist_ok=True)
494+
# 构建要保存的数据字典
495+
data = {"access_token": token}
496+
# 写入JSON文件
497+
with open(target_file, 'w') as f:
498+
json.dump(data, f, indent=4)
499+
except:
500+
pass
501+
502+
454503
# 设置并更新access_token
455504
def set_access_token(token: str):
456505
global access_token, headers
457506
access_token = token
458507
headers["X-ND-AUTH"] = f'MAC id="{access_token}",nonce="0",mac="0"'
459-
save_access_token_to_registry(token)
508+
if os_name == "Windows":
509+
save_access_token_to_registry(token)
510+
elif os_name == "Linux":
511+
save_access_token_on_linux(token)
460512

461513
# 立即尝试加载已存的access_token(如果有的话)
462-
load_access_token_from_registry()
514+
if os_name == "Windows":
515+
load_access_token_from_registry()
516+
elif os_name == "Linux":
517+
load_access_token_on_linux()
463518

464519

465520
# 获取资源列表

0 commit comments

Comments
 (0)