From 82713d82e98a33940b9b6b0e3c59181a622a563d Mon Sep 17 00:00:00 2001 From: khalid_py Date: Wed, 5 Aug 2026 09:58:52 +0000 Subject: [PATCH] =?UTF-8?q?=D8=A5=D8=B6=D8=A7=D9=81=D8=A9=20hadith=5Fapi.p?= =?UTF-8?q?y=20-=20=D8=A8=D8=B3=D9=85=20=D8=A7=D9=84=D9=84=D9=87=20=D8=A7?= =?UTF-8?q?=D9=84=D8=B1=D8=AD=D9=85=D9=86=20=D8=A7=D9=84=D8=B1=D8=AD=D9=8A?= =?UTF-8?q?=D9=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hadith_api.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 hadith_api.py diff --git a/hadith_api.py b/hadith_api.py new file mode 100644 index 0000000..16d2bcc --- /dev/null +++ b/hadith_api.py @@ -0,0 +1,41 @@ +"""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)