Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ai course getAnswer from all examTest #113

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ python main.py -c 114514 -l 25
python main.py -c 114514 -d --proxy http://127.0.0.1:2333
# 又比如您想只干某几个视频, 那您可以使用 -v 传入视频 ID
python main.py -c 114514 -v 1989 604
# 刷ai课程,ai课程号为 114514,classId为 4444
python main.py -ai 114514 4444
```

什么?不知道课程 ID 或视频 ID? 进入课程界面就可以在网址里看到了.
Expand Down Expand Up @@ -216,6 +218,7 @@ python main.py --fetch
- `--tree_view`: 课程目录结构,关闭后不显示所有课程目录。
- `--progressbar_view`: 进度条控制,关闭后不显示当前视频进度。
- `--image_path`: 登录二维码保存路径,留空则不保存。
- `-ai`, `--aicourse`: AI课程, Input AI Course ID and CLASS ID to fu*k aiCourse
- `-h` `--help`: 显示帮助

运行示例如下:
Expand Down
54 changes: 52 additions & 2 deletions fucker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,7 @@ def __init__(self, fucker: Fucker, courseId: int, knowledgeId: int, examTestId:
self.progress_view = progress_view

self.answerCache = {}
self.allAnswerCache = {} # 存储同课程所有题目的答案
self.sheetContent = None
self.timeUpdateIndex = 0
self.examStopped = False
Expand Down Expand Up @@ -1571,8 +1572,45 @@ def getAnswerpath(self, examTestId):
json.dump({}, f)

return answerpath

def getAllAnswerpath(self): # 同课程所有题目的答案
answerpath = getRealPath(
f"aiexamAnswer/{self.courseId}/data.json")

# 确保目录存在
os.makedirs(os.path.dirname(answerpath), exist_ok=True)

# 如果文件不存在,创建一个包含空字典的 JSON 文件
if not os.path.exists(answerpath):
with open(answerpath, "w", encoding="utf-8") as f:
json.dump({}, f)

return answerpath

def readAnswerCache(self, examTestId):
Allanwerpath = self.getAllAnswerpath()
path = getRealPath(f"aiexamAnswer/{self.courseId}/")
VermiIIi0n marked this conversation as resolved.
Show resolved Hide resolved
files = os.listdir(path)
Allcache = {}
for file in files:
if file == "data.json":
continue
file_path = os.path.join(path, file)
with open(file_path, "r", encoding="utf-8") as f:
cache = json.load(f)
Allcache.update(cache)

self.allAnswerCache = {}
for key, value in Allcache.items():
if '_' in key:
questionId, version = key.split('_')
self.allAnswerCache[f"{questionId}_{version}"] = value
else:
self.allAnswerCache[key] = value
# 为没有版本号的答案添加默认版本
value['version'] = 1


answerpath = self.getAnswerpath(examTestId)
with open(answerpath, "r", encoding="utf-8") as f:
cache = json.load(f)
Expand All @@ -1588,16 +1626,20 @@ def readAnswerCache(self, examTestId):
# 为没有版本号的答案添加默认版本
value['version'] = 1

return self.answerCache
return self.answerCache, self.allAnswerCache
VermiIIi0n marked this conversation as resolved.
Show resolved Hide resolved

def writeAnswerCacheToDisk(self):
answerpath = self.getAnswerpath(self.examTestId)
with open(answerpath, "w", encoding="utf-8") as f:
json.dump(self.answerCache, f, ensure_ascii=False, indent=4)

allAnswerpath = self.getAllAnswerpath()
with open(allAnswerpath, "w", encoding="utf-8") as f:
json.dump(self.allAnswerCache, f, ensure_ascii=False, indent=4)

def getAnswer(self, questionId: int, version: int = 1) -> dict | None:
key = str(questionId) if version == 1 else f"{questionId}_{version}"
result = self.answerCache.get(key)
result = self.allAnswerCache.get(key) # 从同课程所有题目的答案中获取
return result

def setAnswer(self, questionId: int, version: int, answer_data: dict):
Expand All @@ -1608,6 +1650,14 @@ def setAnswer(self, questionId: int, version: int, answer_data: dict):
"answer_content": answer_data.get("answer_content", ""),
"questionDict": answer_data.get("questionDict", {})
}
self.allAnswerCache[str(questionId) if version == 1 else f"{questionId}_{version}"] = {
"version": version,
"question": answer_data.get("question", ""),
"answer": answer_data.get("answer", ""),
"answer_content": answer_data.get("answer_content", ""),
"questionDict": answer_data.get("questionDict", {})
}

self.writeAnswerCacheToDisk()

def openExam(self, triedTimes: int = 0):
Expand Down
Loading