chooo choooo
This commit is contained in:
parent
2110914133
commit
991512e796
@ -30,6 +30,11 @@ class ValidKarmaRetrievalRequest(BaseModel):
|
||||
|
||||
keyword: str
|
||||
|
||||
class ValidTopKarmaRequest(BaseModel):
|
||||
"""
|
||||
- **n**: Number of top results to return (default: 10)
|
||||
"""
|
||||
n: int | None = 10
|
||||
|
||||
class KarmaDB:
|
||||
def __init__(self):
|
||||
@ -47,10 +52,10 @@ class KarmaDB:
|
||||
'errorText': f'No records for {keyword}',
|
||||
}
|
||||
|
||||
async def get_top_10(self):
|
||||
async def get_top(self, n: int = 10):
|
||||
try:
|
||||
async with sqlite3.connect(self.db_path, timeout=2) as db_conn:
|
||||
async with db_conn.execute("SELECT keyword, score FROM karma ORDER BY score DESC LIMIT 10") as db_cursor:
|
||||
async with db_conn.execute("SELECT keyword, score FROM karma ORDER BY score DESC LIMIT ?", (n,)) as db_cursor:
|
||||
return await db_cursor.fetchall()
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
@ -111,14 +116,23 @@ class Karma(FastAPI):
|
||||
app.add_api_route(f"/{endpoint}/", handler, methods=["POST"])
|
||||
|
||||
|
||||
async def top_karma_handler(self):
|
||||
async def top_karma_handler(self, request: Request, data: ValidTopKarmaRequest | None = None):
|
||||
"""
|
||||
/karma/top/
|
||||
Get top 10 for karma
|
||||
Get top keywords for karma
|
||||
Requires key
|
||||
"""
|
||||
|
||||
if not self.util.check_key(request.url.path, request.headers.get('X-Authd-With')):
|
||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||
|
||||
n = 10
|
||||
if data:
|
||||
n = data.n
|
||||
|
||||
|
||||
try:
|
||||
top10 = await self.db.get_top_10()
|
||||
top10 = await self.db.get_top(n=n)
|
||||
return top10
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
@ -131,6 +145,7 @@ class Karma(FastAPI):
|
||||
"""
|
||||
/karma/get/
|
||||
Get current karma value
|
||||
Requires key
|
||||
"""
|
||||
|
||||
keyword = data.keyword
|
||||
@ -150,7 +165,8 @@ class Karma(FastAPI):
|
||||
async def modify_karma_handler(self, data: ValidKarmaUpdateRequest, request: Request):
|
||||
"""
|
||||
/karma/update/
|
||||
Update karma count (requires PUT KEY)
|
||||
Update karma count
|
||||
Requires key
|
||||
"""
|
||||
|
||||
if not self.util.check_key(request.url.path, request.headers.get('X-Authd-With'), 2):
|
||||
|
Loading…
x
Reference in New Issue
Block a user