Batnini commited on
Commit
de24a99
·
verified ·
1 Parent(s): bee01d9

Update tools/quran_search.py

Browse files
Files changed (1) hide show
  1. tools/quran_search.py +16 -15
tools/quran_search.py CHANGED
@@ -5,7 +5,7 @@ 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):
@@ -13,10 +13,10 @@ class QuranSearchEngine:
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}")
@@ -27,14 +27,15 @@ class QuranSearchEngine:
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}")
@@ -46,7 +47,7 @@ class QuranSearchEngine:
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()
@@ -55,16 +56,16 @@ class QuranSearchEngine:
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):
 
5
 
6
  class QuranSearchEngine:
7
  def __init__(self):
8
+ self.api_url = "https://quranapi.pages.dev/api/" # Correct base URL (no trailing slash needed, but consistent)
9
  self.logger = logging.getLogger(__name__)
10
 
11
  def get_surahs(self):
 
13
  try:
14
  response = requests.get(f"{self.api_url}surah.json", timeout=5)
15
  response.raise_for_status()
16
+ surahs = response.json() # Array of surah objects
17
  return [
18
+ (f"{s['surahNameArabicLong']} ({s['surahNameTranslation']})", i + 1)
19
+ for i, s in enumerate(surahs)
20
  ]
21
  except Exception as e:
22
  self.logger.error(f"Failed to fetch surahs: {e}")
 
27
  """Get FULL surah text - tested working"""
28
  try:
29
  response = requests.get(
30
+ f"{self.api_url}{surah_id}.json",
31
  timeout=10
32
  )
33
  response.raise_for_status()
34
+ data = response.json()
35
+ verses = data['arabic1'] # Arabic with tashkeel (Uthmani-like)
36
  return "\n\n".join(
37
+ f"آية {i + 1}: {v}"
38
+ for i, v in enumerate(verses)
39
  )
40
  except Exception as e:
41
  self.logger.error(f"Failed to fetch surah {surah_id}: {e}")
 
47
  for source in QURAN_DATA_SOURCES:
48
  try:
49
  df = pd.read_csv(source)
50
+ # Assuming CSV has columns: surah_id, name_arabic, name_english (adjust if needed)
51
  return [
52
  (f"{row['name_arabic']} ({row['name_english']})", row['surah_id'])
53
  for _, row in df.drop_duplicates(subset=['surah_id']).iterrows()
 
56
  continue
57
  # Hardcoded fallback if all sources fail
58
  return [
59
+ ("سورة الفاتحة (The Opening)", 1),
60
+ ("سورة البقرة (The Cow)", 2),
61
+ ("سورة آل عمران (The Family of Imran)", 3)
62
  ]
63
  except Exception as e:
64
  self.logger.error(f"Failed to load fallback surahs: {e}")
65
  return [
66
+ ("سورة الفاتحة (The Opening)", 1),
67
+ ("سورة البقرة (The Cow)", 2),
68
+ ("سورة آل عمران (The Family of Imran)", 3)
69
  ]
70
 
71
  def _load_fallback_verse(self):