initial commit
This commit is contained in:
120
src/app.py
Normal file
120
src/app.py
Normal file
@@ -0,0 +1,120 @@
|
||||
from quart import Quart, render_template, request, jsonify
|
||||
import httpx
|
||||
from anilistapi.queries.manga import REQUESTS_QUERY, TAGS_QUERY, GENRES_QUERY
|
||||
from anilistapi.schemas.manga import Manga
|
||||
from komcache import KomCache
|
||||
|
||||
app = Quart(__name__)
|
||||
|
||||
cache = KomCache()
|
||||
cache.create_table(
|
||||
"CREATE TABLE IF NOT EXISTS manga_requests (id INTEGER PRIMARY KEY, manga_id INTEGER, manga_title TEXT)"
|
||||
)
|
||||
|
||||
|
||||
async def fetch_data(query):
|
||||
# Simulated API response
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
response = await client.post(
|
||||
f"https://graphql.anilist.co",
|
||||
json={"query": REQUESTS_QUERY, "variables": {"search": query}},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
results = []
|
||||
for item in data.get("data", {}).get("Page", {}).get("media", []):
|
||||
manga = Manga(**item)
|
||||
|
||||
results.append(
|
||||
{
|
||||
"id": manga.id,
|
||||
"title": manga.title.romaji if manga.title else "Untitled",
|
||||
"image": manga.coverImage.get("large")
|
||||
if manga.coverImage
|
||||
else "https://demofree.sirv.com/nope-not-here.jpg",
|
||||
"status": manga.status,
|
||||
"type": manga.type,
|
||||
"genres": manga.genres or [],
|
||||
"tags": [tag.name for tag in (manga.tags or [])],
|
||||
"description": manga.description.replace("<br>", "\n")
|
||||
if manga.description
|
||||
else "No description available",
|
||||
"isAdult": manga.isAdult,
|
||||
}
|
||||
)
|
||||
|
||||
return results
|
||||
except Exception as e:
|
||||
print(f"Error fetching data: {e}")
|
||||
return []
|
||||
|
||||
|
||||
@app.route("/api/genres")
|
||||
async def get_genres():
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
response = await client.post(
|
||||
f"https://graphql.anilist.co",
|
||||
json={"query": GENRES_QUERY},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
results = data.get("data", {}).get("genres", [])
|
||||
return jsonify(results)
|
||||
except Exception as e:
|
||||
print(f"Error fetching genres: {e}")
|
||||
return jsonify([])
|
||||
|
||||
|
||||
@app.route("/api/tags")
|
||||
async def get_tags():
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
response = await client.post(
|
||||
f"https://graphql.anilist.co",
|
||||
json={"query": TAGS_QUERY},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
results = data.get("data", {}).get("tags", [])
|
||||
return jsonify(results)
|
||||
except Exception as e:
|
||||
print(f"Error fetching genres: {e}")
|
||||
return jsonify([])
|
||||
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
async def index():
|
||||
query = None
|
||||
results = []
|
||||
|
||||
if request.method == "POST":
|
||||
form = await request.form
|
||||
query = form.get("query")
|
||||
if query:
|
||||
results = await fetch_data(query)
|
||||
|
||||
return await render_template("index.html", results=results)
|
||||
|
||||
|
||||
@app.route("/request", methods=["POST"])
|
||||
async def log_request():
|
||||
data = await request.get_json()
|
||||
print(data)
|
||||
item = data.get("item")
|
||||
if item:
|
||||
asynccache = KomCache()
|
||||
manga_title = data.get("title")
|
||||
asynccache.insert(
|
||||
"INSERT INTO manga_requests (manga_id, manga_title) VALUES (?, ?)",
|
||||
(item, manga_title),
|
||||
)
|
||||
return jsonify({"status": "success"})
|
||||
return jsonify({"status": "failed"}), 400
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, host="0.0.0.0", port=5000)
|
||||
Reference in New Issue
Block a user