update files, add new workflow parts, add version management to pyproject file
This commit is contained in:
84
src/app.py
84
src/app.py
@@ -22,22 +22,23 @@ log.add("application.log", rotation="1 week", retention="1 month")
|
||||
log.add(sys.stdout)
|
||||
app = Quart(__name__)
|
||||
|
||||
cache = KomCache()
|
||||
cache.create_table(
|
||||
"CREATE TABLE IF NOT EXISTS manga_requests (id INTEGER PRIMARY KEY, manga_id INTEGER, grabbed BOOLEAN DEFAULT 0)"
|
||||
)
|
||||
|
||||
settings = KomConfig()
|
||||
|
||||
|
||||
# else:
|
||||
# cache.create_table(
|
||||
# "CREATE TABLE IF NOT EXISTS manga_requests (id INT AUTO_INCREMENT PRIMARY KEY, anilist_id INT NOT NULL, grabbed BOOLEAN DEFAULT 0)"
|
||||
|
||||
komga = KOMGAPI(
|
||||
url=settings.komga.url,
|
||||
username=settings.komga.user,
|
||||
password=settings.komga.password,
|
||||
)
|
||||
komga_series = komga.seriesList()
|
||||
# komga_series = komga.seriesList()
|
||||
|
||||
|
||||
@limit(90, 60)
|
||||
@limit(limit=1, every=2)
|
||||
async def fetch_data(
|
||||
inputdata: Dict[str, Any], check_downloads: bool = False
|
||||
) -> List[Dict[str, Any]]:
|
||||
@@ -67,6 +68,11 @@ async def fetch_data(
|
||||
"query": REQUESTS_QUERY,
|
||||
"variables": variables,
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "KomPage Searcher API/1.0 (contact: discord-id: 908987973264089139)",
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
@@ -74,14 +80,16 @@ async def fetch_data(
|
||||
log.debug(f"GraphQL response: {data}")
|
||||
|
||||
results: List[Dict[str, Any]] = []
|
||||
cache = KomCache()
|
||||
for item in data.get("data", {}).get("Page", {}).get("media", []):
|
||||
manga = Manga(**item)
|
||||
in_komga = komga.getSeries(
|
||||
manga.title.english if manga.title.english else manga.title.romaji
|
||||
)
|
||||
|
||||
requested = cache.fetch_one(
|
||||
query="SELECT manga_id, grabbed FROM manga_requests WHERE manga_id = ?",
|
||||
args=(manga.id,),
|
||||
query="SELECT manga_id, grabbed FROM manga_requests WHERE manga_id = :id",
|
||||
args={"id": manga.id},
|
||||
)
|
||||
komga_request = bool(requested)
|
||||
results.append(
|
||||
@@ -126,9 +134,12 @@ async def fetch_data(
|
||||
log.error(f"Unexpected error in fetch_data: {e}")
|
||||
return []
|
||||
|
||||
@limit(90, 60)
|
||||
|
||||
@limit(limit=1, every=2)
|
||||
async def fetch_requested_data(data: list[int]) -> List[Dict[str, Any]]:
|
||||
requested_data: list[dict[str, Any]] = []
|
||||
|
||||
cache = KomCache()
|
||||
for manga_id in data:
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
@@ -158,8 +169,8 @@ async def fetch_requested_data(data: list[int]) -> List[Dict[str, Any]]:
|
||||
else manga.title.romaji
|
||||
)
|
||||
requested = cache.fetch_one(
|
||||
query="SELECT grabbed FROM manga_requests WHERE manga_id = ?",
|
||||
args=(manga.id,),
|
||||
query="SELECT grabbed FROM manga_requests WHERE manga_id = :id",
|
||||
args={"id": manga.id},
|
||||
)
|
||||
komga_request = bool(requested)
|
||||
|
||||
@@ -281,18 +292,28 @@ async def api_search():
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
async def index():
|
||||
return await render_template("index.html", komga_series=komga_series)
|
||||
return await render_template("index.html") # , komga_series=komga_series)
|
||||
|
||||
|
||||
@app.route("/request", methods=["POST"])
|
||||
async def log_request():
|
||||
data = await request.get_json()
|
||||
log.debug(f"Received request data: {data}")
|
||||
item = data.get("item")
|
||||
if item:
|
||||
data = await fetch_requested_data([item])
|
||||
if not data:
|
||||
return jsonify({"status": "failed", "message": "Item not found"}), 404
|
||||
data = data[0]
|
||||
title = data.get("title")
|
||||
image_url = data.get("image")
|
||||
log.debug(
|
||||
f"Logging request for item: {item}, title: {title}, image_url: {image_url}"
|
||||
)
|
||||
asynccache = KomCache()
|
||||
asynccache.insert(
|
||||
"INSERT INTO manga_requests (manga_id) VALUES (?)",
|
||||
(item,),
|
||||
f"INSERT INTO manga_requests (manga_id, title, image) VALUES ({item}, '{title}', :image)",
|
||||
args={"image": image_url},
|
||||
)
|
||||
return jsonify({"status": "success"})
|
||||
return jsonify({"status": "failed"}), 400
|
||||
@@ -300,21 +321,42 @@ async def log_request():
|
||||
|
||||
@app.route("/requests", methods=["GET"])
|
||||
async def requests_page():
|
||||
cache = KomCache()
|
||||
requests = (
|
||||
cache.fetch_all(query="SELECT manga_id, grabbed FROM manga_requests") or []
|
||||
cache.fetch_all(
|
||||
query="SELECT manga_id, title, image grabbed FROM manga_requests"
|
||||
)
|
||||
or []
|
||||
)
|
||||
entries: List[Dict[str, Any]] = []
|
||||
req_ids = [req[0] for req in requests]
|
||||
if req_ids:
|
||||
entries = await fetch_requested_data(req_ids)
|
||||
entries = [
|
||||
{"manga_id": req[0], "title": req[1], "image": req[2]} for req in requests
|
||||
]
|
||||
else:
|
||||
entries = []
|
||||
data = []
|
||||
for entry in entries:
|
||||
data.append(dict(entry))
|
||||
|
||||
return await render_template("requests.html", requests=data)
|
||||
return await render_template("requests.html", requests=entries)
|
||||
|
||||
@app.route("/delete", methods=["POST"])
|
||||
async def delete_request():
|
||||
# Delete a request from the database. ID is sent after the /delete endpoint, so: /delete/<id>
|
||||
data = await request.get_json()
|
||||
item_id = data.get("item")
|
||||
|
||||
if item_id:
|
||||
asynccache = KomCache()
|
||||
asynccache.query(
|
||||
"DELETE FROM manga_requests WHERE manga_id = :id", args={"id": item_id}
|
||||
)
|
||||
return jsonify({"status": "success"})
|
||||
return jsonify({"status": "failed"}), 400
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, host="0.0.0.0", port=5001)
|
||||
log.info("Starting Komga Manga Grabber API")
|
||||
# use hypercorn to run the app
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=5001)
|
||||
# run in test mode
|
||||
|
||||
Reference in New Issue
Block a user