Spaces:
Sleeping
Sleeping
Create indexer.py
Browse files- indexer.py +119 -0
indexer.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import hashlib
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
def create_database():
|
| 6 |
+
"""إنشاء قاعدة البيانات وجداولها"""
|
| 7 |
+
conn = sqlite3.connect('news_database.db')
|
| 8 |
+
cursor = conn.cursor()
|
| 9 |
+
|
| 10 |
+
# جدول الأخبار
|
| 11 |
+
cursor.execute('''
|
| 12 |
+
CREATE TABLE IF NOT EXISTS news (
|
| 13 |
+
id TEXT PRIMARY KEY,
|
| 14 |
+
title TEXT NOT NULL,
|
| 15 |
+
link TEXT UNIQUE,
|
| 16 |
+
summary TEXT,
|
| 17 |
+
source TEXT,
|
| 18 |
+
date TEXT,
|
| 19 |
+
search_terms TEXT
|
| 20 |
+
)
|
| 21 |
+
''')
|
| 22 |
+
|
| 23 |
+
# جدول للصور
|
| 24 |
+
cursor.execute('''
|
| 25 |
+
CREATE TABLE IF NOT EXISTS images (
|
| 26 |
+
id TEXT PRIMARY KEY,
|
| 27 |
+
title TEXT,
|
| 28 |
+
image_url TEXT,
|
| 29 |
+
source_url TEXT,
|
| 30 |
+
thumbnail TEXT,
|
| 31 |
+
date TEXT,
|
| 32 |
+
search_terms TEXT
|
| 33 |
+
)
|
| 34 |
+
''')
|
| 35 |
+
|
| 36 |
+
# جدول للفيديوهات
|
| 37 |
+
cursor.execute('''
|
| 38 |
+
CREATE TABLE IF NOT EXISTS videos (
|
| 39 |
+
id TEXT PRIMARY KEY,
|
| 40 |
+
title TEXT,
|
| 41 |
+
video_url TEXT,
|
| 42 |
+
thumbnail TEXT,
|
| 43 |
+
duration TEXT,
|
| 44 |
+
source TEXT,
|
| 45 |
+
date TEXT,
|
| 46 |
+
search_terms TEXT
|
| 47 |
+
)
|
| 48 |
+
''')
|
| 49 |
+
|
| 50 |
+
conn.commit()
|
| 51 |
+
conn.close()
|
| 52 |
+
print("✅ تم إنشاء قاعدة البيانات بنجاح")
|
| 53 |
+
|
| 54 |
+
def save_news_to_db(news_list):
|
| 55 |
+
"""حفظ الأخبار في قاعدة البيانات"""
|
| 56 |
+
conn = sqlite3.connect('news_database.db')
|
| 57 |
+
cursor = conn.cursor()
|
| 58 |
+
count = 0
|
| 59 |
+
|
| 60 |
+
for news in news_list:
|
| 61 |
+
# إنشاء معرف فريد للخبر
|
| 62 |
+
news_id = hashlib.md5(news['link'].encode()).hexdigest()
|
| 63 |
+
|
| 64 |
+
# تحضير كلمات البحث
|
| 65 |
+
search_terms = ' '.join([news['title'], news['summary'], news['source']]).lower()
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
cursor.execute('''
|
| 69 |
+
INSERT OR REPLACE INTO news
|
| 70 |
+
(id, title, link, summary, source, date, search_terms)
|
| 71 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 72 |
+
''', (news_id, news['title'], news['link'], news['summary'],
|
| 73 |
+
news['source'], news['date'], search_terms))
|
| 74 |
+
count += 1
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"خطأ في حفظ الخبر: {e}")
|
| 77 |
+
|
| 78 |
+
conn.commit()
|
| 79 |
+
conn.close()
|
| 80 |
+
print(f"✅ تم حفظ {count} خبر في قاعدة البيانات")
|
| 81 |
+
return count
|
| 82 |
+
|
| 83 |
+
def save_sample_images():
|
| 84 |
+
"""حفظ صور تجريبية"""
|
| 85 |
+
conn = sqlite3.connect('news_database.db')
|
| 86 |
+
cursor = conn.cursor()
|
| 87 |
+
|
| 88 |
+
sample_images = [
|
| 89 |
+
{
|
| 90 |
+
'title': 'صورة تجريبية 1',
|
| 91 |
+
'image_url': 'https://picsum.photos/id/1/200/150',
|
| 92 |
+
'thumbnail': 'https://picsum.photos/id/1/100/75',
|
| 93 |
+
'source_url': '#'
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
'title': 'صورة تجريبية 2',
|
| 97 |
+
'image_url': 'https://picsum.photos/id/2/200/150',
|
| 98 |
+
'thumbnail': 'https://picsum.photos/id/2/100/75',
|
| 99 |
+
'source_url': '#'
|
| 100 |
+
}
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
for img in sample_images:
|
| 104 |
+
img_id = hashlib.md5(img['image_url'].encode()).hexdigest()
|
| 105 |
+
search_terms = img['title'].lower()
|
| 106 |
+
|
| 107 |
+
cursor.execute('''
|
| 108 |
+
INSERT OR IGNORE INTO images
|
| 109 |
+
(id, title, image_url, thumbnail, source_url, date, search_terms)
|
| 110 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 111 |
+
''', (img_id, img['title'], img['image_url'], img['thumbnail'],
|
| 112 |
+
img['source_url'], datetime.now().strftime('%Y-%m-%d'), search_terms))
|
| 113 |
+
|
| 114 |
+
conn.commit()
|
| 115 |
+
conn.close()
|
| 116 |
+
print("✅ تم حفظ صور تجريبية")
|
| 117 |
+
|
| 118 |
+
if __name__ == '__main__':
|
| 119 |
+
create_database()
|