Spaces:
Sleeping
Sleeping
Update init_db.py
Browse files- init_db.py +61 -18
init_db.py
CHANGED
|
@@ -1,6 +1,28 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def initialize():
|
| 6 |
print("🔄 جاري تهيئة قاعدة البيانات...")
|
|
@@ -10,23 +32,44 @@ def initialize():
|
|
| 10 |
news_list = fetch_news_from_aljazeera()
|
| 11 |
|
| 12 |
if news_list:
|
| 13 |
-
save_news_to_db(news_list)
|
| 14 |
-
print(f"✅ تم حفظ {
|
| 15 |
else:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
print("✅ اكتمل التهيئة!")
|
| 30 |
|
| 31 |
if __name__ == '__main__':
|
| 32 |
initialize()
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# إضافة المجلد الحالي إلى المسار
|
| 5 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
from crawler import fetch_news_from_aljazeera
|
| 9 |
+
from indexer import create_database, save_news_to_db, save_sample_images
|
| 10 |
+
except ImportError as e:
|
| 11 |
+
print(f"⚠️ خطأ في الاستيراد: {e}")
|
| 12 |
+
# تعريف دوال بديلة في حالة الخطأ
|
| 13 |
+
def fetch_news_from_aljazeera():
|
| 14 |
+
return []
|
| 15 |
+
def create_database():
|
| 16 |
+
import sqlite3
|
| 17 |
+
conn = sqlite3.connect('news_database.db')
|
| 18 |
+
cursor = conn.cursor()
|
| 19 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS news (id TEXT PRIMARY KEY, title TEXT, link TEXT, summary TEXT, source TEXT, date TEXT, search_terms TEXT)''')
|
| 20 |
+
conn.commit()
|
| 21 |
+
conn.close()
|
| 22 |
+
def save_news_to_db(news_list):
|
| 23 |
+
pass
|
| 24 |
+
def save_sample_images():
|
| 25 |
+
pass
|
| 26 |
|
| 27 |
def initialize():
|
| 28 |
print("🔄 جاري تهيئة قاعدة البيانات...")
|
|
|
|
| 32 |
news_list = fetch_news_from_aljazeera()
|
| 33 |
|
| 34 |
if news_list:
|
| 35 |
+
saved = save_news_to_db(news_list)
|
| 36 |
+
print(f"✅ تم حفظ {saved} خبر")
|
| 37 |
else:
|
| 38 |
+
print("⚠️ لم يتم جلب أخبار، سيتم استخدام بيانات تجريبية")
|
| 39 |
+
# إضافة خبر تجريبي يدويًا
|
| 40 |
+
import sqlite3
|
| 41 |
+
import hashlib
|
| 42 |
+
from datetime import datetime
|
| 43 |
+
|
| 44 |
+
conn = sqlite3.connect('news_database.db')
|
| 45 |
+
cursor = conn.cursor()
|
| 46 |
+
|
| 47 |
+
sample = {
|
| 48 |
+
'title': 'محرك بحث جديد - مرحباً بك!',
|
| 49 |
+
'link': 'https://huggingface.co/spaces',
|
| 50 |
+
'summary': 'هذا محرك بحث تجريبي للأخبار والصور والفيديوهات',
|
| 51 |
+
'source': 'Hugging Face',
|
| 52 |
+
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
news_id = hashlib.md5(sample['link'].encode()).hexdigest()
|
| 56 |
+
search_terms = ' '.join([sample['title'], sample['summary'], sample['source']]).lower()
|
| 57 |
+
|
| 58 |
+
cursor.execute('''
|
| 59 |
+
INSERT OR REPLACE INTO news
|
| 60 |
+
(id, title, link, summary, source, date, search_terms)
|
| 61 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 62 |
+
''', (news_id, sample['title'], sample['link'], sample['summary'],
|
| 63 |
+
sample['source'], sample['date'], search_terms))
|
| 64 |
+
|
| 65 |
+
conn.commit()
|
| 66 |
+
conn.close()
|
| 67 |
+
print("✅ تم إضافة خبر تجريبي")
|
| 68 |
+
|
| 69 |
+
print("🔄 إضافة صور تجريبية...")
|
| 70 |
+
save_sample_images()
|
| 71 |
|
| 72 |
+
print("✅ اكتمل التهيئة بنجاح!")
|
| 73 |
|
| 74 |
if __name__ == '__main__':
|
| 75 |
initialize()
|