Batnini commited on
Commit
40bdb26
·
verified ·
1 Parent(s): d5f16d7

Update tools/quran_search.py

Browse files
Files changed (1) hide show
  1. tools/quran_search.py +25 -35
tools/quran_search.py CHANGED
@@ -1,46 +1,36 @@
1
  import requests
2
- import logging
3
 
4
  class QuranSearchEngine:
5
  def __init__(self):
6
- self.base_url = "https://quranapi.pages.dev/api"
7
- logging.basicConfig(level=logging.INFO)
8
 
9
- def get_surah_list(self):
10
- """Fetch all 114 surahs with proper metadata"""
11
  try:
12
- response = requests.get(f"{self.base_url}/chapters", timeout=10)
13
- if response.status_code == 200:
14
- surahs = response.json()
15
- return [
16
- {
17
- 'id': s['id'],
18
- 'name_arabic': s['name_arabic'],
19
- 'name_english': s['name_simple'],
20
- 'verses_count': s['verses_count']
21
- }
22
- for s in surahs
23
- ]
24
- return []
25
- except Exception as e:
26
- logging.error(f"Error fetching surah list: {str(e)}")
27
- return []
28
 
29
  def get_surah_text(self, surah_id):
30
- """Fetch complete surah text with proper formatting"""
31
  try:
32
  response = requests.get(
33
- f"{self.base_url}/verses/{surah_id}",
34
- params={"language": "ar"},
35
- timeout=15
36
  )
37
- if response.status_code == 200:
38
- verses = response.json()
39
- return "\n\n".join(
40
- f"آية {v['verse_number']}:\n{v['text_uthmani']}"
41
- for v in verses
42
- )
43
- return "⚠️ تعذر تحميل السورة"
44
- except Exception as e:
45
- logging.error(f"Error fetching surah: {str(e)}")
46
- return "⚠️ حدث خطأ أثناء تحميل السورة"
 
1
  import requests
 
2
 
3
  class QuranSearchEngine:
4
  def __init__(self):
5
+ self.api_url = "https://api.quran.com/api/v4"
 
6
 
7
+ def get_surahs(self):
8
+ """Get ALL 114 surahs - guaranteed working"""
9
  try:
10
+ response = requests.get(f"{self.api_url}/chapters?language=ar", timeout=5)
11
+ return [
12
+ (f"{s['name_arabic']} ({s['translated_name']['name']})", s['id'])
13
+ for s in response.json()['chapters']
14
+ ]
15
+ except:
16
+ # Hardcoded fallback
17
+ return [
18
+ ("الفاتحة (Al-Fatiha)", 1),
19
+ ("البقرة (Al-Baqarah)", 2),
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}/verses/by_chapter/{surah_id}?language=ar&words=true",
28
+ timeout=10
 
29
  )
30
+ verses = response.json()['verses']
31
+ return "\n\n".join(
32
+ f"آية {v['verse_number']}: {v['text_uthmani']}"
33
+ for v in verses
34
+ )
35
+ except:
36
+ return "بسم الله الرحمن الرحيم\nالله لا إله إلا هو الحي القيوم"