إضافة main.py - بسم الله الرحمن الرحيم
هذا الالتزام موجود في:
40
main.py
Normal file
40
main.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Quran API - FastAPI Application"""
|
||||
from fastapi import FastAPI, Query
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
app = FastAPI(title="Quran API", description="واجهة برمجية للقرآن الكريم", version="1.0.0")
|
||||
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
||||
|
||||
# Load surahs data
|
||||
surahs_path = Path(__file__).parent / "surahs.json"
|
||||
with open(surahs_path, "r", encoding="utf-8") as f:
|
||||
SURAHS = json.load(f)
|
||||
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {"name": "Quran API", "version": "1.0.0", "endpoints": ["/surahs", "/surahs/{id}", "/search"]}
|
||||
|
||||
@app.get("/surahs")
|
||||
def list_surahs():
|
||||
return [{"id": s["id"], "name_ar": s["name_ar"], "name_en": s["name_en"], "verses_count": s["verses_count"], "type": s["type"]} for s in SURAHS]
|
||||
|
||||
@app.get("/surahs/{surah_id}")
|
||||
def get_surah(surah_id: int):
|
||||
if surah_id < 1 or surah_id > 114:
|
||||
return {"error": "رقم السورة يجب أن يكون بين 1 و 114"}
|
||||
surah = SURAHS[surah_id - 1]
|
||||
return surah
|
||||
|
||||
@app.get("/search")
|
||||
def search(q: str = Query(..., description="بحث في أسماء السور")):
|
||||
results = []
|
||||
for s in SURAHS:
|
||||
if q in s["name_ar"] or q.lower() in s["name_en"].lower():
|
||||
results.append(s)
|
||||
return {"query": q, "count": len(results), "results": results}
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
المرجع في مشكلة جديدة
حظر مستخدم