cleanup
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
#!/usr/bin/env python3.12
|
||||
# pylint: disable=bare-except, broad-exception-caught
|
||||
|
||||
import os
|
||||
import logging
|
||||
import time
|
||||
import datetime
|
||||
import traceback
|
||||
import aiosqlite as sqlite3
|
||||
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -36,31 +38,35 @@ class ValidTopKarmaRequest(BaseModel):
|
||||
n: int | None = 10
|
||||
|
||||
class KarmaDB:
|
||||
"""Karma DB Util"""
|
||||
def __init__(self):
|
||||
self.db_path = os.path.join("/", "var", "lib", "singerdbs", "karma.db")
|
||||
|
||||
async def get_karma(self, keyword: str) -> int | dict:
|
||||
"""Get Karma Value for Keyword"""
|
||||
async with sqlite3.connect(self.db_path, timeout=2) as db_conn:
|
||||
async with db_conn.execute("SELECT score FROM karma WHERE keyword LIKE ? LIMIT 1", (keyword,)) as db_cursor:
|
||||
try:
|
||||
(score,) = await db_cursor.fetchone()
|
||||
return score
|
||||
except TypeError as e:
|
||||
except TypeError:
|
||||
return {
|
||||
'err': True,
|
||||
'errorText': f'No records for {keyword}',
|
||||
}
|
||||
|
||||
async def get_top(self, n: int = 10):
|
||||
"""Get Top n=10 Karma Entries"""
|
||||
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 ?", (n,)) as db_cursor:
|
||||
return await db_cursor.fetchall()
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return
|
||||
|
||||
async def update_karma(self, granter: str, keyword: str, flag: int):
|
||||
"""Update Karma for Keyword"""
|
||||
if not flag in [0, 1]:
|
||||
return
|
||||
|
||||
@@ -72,7 +78,7 @@ class KarmaDB:
|
||||
audit_query = "INSERT INTO karma_audit(impacted_keyword, comment) VALUES(?, ?)"
|
||||
now = int(time.time())
|
||||
|
||||
print(f"Audit message: {audit_message}\nKeyword: {keyword}")
|
||||
logging.debug("Audit message: %s{audit_message}\nKeyword: %s{keyword}")
|
||||
|
||||
async with sqlite3.connect(self.db_path, timeout=2) as db_conn:
|
||||
async with db_conn.execute(audit_query, (keyword, audit_message,)) as db_cursor:
|
||||
@@ -133,8 +139,8 @@ class Karma(FastAPI):
|
||||
try:
|
||||
top10 = await self.db.get_top(n=n)
|
||||
return top10
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': True,
|
||||
'errorText': 'Exception occurred.',
|
||||
@@ -158,7 +164,7 @@ class Karma(FastAPI):
|
||||
'count': count,
|
||||
}
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': True,
|
||||
'errorText': "Exception occurred."
|
||||
@@ -182,5 +188,4 @@ class Karma(FastAPI):
|
||||
|
||||
return {
|
||||
'success': await self.db.update_karma(data.granter, data.keyword, data.flag)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user