42 أسطر
3.1 KiB
Python
42 أسطر
3.1 KiB
Python
"""Hadith Database API"""
|
|
from fastapi import FastAPI, Query
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
app = FastAPI(title="Hadith DB", description="قاعدة بيانات الأحاديث النبوية", version="1.0.0")
|
|
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
|
|
|
HADITHS = [
|
|
{"id": 1, "book": "البخاري", "narrator": "عمر بن الخطاب", "text": "إِنَّمَا الْأَعْمَالُ بِالنِّيَّاتِ", "topic": "النية"},
|
|
{"id": 2, "book": "البخاري", "narrator": "أبو هريرة", "text": "مَنْ كَانَ يُؤْمِنُ بِاللَّهِ وَالْيَوْمِ الْآخِرِ فَلْيَقُلْ خَيْرًا أَوْ لِيَصْمُتْ", "topic": "الأخلاق"},
|
|
{"id": 3, "book": "مسلم", "narrator": "أبو هريرة", "text": "مَنْ سَلَكَ طَرِيقًا يَلْتَمِسُ فِيهِ عِلْمًا سَهَّلَ اللَّهُ لَهُ بِهِ طَرِيقًا إِلَى الْجَنَّةِ", "topic": "العلم"},
|
|
{"id": 4, "book": "البخاري", "narrator": "عبد الله بن عمرو", "text": "الْمُسْلِمُ مَنْ سَلِمَ الْمُسْلِمُونَ مِنْ لِسَانِهِ وَيَدِهِ", "topic": "الأخلاق"},
|
|
{"id": 5, "book": "مسلم", "narrator": "أبو ذر", "text": "اتَّقِ اللَّهَ حَيْثُمَا كُنْتَ وَأَتْبِعِ السَّيِّئَةَ الْحَسَنَةَ تَمْحُهَا وَخَالِقِ النَّاسَ بِخُلُقٍ حَسَنٍ", "topic": "التقوى"},
|
|
{"id": 6, "book": "البخاري", "narrator": "أنس بن مالك", "text": "لَا يُؤْمِنُ أَحَدُكُمْ حَتَّى يُحِبَّ لِأَخِيهِ مَا يُحِبُّ لِنَفْسِهِ", "topic": "الإيمان"},
|
|
{"id": 7, "book": "مسلم", "narrator": "النووي", "text": "مِنْ حُسْنِ إِسْلَامِ الْمَرْءِ تَرْكُهُ مَا لَا يَعْنِيهِ", "topic": "الأخلاق"},
|
|
{"id": 8, "book": "البخاري", "narrator": "أبو موسى", "text": "مَثَلُ الْمُؤْمِنِ الَّذِي يَقْرَأُ الْقُرْآنَ كَالْأُتْرُجَّةِ", "topic": "القرآن"},
|
|
]
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return {"name": "Hadith DB", "total_hadiths": len(HADITHS), "books": ["البخاري", "مسلم"]}
|
|
|
|
@app.get("/hadith")
|
|
def get_hadith(book: str = Query(None), id: int = Query(None)):
|
|
results = HADITHS
|
|
if book:
|
|
results = [h for h in results if h["book"] == book]
|
|
if id:
|
|
results = [h for h in results if h["id"] == id]
|
|
if not results:
|
|
return {"error": "لم يتم العثور على حديث"}
|
|
return results if len(results) > 1 else results[0]
|
|
|
|
@app.get("/search")
|
|
def search(q: str = Query(..., description="بحث في نصوص الأحاديث")):
|
|
results = [h for h in HADITHS if q in h["text"] or q in h["topic"]]
|
|
return {"query": q, "count": len(results), "results": results}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|