Mohamed478 commited on
Commit
6f24875
·
1 Parent(s): 2706084

Fix search endpoint file permissions

Browse files
Files changed (2) hide show
  1. app/main.py +1 -2
  2. test_search.py +31 -0
app/main.py CHANGED
@@ -352,6 +352,5 @@ def web_search(q: str, max_results: int = 8):
352
  return results
353
  except Exception as e:
354
  import traceback
355
- with open("error.log", "w", encoding="utf-8") as f:
356
- f.write(traceback.format_exc())
357
  raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء البحث: {str(e)}")
 
352
  return results
353
  except Exception as e:
354
  import traceback
355
+ traceback.print_exc()
 
356
  raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء البحث: {str(e)}")
test_search.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import urllib.request
2
+ import urllib.parse
3
+ import json
4
+ import re
5
+
6
+ q = "محمد نجيب"
7
+ max_results = 9
8
+
9
+ is_arabic = bool(re.search(r'[\u0600-\u06FF]', q))
10
+ lang = "ar" if is_arabic else "en"
11
+
12
+ url = f"https://{lang}.wikipedia.org/w/api.php?action=query&list=search&srsearch={urllib.parse.quote(q)}&utf8=&format=json&srlimit={max_results}"
13
+ req = urllib.request.Request(url, headers={'User-Agent': 'SmartReader/1.0'})
14
+
15
+ with urllib.request.urlopen(req, timeout=10) as response:
16
+ data = json.loads(response.read().decode('utf-8'))
17
+
18
+ results = []
19
+ for item in data.get('query', {}).get('search', []):
20
+ title = item.get('title', '')
21
+ snippet_html = item.get('snippet', '')
22
+ snippet = re.sub(r'<[^>]+>', '', snippet_html)
23
+ article_url = f"https://{lang}.wikipedia.org/wiki/{urllib.parse.quote(title.replace(' ', '_'))}"
24
+ results.append({
25
+ "title": title,
26
+ "url": article_url,
27
+ "snippet": snippet,
28
+ "source": "ويكيبيديا" if is_arabic else "Wikipedia"
29
+ })
30
+
31
+ print(json.dumps(results, indent=2, ensure_ascii=False))