AnesKAM commited on
Commit
7908014
·
verified ·
1 Parent(s): fac552f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -96
app.py CHANGED
@@ -1,12 +1,10 @@
1
- from flask import Flask, render_template_string, request, jsonify
2
- import requests
3
 
4
  app = Flask(__name__)
5
 
6
- # --- ضع مفتاح Tavily API الخاص بك هنا ---
7
- TAVILY_API_KEY = "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"
8
 
9
- # --- جزء HTML (نفس التصميم السابق الجميل) ---
10
  HTML_TEMPLATE = """
11
  <!DOCTYPE html>
12
  <html lang="ar" dir="rtl">
@@ -20,15 +18,17 @@ HTML_TEMPLATE = """
20
  font-family: 'Segoe UI', Tahoma, sans-serif;
21
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
22
  min-height: 100vh; display: flex; flex-direction: column; align-items: center;
23
- justify-content: center; padding: 20px;
24
  }
25
- .container { width: 100%; max-width: 700px; text-align: center; }
26
  .logo { font-size: 3rem; color: #e94560; margin-bottom: 10px; font-weight: bold; letter-spacing: 2px; }
27
  .subtitle { color: #a0a0b0; margin-bottom: 30px; font-size: 0.9rem; }
 
 
28
  .search-box {
29
  display: flex; gap: 10px; background: rgba(255,255,255,0.1);
30
  backdrop-filter: blur(10px); border-radius: 30px; padding: 5px 10px;
31
- box-shadow: 0 8px 32px rgba(0,0,0,0.3); margin-bottom: 20px;
32
  }
33
  .search-box input {
34
  flex: 1; padding: 15px 20px; border: none; background: transparent;
@@ -41,121 +41,103 @@ HTML_TEMPLATE = """
41
  transition: background 0.3s;
42
  }
43
  .search-box button:hover { background: #c23152; }
44
- #results { width: 100%; margin-top: 20px; text-align: right; }
45
- .result-item {
46
- background: rgba(255,255,255,0.05); backdrop-filter: blur(5px);
47
- border-radius: 12px; padding: 15px; margin-bottom: 10px;
48
- border-right: 3px solid #e94560;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  }
50
- .result-item:hover { background: rgba(255,255,255,0.1); }
51
- .result-item a {
52
- color: #10b981; text-decoration: none; font-size: 1.1rem; font-weight: bold;
53
  }
54
- .result-item a:hover { text-decoration: underline; }
55
- .result-item .url { color: #6ee7b7; font-size: 0.8rem; margin: 5px 0; word-break: break-all; }
56
- .result-item p { color: #ccc; font-size: 0.9rem; margin-top: 8px; line-height: 1.5; }
57
- .loading { color: white; margin: 20px 0; display: none; }
58
- .spinner { display: inline-block; width: 20px; height: 20px; border: 3px solid rgba(255,255,255,0.3); border-radius: 50%; border-top-color: #10b981; animation: spin 1s linear infinite; margin-left: 8px; }
59
- @keyframes spin { to { transform: rotate(360deg); } }
60
  </style>
 
 
 
61
  </head>
62
  <body>
63
  <div class="container">
64
  <div class="logo">🔎 باحث</div>
65
- <p class="subtitle">بحث ذكي وسريع - مدعوم من Tavily</p>
66
 
 
67
  <div class="search-box">
68
- <input type="text" id="searchInput" placeholder="ابحث عن أي شيء..." dir="auto">
69
- <button onclick="performSearch()">🔍 بحث</button>
 
70
  </div>
71
 
72
- <div class="loading" id="loading">
73
- <span class="spinner"></span> جاري البحث...
 
74
  </div>
75
-
76
- <div id="results"></div>
77
  </div>
78
 
79
  <script>
80
- async function performSearch() {
81
  const query = document.getElementById('searchInput').value.trim();
82
  if (!query) return;
83
-
84
- const loading = document.getElementById('loading');
85
- const resultsDiv = document.getElementById('results');
86
 
87
- loading.style.display = 'block';
88
- resultsDiv.innerHTML = '';
89
-
90
- try {
91
- const response = await fetch(`/search?q=${encodeURIComponent(query)}`);
92
- const data = await response.json();
93
-
94
- loading.style.display = 'none';
95
-
96
- if (data.results && data.results.length > 0) {
97
- resultsDiv.innerHTML = data.results.map((res, i) => `
98
- <div class="result-item">
99
- <a href="${res.link}" target="_blank" rel="noopener">${i + 1}. ${res.title}</a>
100
- <div class="url">${res.link}</div>
101
- <p>${res.snippet}</p>
102
- </div>
103
- `).join('');
104
- } else {
105
- resultsDiv.innerHTML = '<p style="color: #ccc;">😔 عذراً، لم يتم العثور على نتائج.</p>';
106
- }
107
- } catch (error) {
108
- loading.style.display = 'none';
109
- resultsDiv.innerHTML = `<p style="color: #ff6b6b;">⚠️ حدث خطأ: ${error.message}</p>`;
110
  }
111
  }
112
- document.getElementById('searchInput').addEventListener('keypress', function(e) {
113
- if (e.key === 'Enter') performSearch();
114
- });
115
  </script>
116
  </body>
117
  </html>
118
  """
119
 
120
- def search_tavily(query, max_results=10):
121
- url = "https://api.tavily.com/search"
122
- headers = {
123
- "Content-Type": "application/json"
124
- }
125
- data = {
126
- "api_key": TAVILY_API_KEY,
127
- "query": query,
128
- "max_results": max_results
129
- }
130
-
131
- try:
132
- response = requests.post(url, json=data, headers=headers, timeout=10)
133
- response.raise_for_status()
134
- search_results = response.json()
135
-
136
- results = []
137
- for item in search_results.get("results", []):
138
- results.append({
139
- "title": item.get("title", ""),
140
- "link": item.get("url", ""),
141
- "snippet": item.get("content", "")
142
- })
143
- return results
144
- except Exception as e:
145
- print(f"Tavily API Error: {e}")
146
- return []
147
-
148
  @app.route('/')
149
  def home():
150
- return render_template_string(HTML_TEMPLATE)
151
-
152
- @app.route('/search')
153
- def search():
154
- query = request.args.get('q', '')
155
- if not query:
156
- return jsonify({'results': []})
157
- results = search_tavily(query)
158
- return jsonify({'results': results})
159
 
160
  if __name__ == '__main__':
161
  app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, render_template_string
 
2
 
3
  app = Flask(__name__)
4
 
5
+ # --- Google CSE ID الخاص بك ---
6
+ GOOGLE_CSE_ID = "2342fc17cd34f4cba"
7
 
 
8
  HTML_TEMPLATE = """
9
  <!DOCTYPE html>
10
  <html lang="ar" dir="rtl">
 
18
  font-family: 'Segoe UI', Tahoma, sans-serif;
19
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
20
  min-height: 100vh; display: flex; flex-direction: column; align-items: center;
21
+ padding: 20px;
22
  }
23
+ .container { width: 100%; max-width: 800px; text-align: center; }
24
  .logo { font-size: 3rem; color: #e94560; margin-bottom: 10px; font-weight: bold; letter-spacing: 2px; }
25
  .subtitle { color: #a0a0b0; margin-bottom: 30px; font-size: 0.9rem; }
26
+
27
+ /* صندوق البحث */
28
  .search-box {
29
  display: flex; gap: 10px; background: rgba(255,255,255,0.1);
30
  backdrop-filter: blur(10px); border-radius: 30px; padding: 5px 10px;
31
+ box-shadow: 0 8px 32px rgba(0,0,0,0.3); margin-bottom: 30px;
32
  }
33
  .search-box input {
34
  flex: 1; padding: 15px 20px; border: none; background: transparent;
 
41
  transition: background 0.3s;
42
  }
43
  .search-box button:hover { background: #c23152; }
44
+
45
+ /* تنسيق نتائج Google CSE */
46
+ #results-area {
47
+ width: 100%;
48
+ text-align: right;
49
+ margin-top: 20px;
50
+ }
51
+ .gsc-control-cse {
52
+ background: transparent !important;
53
+ border: none !important;
54
+ padding: 0 !important;
55
+ }
56
+ .gsc-result {
57
+ background: rgba(255,255,255,0.05) !important;
58
+ backdrop-filter: blur(5px);
59
+ border-radius: 12px !important;
60
+ padding: 15px !important;
61
+ margin-bottom: 10px !important;
62
+ border-right: 3px solid #e94560 !important;
63
+ border: none !important;
64
+ }
65
+ .gsc-result:hover {
66
+ background: rgba(255,255,255,0.1) !important;
67
+ }
68
+ .gs-title {
69
+ color: #4fc3f7 !important;
70
+ text-decoration: none !important;
71
+ font-size: 1.1rem !important;
72
+ }
73
+ .gs-title:hover {
74
+ text-decoration: underline !important;
75
+ }
76
+ .gs-visibleUrl {
77
+ color: #66bb6a !important;
78
+ }
79
+ .gs-snippet {
80
+ color: #ccc !important;
81
+ }
82
+ .gsc-search-box {
83
+ display: none !important; /* نخفي صندوق بحث Google العلوي لأن لدينا صندوقنا الخاص */
84
  }
85
+ .gsc-branding {
86
+ display: none !important; /* نخفي شعار Google */
 
87
  }
 
 
 
 
 
 
88
  </style>
89
+
90
+ <!-- Google CSE Script -->
91
+ <script async src="https://cse.google.com/cse.js?cx={{ cse_id }}"></script>
92
  </head>
93
  <body>
94
  <div class="container">
95
  <div class="logo">🔎 باحث</div>
96
+ <p class="subtitle">محرك بحث مخصص - ابحث في الإنترنت</p>
97
 
98
+ <!-- صندوق البحث الخاص بنا -->
99
  <div class="search-box">
100
+ <input type="text" id="searchInput" placeholder="ماذا تريد أن تبحث؟" dir="auto"
101
+ onkeypress="if(event.key==='Enter') executeSearch()">
102
+ <button onclick="executeSearch()">🔍 بحث</button>
103
  </div>
104
 
105
+ <!-- منطقة عرض النتائج من Google CSE -->
106
+ <div id="results-area">
107
+ <div class="gcse-searchresults-only"></div>
108
  </div>
 
 
109
  </div>
110
 
111
  <script>
112
+ function executeSearch() {
113
  const query = document.getElementById('searchInput').value.trim();
114
  if (!query) return;
 
 
 
115
 
116
+ // نستخدم Google CSE API لتشغيل البحث
117
+ var element = google.search.cse.element;
118
+ if (element) {
119
+ // إذا كانت نتائج البحث معروضة بالفعل، نقوم بتحديثها
120
+ element.getElement({name: 'searchresults-only'}).execute(query);
121
+ } else {
122
+ // أول مرة، نقوم بتهيئة محرك البحث
123
+ google.search.cse.element.render({
124
+ div: "results-area",
125
+ tag: 'searchresults-only',
126
+ gname: 'searchresults-only',
127
+ attributes: {
128
+ query: query
129
+ }
130
+ });
 
 
 
 
 
 
 
 
131
  }
132
  }
 
 
 
133
  </script>
134
  </body>
135
  </html>
136
  """
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  @app.route('/')
139
  def home():
140
+ return render_template_string(HTML_TEMPLATE, cse_id=GOOGLE_CSE_ID)
 
 
 
 
 
 
 
 
141
 
142
  if __name__ == '__main__':
143
  app.run(host='0.0.0.0', port=7860)