Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
from flask import Flask, request, render_template_string, jsonify
|
| 2 |
import sqlite3
|
| 3 |
from datetime import datetime
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
HTML_TEMPLATE = '''
|
| 11 |
<!DOCTYPE html>
|
| 12 |
<html dir="rtl">
|
|
@@ -17,7 +17,7 @@ HTML_TEMPLATE = '''
|
|
| 17 |
<style>
|
| 18 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 19 |
body {
|
| 20 |
-
font-family: '
|
| 21 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 22 |
min-height: 100vh;
|
| 23 |
}
|
|
@@ -30,43 +30,104 @@ HTML_TEMPLATE = '''
|
|
| 30 |
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 31 |
text-align: center;
|
| 32 |
}
|
| 33 |
-
h1 {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
.subtitle { color: #666; margin-bottom: 30px; }
|
| 35 |
.search-form { display: flex; gap: 10px; flex-wrap: wrap; }
|
| 36 |
.search-box {
|
| 37 |
-
flex: 1;
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
.search-box:focus { outline: none; border-color: #667eea; }
|
| 41 |
button {
|
| 42 |
-
padding: 15px 30px;
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
.result {
|
| 46 |
-
background: white;
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
-
.title {
|
| 50 |
.source { color: #006621; font-size: 14px; }
|
| 51 |
.date { color: #808080; font-size: 12px; }
|
| 52 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
</style>
|
| 54 |
</head>
|
| 55 |
<body>
|
| 56 |
<div class="container">
|
| 57 |
<div class="header">
|
| 58 |
-
<h1>🔍 محرك البحث المتكامل</h1>
|
| 59 |
<p class="subtitle">بحث في الأخبار - الصور - الفيديوهات</p>
|
|
|
|
| 60 |
<form method="get" class="search-form">
|
| 61 |
-
<input type="text" name="q" class="search-box" placeholder="ابحث عن..." value="{{ query }}">
|
| 62 |
<button type="submit">🔍 بحث</button>
|
| 63 |
</form>
|
| 64 |
</div>
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
{% if query %}
|
| 67 |
-
<div class="
|
| 68 |
-
|
| 69 |
</div>
|
|
|
|
| 70 |
{% for result in results %}
|
| 71 |
<div class="result">
|
| 72 |
<a href="{{ result.link }}" class="title" target="_blank">
|
|
@@ -76,42 +137,112 @@ HTML_TEMPLATE = '''
|
|
| 76 |
<span class="source">{{ result.source }}</span>
|
| 77 |
<span class="date"> - {{ result.date }}</span>
|
| 78 |
</div>
|
| 79 |
-
<
|
| 80 |
</div>
|
| 81 |
{% endfor %}
|
| 82 |
{% endif %}
|
| 83 |
|
| 84 |
<div class="footer">
|
| 85 |
<p>🚀 تم النشر على Hugging Face Spaces باستخدام Docker</p>
|
|
|
|
| 86 |
</div>
|
| 87 |
</div>
|
| 88 |
</body>
|
| 89 |
</html>
|
| 90 |
'''
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def search_news(keyword):
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
WHERE search_terms LIKE ?
|
| 101 |
-
ORDER BY date DESC
|
| 102 |
-
LIMIT 30
|
| 103 |
-
''', (f'%{keyword}%',))
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
@app.route('/')
|
| 111 |
def index():
|
| 112 |
query = request.args.get('q', '')
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
@app.route('/api/search')
|
| 117 |
def api_search():
|
|
@@ -119,5 +250,34 @@ def api_search():
|
|
| 119 |
results = search_news(query)
|
| 120 |
return jsonify({'results': results, 'count': len(results)})
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
if __name__ == '__main__':
|
| 123 |
-
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, render_template_string, jsonify
|
| 2 |
import sqlite3
|
| 3 |
from datetime import datetime
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# قالب HTML مبسط وجميل
|
| 10 |
HTML_TEMPLATE = '''
|
| 11 |
<!DOCTYPE html>
|
| 12 |
<html dir="rtl">
|
|
|
|
| 17 |
<style>
|
| 18 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 19 |
body {
|
| 20 |
+
font-family: 'Segoe UI', Tahoma, Arial, sans-serif;
|
| 21 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 22 |
min-height: 100vh;
|
| 23 |
}
|
|
|
|
| 30 |
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 31 |
text-align: center;
|
| 32 |
}
|
| 33 |
+
h1 {
|
| 34 |
+
color: #333;
|
| 35 |
+
margin-bottom: 10px;
|
| 36 |
+
font-size: 2.5em;
|
| 37 |
+
}
|
| 38 |
.subtitle { color: #666; margin-bottom: 30px; }
|
| 39 |
.search-form { display: flex; gap: 10px; flex-wrap: wrap; }
|
| 40 |
.search-box {
|
| 41 |
+
flex: 1;
|
| 42 |
+
padding: 15px;
|
| 43 |
+
border: 2px solid #ddd;
|
| 44 |
+
border-radius: 10px;
|
| 45 |
+
font-size: 16px;
|
| 46 |
+
font-family: inherit;
|
| 47 |
}
|
| 48 |
.search-box:focus { outline: none; border-color: #667eea; }
|
| 49 |
button {
|
| 50 |
+
padding: 15px 30px;
|
| 51 |
+
background: #667eea;
|
| 52 |
+
color: white;
|
| 53 |
+
border: none;
|
| 54 |
+
border-radius: 10px;
|
| 55 |
+
cursor: pointer;
|
| 56 |
+
font-size: 16px;
|
| 57 |
+
transition: transform 0.2s;
|
| 58 |
+
}
|
| 59 |
+
button:hover { transform: scale(1.05); background: #5a67d8; }
|
| 60 |
+
.results-count {
|
| 61 |
+
background: white;
|
| 62 |
+
padding: 10px;
|
| 63 |
+
border-radius: 10px;
|
| 64 |
+
margin-bottom: 20px;
|
| 65 |
+
text-align: center;
|
| 66 |
}
|
| 67 |
.result {
|
| 68 |
+
background: white;
|
| 69 |
+
padding: 20px;
|
| 70 |
+
margin-bottom: 15px;
|
| 71 |
+
border-radius: 10px;
|
| 72 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 73 |
+
transition: transform 0.2s;
|
| 74 |
+
}
|
| 75 |
+
.result:hover { transform: translateX(-5px); }
|
| 76 |
+
.title {
|
| 77 |
+
color: #1a0dab;
|
| 78 |
+
text-decoration: none;
|
| 79 |
+
font-size: 18px;
|
| 80 |
+
font-weight: bold;
|
| 81 |
+
display: block;
|
| 82 |
+
margin-bottom: 8px;
|
| 83 |
}
|
| 84 |
+
.title:hover { text-decoration: underline; }
|
| 85 |
.source { color: #006621; font-size: 14px; }
|
| 86 |
.date { color: #808080; font-size: 12px; }
|
| 87 |
+
.summary { color: #545454; margin-top: 10px; line-height: 1.5; }
|
| 88 |
+
.footer {
|
| 89 |
+
text-align: center;
|
| 90 |
+
margin-top: 30px;
|
| 91 |
+
color: white;
|
| 92 |
+
padding: 20px;
|
| 93 |
+
}
|
| 94 |
+
.error {
|
| 95 |
+
background: #ffebee;
|
| 96 |
+
color: #c62828;
|
| 97 |
+
padding: 15px;
|
| 98 |
+
border-radius: 10px;
|
| 99 |
+
margin: 20px 0;
|
| 100 |
+
text-align: center;
|
| 101 |
+
}
|
| 102 |
+
@media (max-width: 600px) {
|
| 103 |
+
.search-form { flex-direction: column; }
|
| 104 |
+
.container { padding: 10px; }
|
| 105 |
+
}
|
| 106 |
</style>
|
| 107 |
</head>
|
| 108 |
<body>
|
| 109 |
<div class="container">
|
| 110 |
<div class="header">
|
| 111 |
+
<h1>📰 🔍 🎥 محرك البحث المتكامل</h1>
|
| 112 |
<p class="subtitle">بحث في الأخبار - الصور - الفيديوهات</p>
|
| 113 |
+
|
| 114 |
<form method="get" class="search-form">
|
| 115 |
+
<input type="text" name="q" class="search-box" placeholder="ابحث عن أخبار، صور، فيديوهات..." value="{{ query }}">
|
| 116 |
<button type="submit">🔍 بحث</button>
|
| 117 |
</form>
|
| 118 |
</div>
|
| 119 |
|
| 120 |
+
{% if error %}
|
| 121 |
+
<div class="error">
|
| 122 |
+
⚠️ {{ error }}
|
| 123 |
+
</div>
|
| 124 |
+
{% endif %}
|
| 125 |
+
|
| 126 |
{% if query %}
|
| 127 |
+
<div class="results-count">
|
| 128 |
+
📊 تم العثور على {{ results|length }} نتيجة عن "{{ query }}"
|
| 129 |
</div>
|
| 130 |
+
|
| 131 |
{% for result in results %}
|
| 132 |
<div class="result">
|
| 133 |
<a href="{{ result.link }}" class="title" target="_blank">
|
|
|
|
| 137 |
<span class="source">{{ result.source }}</span>
|
| 138 |
<span class="date"> - {{ result.date }}</span>
|
| 139 |
</div>
|
| 140 |
+
<div class="summary">{{ result.summary[:200] }}...</div>
|
| 141 |
</div>
|
| 142 |
{% endfor %}
|
| 143 |
{% endif %}
|
| 144 |
|
| 145 |
<div class="footer">
|
| 146 |
<p>🚀 تم النشر على Hugging Face Spaces باستخدام Docker</p>
|
| 147 |
+
<p>💡 جرب البحث عن: "تقنية"، "ذكاء اصطناعي"، "أخبار"</p>
|
| 148 |
</div>
|
| 149 |
</div>
|
| 150 |
</body>
|
| 151 |
</html>
|
| 152 |
'''
|
| 153 |
|
| 154 |
+
def get_db_connection():
|
| 155 |
+
"""الحصول على اتصال بقاعدة البيانات"""
|
| 156 |
+
try:
|
| 157 |
+
conn = sqlite3.connect('news_database.db')
|
| 158 |
+
conn.row_factory = sqlite3.Row
|
| 159 |
+
return conn
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print(f"خطأ في الاتصال بقاعدة البيانات: {e}")
|
| 162 |
+
return None
|
| 163 |
+
|
| 164 |
+
def init_sample_data():
|
| 165 |
+
"""إضافة بيانات تجريبية إذا كانت قاعدة البيانات فارغة"""
|
| 166 |
+
conn = get_db_connection()
|
| 167 |
+
if conn:
|
| 168 |
+
cursor = conn.cursor()
|
| 169 |
+
|
| 170 |
+
# التحقق من وجود بيانات
|
| 171 |
+
cursor.execute("SELECT COUNT(*) FROM news")
|
| 172 |
+
count = cursor.fetchone()[0]
|
| 173 |
+
|
| 174 |
+
if count == 0:
|
| 175 |
+
print("إضافة بيانات تجريبية...")
|
| 176 |
+
sample_news = [
|
| 177 |
+
('خبر 1: تقنية جديدة في الذكاء الاصطناعي', 'https://example.com/1', 'اكتشافات جديدة في عالم الذكاء الاصطناعي', 'وكالة التقنية', datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
|
| 178 |
+
('خبر 2: تطور محركات البحث', 'https://example.com/2', 'جيل جديد من محركات البحث يعتمد على الذكاء الاصطناعي', 'بوابة التكنولوجيا', datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
|
| 179 |
+
('خبر 3: أخبار الجزائر', 'https://example.com/3', 'آخر التطورات في الجزائر', 'وكالة الأنباء', datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
|
| 180 |
+
]
|
| 181 |
+
|
| 182 |
+
for title, link, summary, source, date in sample_news:
|
| 183 |
+
import hashlib
|
| 184 |
+
news_id = hashlib.md5(link.encode()).hexdigest()
|
| 185 |
+
search_terms = f"{title} {summary} {source}".lower()
|
| 186 |
+
|
| 187 |
+
cursor.execute('''
|
| 188 |
+
INSERT OR REPLACE INTO news (id, title, link, summary, source, date, search_terms)
|
| 189 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 190 |
+
''', (news_id, title, link, summary, source, date, search_terms))
|
| 191 |
+
|
| 192 |
+
conn.commit()
|
| 193 |
+
print("✅ تم إضافة بيانات تجريبية")
|
| 194 |
+
|
| 195 |
+
conn.close()
|
| 196 |
+
|
| 197 |
def search_news(keyword):
|
| 198 |
+
"""البحث في الأخبار"""
|
| 199 |
+
if not keyword or keyword.strip() == '':
|
| 200 |
+
return []
|
| 201 |
|
| 202 |
+
conn = get_db_connection()
|
| 203 |
+
if not conn:
|
| 204 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
cursor = conn.cursor()
|
| 207 |
+
keyword = keyword.lower()
|
| 208 |
|
| 209 |
+
try:
|
| 210 |
+
cursor.execute('''
|
| 211 |
+
SELECT title, link, summary, source, date
|
| 212 |
+
FROM news
|
| 213 |
+
WHERE search_terms LIKE ?
|
| 214 |
+
ORDER BY date DESC
|
| 215 |
+
LIMIT 30
|
| 216 |
+
''', (f'%{keyword}%',))
|
| 217 |
+
|
| 218 |
+
results = cursor.fetchall()
|
| 219 |
+
conn.close()
|
| 220 |
+
|
| 221 |
+
return [{'title': r[0], 'link': r[1], 'summary': r[2], 'source': r[3], 'date': r[4]} for r in results]
|
| 222 |
+
except Exception as e:
|
| 223 |
+
print(f"خطأ في البحث: {e}")
|
| 224 |
+
conn.close()
|
| 225 |
+
return []
|
| 226 |
|
| 227 |
@app.route('/')
|
| 228 |
def index():
|
| 229 |
query = request.args.get('q', '')
|
| 230 |
+
error = None
|
| 231 |
+
results = []
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
if query:
|
| 235 |
+
results = search_news(query)
|
| 236 |
+
except Exception as e:
|
| 237 |
+
error = f"حدث خطأ: {str(e)}"
|
| 238 |
+
print(f"خطأ: {e}")
|
| 239 |
+
|
| 240 |
+
return render_template_string(HTML_TEMPLATE, query=query, results=results, error=error)
|
| 241 |
+
|
| 242 |
+
@app.route('/health')
|
| 243 |
+
def health():
|
| 244 |
+
"""نقطة نهاية للتحقق من صحة ا��تطبيق"""
|
| 245 |
+
return jsonify({'status': 'ok', 'message': 'محرك البحث يعمل بشكل طبيعي'})
|
| 246 |
|
| 247 |
@app.route('/api/search')
|
| 248 |
def api_search():
|
|
|
|
| 250 |
results = search_news(query)
|
| 251 |
return jsonify({'results': results, 'count': len(results)})
|
| 252 |
|
| 253 |
+
# تهيئة قاعدة البيانات عند بدء التشغيل
|
| 254 |
+
with app.app_context():
|
| 255 |
+
print("🔄 جاري تهيئة محرك البحث...")
|
| 256 |
+
# التحقق من وجود قاعدة البيانات
|
| 257 |
+
if not os.path.exists('news_database.db'):
|
| 258 |
+
conn = get_db_connection()
|
| 259 |
+
if conn:
|
| 260 |
+
cursor = conn.cursor()
|
| 261 |
+
cursor.execute('''
|
| 262 |
+
CREATE TABLE IF NOT EXISTS news (
|
| 263 |
+
id TEXT PRIMARY KEY,
|
| 264 |
+
title TEXT,
|
| 265 |
+
link TEXT,
|
| 266 |
+
summary TEXT,
|
| 267 |
+
source TEXT,
|
| 268 |
+
date TEXT,
|
| 269 |
+
search_terms TEXT
|
| 270 |
+
)
|
| 271 |
+
''')
|
| 272 |
+
conn.commit()
|
| 273 |
+
conn.close()
|
| 274 |
+
print("✅ تم إنشاء قاعدة البيانات")
|
| 275 |
+
|
| 276 |
+
# إضافة بيانات تجريبية
|
| 277 |
+
init_sample_data()
|
| 278 |
+
print("✅ محرك البحث جاهز للاستخدام!")
|
| 279 |
+
|
| 280 |
if __name__ == '__main__':
|
| 281 |
+
# للحصول على أفضل أداء على Hugging Face Spaces
|
| 282 |
+
port = int(os.environ.get('PORT', 7860))
|
| 283 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|