-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (85 loc) · 3.23 KB
/
Copy pathmain.py
File metadata and controls
113 lines (85 loc) · 3.23 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
from typing import Union, Dict, List
from glob import glob
from fastapi import FastAPI
import random
import aiofiles
from fastapi.middleware.cors import CORSMiddleware
from fuzzywuzzy import fuzz
import json
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}", response_model=Dict)
async def read_item(item1_id: Union[float, None] = 1.0, item2_id: Union[float, None] = 1.0) -> Dict:
return {"item1*item2": item1_id * item2_id}
@app.get("/randomQuiz", response_model=Dict)
async def get_quiz() -> Dict:
"""Get a random quiz from all available quiz files"""
quizfiles = glob('./quizzes/**/*.json', recursive=True)
quizfile = random.choice(quizfiles)
async with aiofiles.open(quizfile, 'r') as f:
contents = await f.read()
return {"randomQuiz": contents}
def search_str_in_file(file_path: str, word: str) -> bool:
"""Search engine"""
try:
with open(file_path, 'r', errors="ignore") as file:
content = file.read().lower()
words = content.split()
if word.lower() in content:
return True
for w in words:
if fuzz.ratio(w, word.lower()) > 70:
return True
except Exception as e:
print(f"Error reading file {file_path}: {e}")
return False
async def load_quiz_file(file_path: str) -> Dict:
"""Load and parse a quiz file"""
try:
async with aiofiles.open(file_path, 'r') as f:
contents = await f.read()
return json.loads(contents)
except Exception as e:
print(f"Error loading quiz file {file_path}: {e}")
return None
@app.get("/searchQuiz", response_model=Dict)
async def search_quiz(query: str) -> Dict:
"""Search for quizzes"""
if not query or len(query.strip()) == 0:
return {"success": False, "message": "Search query cannot be empty", "results": []}
quizfiles = glob('./Quizzes/**/*.json', recursive=True)
matching_files = []
for file in quizfiles:
if search_str_in_file(file, query):
matching_files.append(file)
if not matching_files:
return {"success": False, "message": "No quizzes found matching your search", "results": []}
results = []
for file_path in matching_files:
quiz_data = await load_quiz_file(file_path)
if quiz_data:
results.append({
"title": quiz_data.get('title', 'Untitled Quiz'),
"filePath": file_path,
"quiz": quiz_data
})
return {"success": True, "message": f"Found {len(results)} quiz(zes)", "results": results}
@app.get("/getQuizByPath", response_model=Dict)
async def get_quiz_by_path(path: str) -> Dict:
"""Get a specific quiz by file path"""
try:
async with aiofiles.open(path, 'r') as f:
contents = await f.read()
return {"success": True, "randomQuiz": contents}
except Exception as e:
return {"success": False, "message": f"Error loading quiz: {str(e)}"}