From cea4bc3caaca5c1d378fb958fe7c4afc7e4a5c3f Mon Sep 17 00:00:00 2001 From: khalid_py Date: Wed, 5 Aug 2026 10:34:00 +0000 Subject: [PATCH] Update hadith_api.py --- hadith_api.py | 55 +++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/hadith_api.py b/hadith_api.py index 16d2bcc..52c65db 100644 --- a/hadith_api.py +++ b/hadith_api.py @@ -1,41 +1,32 @@ -"""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=["*"]) +app=FastAPI(title='Hadith DB') +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": "القرآن"}, +H=[ +{'id':1,'book':'Bukhari','narrator':'Umar','text':'Actions are judged by intentions','topic':'intention'}, +{'id':2,'book':'Bukhari','narrator':'Abu Hurairah','text':'Speak good or remain silent','topic':'ethics'}, +{'id':3,'book':'Muslim','narrator':'Abu Hurairah','text':'Seeking knowledge leads to Paradise','topic':'knowledge'}, +{'id':4,'book':'Bukhari','narrator':'Abdullah bin Amr','text':'A Muslim is safe from his tongue and hand','topic':'ethics'}, +{'id':5,'book':'Muslim','narrator':'Abu Dharr','text':'Fear Allah wherever you are','topic':'piety'}, +{'id':6,'book':'Bukhari','narrator':'Anas','text':'Love for your brother what you love for yourself','topic':'faith'}, ] -@app.get("/") -def root(): - return {"name": "Hadith DB", "total_hadiths": len(HADITHS), "books": ["البخاري", "مسلم"]} +@app.get('/') +def root(): return {'name':'Hadith DB','total':len(H)} -@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('/hadith') +def hadith(id:int=None,book:str=None): + r=H + if book: r=[h for h in r if h['book']==book] + if id: r=[h for h in r if h['id']==id] + return r if len(r)>1 else r[0] if r else {'error':'not found'} -@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} +@app.get('/search') +def search(q:str): + r=[h for h in H if q.lower() in h['text'].lower() or q.lower() in h['topic'].lower()] + return {'q':q,'n':len(r),'results':r} -if __name__ == "__main__": - import uvicorn - uvicorn.run(app, host="0.0.0.0", port=8000) +if __name__=='__main__': + import uvicorn;uvicorn.run(app,host='0.0.0.0',port=8000)