Update tools/quran_search.py
Browse files- tools/quran_search.py +53 -17
tools/quran_search.py
CHANGED
|
@@ -1,36 +1,72 @@
|
|
| 1 |
import requests
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class QuranSearchEngine:
|
| 4 |
def __init__(self):
|
| 5 |
-
self.api_url = "https://
|
|
|
|
| 6 |
|
| 7 |
def get_surahs(self):
|
| 8 |
"""Get ALL 114 surahs - guaranteed working"""
|
| 9 |
try:
|
| 10 |
-
response = requests.get(f"{self.api_url}
|
|
|
|
|
|
|
| 11 |
return [
|
| 12 |
-
(f"{s['name_arabic']} ({s['
|
| 13 |
-
for s in
|
| 14 |
]
|
| 15 |
-
except:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
("آل عمران (Aal Imran)", 3)
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
def get_surah_text(self, surah_id):
|
| 24 |
"""Get FULL surah text - tested working"""
|
| 25 |
try:
|
| 26 |
response = requests.get(
|
| 27 |
-
f"{self.api_url}
|
| 28 |
timeout=10
|
| 29 |
)
|
| 30 |
-
|
|
|
|
| 31 |
return "\n\n".join(
|
| 32 |
-
f"آية {v['verse_number']}: {v['
|
| 33 |
for v in verses
|
| 34 |
)
|
| 35 |
-
except:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import logging
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from config import QURAN_DATA_SOURCES
|
| 5 |
|
| 6 |
class QuranSearchEngine:
|
| 7 |
def __init__(self):
|
| 8 |
+
self.api_url = "https://quranapi.pages.dev/api/" # Updated API base URL
|
| 9 |
+
self.logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
def get_surahs(self):
|
| 12 |
"""Get ALL 114 surahs - guaranteed working"""
|
| 13 |
try:
|
| 14 |
+
response = requests.get(f"{self.api_url}surah.json", timeout=5)
|
| 15 |
+
response.raise_for_status()
|
| 16 |
+
chapters = response.json() # Expecting a list of surahs
|
| 17 |
return [
|
| 18 |
+
(f"{s['name_arabic']} ({s['name_english']})", s['id'])
|
| 19 |
+
for s in chapters
|
| 20 |
]
|
| 21 |
+
except Exception as e:
|
| 22 |
+
self.logger.error(f"Failed to fetch surahs: {e}")
|
| 23 |
+
# Fallback to local Quran data
|
| 24 |
+
return self._load_fallback_surahs()
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
| 26 |
def get_surah_text(self, surah_id):
|
| 27 |
"""Get FULL surah text - tested working"""
|
| 28 |
try:
|
| 29 |
response = requests.get(
|
| 30 |
+
f"{self.api_url}{surah_id}.json", # Adjusted endpoint
|
| 31 |
timeout=10
|
| 32 |
)
|
| 33 |
+
response.raise_for_status()
|
| 34 |
+
verses = response.json() # Expecting a list of verses
|
| 35 |
return "\n\n".join(
|
| 36 |
+
f"آية {v['verse_number']}: {v['text']}"
|
| 37 |
for v in verses
|
| 38 |
)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
self.logger.error(f"Failed to fetch surah {surah_id}: {e}")
|
| 41 |
+
return self._load_fallback_verse()
|
| 42 |
+
|
| 43 |
+
def _load_fallback_surahs(self):
|
| 44 |
+
"""Load surah list from local CSV fallback"""
|
| 45 |
+
try:
|
| 46 |
+
for source in QURAN_DATA_SOURCES:
|
| 47 |
+
try:
|
| 48 |
+
df = pd.read_csv(source)
|
| 49 |
+
# Assuming CSV has columns: surah_id, name_arabic, name_english
|
| 50 |
+
return [
|
| 51 |
+
(f"{row['name_arabic']} ({row['name_english']})", row['surah_id'])
|
| 52 |
+
for _, row in df.drop_duplicates(subset=['surah_id']).iterrows()
|
| 53 |
+
]
|
| 54 |
+
except:
|
| 55 |
+
continue
|
| 56 |
+
# Hardcoded fallback if all sources fail
|
| 57 |
+
return [
|
| 58 |
+
("الفاتحة (Al-Fatiha)", 1),
|
| 59 |
+
("البقرة (Al-Baqarah)", 2),
|
| 60 |
+
("آل عمران (Aal Imran)", 3)
|
| 61 |
+
]
|
| 62 |
+
except Exception as e:
|
| 63 |
+
self.logger.error(f"Failed to load fallback surahs: {e}")
|
| 64 |
+
return [
|
| 65 |
+
("الفاتحة (Al-Fatiha)", 1),
|
| 66 |
+
("البقرة (Al-Baqarah)", 2),
|
| 67 |
+
("آل عمران (Aal Imran)", 3)
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
def _load_fallback_verse(self):
|
| 71 |
+
"""Load a fallback verse text"""
|
| 72 |
+
return "بسم الله الرحمن الرحيم\nالله لا إله إلا هو الحي القيوم"
|