Update hadith_api.py

هذا الالتزام موجود في:
2026-08-05 10:34:00 +00:00
الأصل e5bfa4cdfa
التزام cea4bc3caa

عرض الملف

@@ -1,41 +1,32 @@
"""Hadith Database API"""
from fastapi import FastAPI, Query from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="Hadith DB", description="قاعدة بيانات الأحاديث النبوية", version="1.0.0") app=FastAPI(title='Hadith DB')
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) app.add_middleware(CORSMiddleware,allow_origins=['*'],allow_methods=['*'],allow_headers=['*'])
HADITHS = [ H=[
{"id": 1, "book": "البخاري", "narrator": "عمر بن الخطاب", "text": "إِنَّمَا الْأَعْمَالُ بِالنِّيَّاتِ", "topic": "النية"}, {'id':1,'book':'Bukhari','narrator':'Umar','text':'Actions are judged by intentions','topic':'intention'},
{"id": 2, "book": "البخاري", "narrator": "أبو هريرة", "text": "مَنْ كَانَ يُؤْمِنُ بِاللَّهِ وَالْيَوْمِ الْآخِرِ فَلْيَقُلْ خَيْرًا أَوْ لِيَصْمُتْ", "topic": "الأخلاق"}, {'id':2,'book':'Bukhari','narrator':'Abu Hurairah','text':'Speak good or remain silent','topic':'ethics'},
{"id": 3, "book": "مسلم", "narrator": "أبو هريرة", "text": "مَنْ سَلَكَ طَرِيقًا يَلْتَمِسُ فِيهِ عِلْمًا سَهَّلَ اللَّهُ لَهُ بِهِ طَرِيقًا إِلَى الْجَنَّةِ", "topic": "العلم"}, {'id':3,'book':'Muslim','narrator':'Abu Hurairah','text':'Seeking knowledge leads to Paradise','topic':'knowledge'},
{"id": 4, "book": "البخاري", "narrator": "عبد الله بن عمرو", "text": "الْمُسْلِمُ مَنْ سَلِمَ الْمُسْلِمُونَ مِنْ لِسَانِهِ وَيَدِهِ", "topic": "الأخلاق"}, {'id':4,'book':'Bukhari','narrator':'Abdullah bin Amr','text':'A Muslim is safe from his tongue and hand','topic':'ethics'},
{"id": 5, "book": "مسلم", "narrator": "أبو ذر", "text": "اتَّقِ اللَّهَ حَيْثُمَا كُنْتَ وَأَتْبِعِ السَّيِّئَةَ الْحَسَنَةَ تَمْحُهَا وَخَالِقِ النَّاسَ بِخُلُقٍ حَسَنٍ", "topic": "التقوى"}, {'id':5,'book':'Muslim','narrator':'Abu Dharr','text':'Fear Allah wherever you are','topic':'piety'},
{"id": 6, "book": "البخاري", "narrator": "أنس بن مالك", "text": "لَا يُؤْمِنُ أَحَدُكُمْ حَتَّى يُحِبَّ لِأَخِيهِ مَا يُحِبُّ لِنَفْسِهِ", "topic": "الإيمان"}, {'id':6,'book':'Bukhari','narrator':'Anas','text':'Love for your brother what you love for yourself','topic':'faith'},
{"id": 7, "book": "مسلم", "narrator": "النووي", "text": "مِنْ حُسْنِ إِسْلَامِ الْمَرْءِ تَرْكُهُ مَا لَا يَعْنِيهِ", "topic": "الأخلاق"},
{"id": 8, "book": "البخاري", "narrator": "أبو موسى", "text": "مَثَلُ الْمُؤْمِنِ الَّذِي يَقْرَأُ الْقُرْآنَ كَالْأُتْرُجَّةِ", "topic": "القرآن"},
] ]
@app.get("/") @app.get('/')
def root(): def root(): return {'name':'Hadith DB','total':len(H)}
return {"name": "Hadith DB", "total_hadiths": len(HADITHS), "books": ["البخاري", "مسلم"]}
@app.get("/hadith") @app.get('/hadith')
def get_hadith(book: str = Query(None), id: int = Query(None)): def hadith(id:int=None,book:str=None):
results = HADITHS r=H
if book: if book: r=[h for h in r if h['book']==book]
results = [h for h in results if h["book"] == book] if id: r=[h for h in r if h['id']==id]
if id: return r if len(r)>1 else r[0] if r else {'error':'not found'}
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") @app.get('/search')
def search(q: str = Query(..., description="بحث في نصوص الأحاديث")): def search(q:str):
results = [h for h in HADITHS if q in h["text"] or q in h["topic"]] r=[h for h in H if q.lower() in h['text'].lower() or q.lower() in h['topic'].lower()]
return {"query": q, "count": len(results), "results": results} return {'q':q,'n':len(r),'results':r}
if __name__ == "__main__": if __name__=='__main__':
import uvicorn import uvicorn;uvicorn.run(app,host='0.0.0.0',port=8000)
uvicorn.run(app, host="0.0.0.0", port=8000)