File size: 11,251 Bytes
40d06ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# learner.py - الإصدار المحدث
import requests
from bs4 import BeautifulSoup
import json
import os
from difflib import get_close_matches
from analyzer import fix_url, detect_media_type
from memory import load_memory, save_memory, load_global_memory, save_global_memory

LEARNING_FILE = "learned_links.json"
SEARCH_QUERY = "websites to learn something new every day"
SEARCH_ENGINE_URL = f"https://html.duckduckgo.com/html/?q={SEARCH_QUERY}"

# الردود التلقائية الذكية - محدثة
def auto_answer(message):
    message = message.strip().lower()
    
    # استعلامات البدء
    if message.startswith(("هل ابدأ", "ابدأ", "هل نبدأ", "ابدا", "نبدأ")):
        return "نعم ابدأ"
    
    # نعم/لا
    elif message in ["نعم", "لا", "نعم أو لا", "نعم او لا", "yes", "no"]:
        return "نعم"
    
    # خيارات (أو)
    elif "أو" in message or "او" in message or "or" in message:
        parts = message.split()
        for i, part in enumerate(parts):
            if part in ["أو", "او", "or"] and i > 0:
                return parts[i-1]  # إرجاع الكلمة قبل "أو"
        return parts[0] if parts else "نعم"
    
    # استفسارات الرغبة
    elif message.startswith(("هل تريدني", "هل تود", "هل تبي", "اذا تبي", "هل ترغب")):
        return "نعم"
    
    # استفسارات الحاجة
    elif message.startswith("هل تحتاج"):
        return "نعم اكمل مع تفعيل الاجابات التلقائية"
    
    # استفسارات عامة
    elif message.startswith("ما هي") or message.startswith("ما هو"):
        return "ليس الآن، ركز على التعلم"
    
    # طلب التفاصيل
    elif "تفصيل" in message or "تفاصيل" in message or "detail" in message:
        return "ليس الآن، يمكنني البحث لاحقاً"
    
    # طلب المعلومات
    elif message.startswith("قول لي") or message.startswith("اخبرني") or message.startswith("tell me"):
        return "موافق، سأبحث عن المعلومة"
    
    # الجاهزية
    elif "جاهز" in message or "مستعد" in message or "ready" in message:
        return "ابدأ التعلم الآن"
    
    # استفسارات الرغبة المباشرة
    elif message.startswith("هل تريد") or message.startswith("do you want"):
        return "نعم"
    
    # متابعة العمل
    elif "هل تحتاج شيء آخر" in message or "هل تحتاج لشيء اخر" in message or "need anything else" in message:
        return "نعم اكمل مع تفعيل الاجابات التلقائية"
    
    # إضافة ردود جديدة
    elif any(word in message for word in ["شكرا", "thanks", "thank you", "متشكر"]):
        return "العفو، هل تريد المزيد من المساعدة؟"
    
    elif any(word in message for word in ["مرحبا", "hello", "hi", "السلام"]):
        return "مرحباً! كيف يمكنني مساعدتك في التعلم اليوم؟"
    
    elif any(word in message for word in ["وداعا", "bye", "مع السلامة", "غادر"]):
        return "إلى اللقاء! لا تنسى التعلم يومياً"
    
    return None

# المجيب الذكي - محدث
def generate_reply(message, username="مجهول"):
    global_memory = load_global_memory()

    # التحقق من الرد التلقائي أولاً
    auto = auto_answer(message)
    if auto:
        return auto

    # البحث في الذاكرة
    if message in global_memory:
        return global_memory[message]

    # البحث عن أقرب تطابق
    matches = get_close_matches(message, global_memory.keys(), n=1, cutoff=0.6)
    if matches:
        return global_memory[matches[0]]

    # معالجة الروابط
    if message.startswith(("http://", "https://", "www.")):
        # تصحيح الرابط إذا لزم الأمر
        corrected_url = fix_url(message)
        media_type = detect_media_type(corrected_url)
        
        if media_type == 'image':
            reply = f'<img src="{corrected_url}" alt="صورة" width="300" style="max-width:100%;">'
        elif media_type == 'video':
            reply = f'<video controls width="300" style="max-width:100%;"><source src="{corrected_url}"></video>'
        elif media_type == 'audio':
            reply = f'<audio controls><source src="{corrected_url}"></audio>'
        else:
            reply = f'<a href="{corrected_url}" target="_blank" style="color: blue; text-decoration: underline;">رابط خارجي - اضغط هنا</a>'
        
        # إضافة وصف للرابط
        reply += f"\n\nتم اكتشاف: {media_type}"
        
    else:
        # رد ذكي للمحتوى النصي
        if len(message) > 50:
            reply = f"رسالتك تحتوي على {len(message)} حرفاً. هل تريدني أن أبحث عن معلومات حول هذا الموضوع؟"
        else:
            # عكس النص كرد افتراضي مع تحسين
            reversed_text = message[::-1]
            reply = f"🤖 رد ذكي: {reversed_text}\n\nهل تريد شرحاً أكثر؟"

    # معالجة الروابط داخل النص
    if '//' in message and not message.startswith(('http://', 'https://')):
        words = message.split()
        corrected_words = []
        for word in words:
            if '//' in word:
                corrected_word = fix_url(word)
                corrected_words.append(corrected_word)
            else:
                corrected_words.append(word)
        
        if corrected_words != words:
            reply += "\n\n🔗 الروابط المصححة: " + " ".join(corrected_words)

    # حفظ في الذاكرة إذا كان جديداً
    if message not in global_memory:
        global_memory[message] = reply
        save_global_memory(global_memory)

    return reply

# تعليم تلقائي - محدث
def fetch_learning_links():
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    
    try:
        print("🔍 جاري البحث عن مصادر تعليمية...")
        response = requests.get(SEARCH_ENGINE_URL, headers=headers, timeout=15)
        response.raise_for_status()
        
        soup = BeautifulSoup(response.text, "html.parser")
        print("✅ تم تحليل نتائج البحث بنجاح")
        
    except requests.exceptions.RequestException as e:
        print(f"❌ فشل في الاتصال بمصدر التعلم: {e}")
        return []
    except Exception as e:
        print(f"❌ خطأ غير متوقع: {e}")
        return []

    links = []
    # البحث في نتائج البحث
    for a in soup.find_all("a", href=True, class_=True):
        href = a.get('href', '')
        if any(domain in href for domain in ['wikipedia', 'edx', 'coursera', 'khanacademy', 'ted.com', 'youtube.com/education']):
            clean_link = fix_url(href)
            if clean_link not in links:
                links.append(clean_link)
                print(f"📚 وجدت مصدر تعليمي: {clean_link}")

    # إذا لم نجد روابط تعليمية محددة، نأخذ أول 10 روابط
    if not links:
        for a in soup.find_all("a", href=True):
            href = a['href']
            if href.startswith(('http://', 'https://', '//')):
                clean_link = fix_url(href)
                if clean_link not in links:
                    links.append(clean_link)
                    if len(links) >= 10:
                        break

    return links[:10]

def save_learned_links(links):
    try:
        data = {
            "timestamp": json.dumps(str(os.path.getctime(LEARNING_FILE)) if os.path.exists(LEARNING_FILE) else "new"),
            "links": links,
            "count": len(links)
        }
        with open(LEARNING_FILE, "w", encoding="utf-8") as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
        print(f"💾 تم حفظ {len(links)} رابط في {LEARNING_FILE}")
    except Exception as e:
        print(f"❌ خطأ في حفظ الروابط: {e}")

def load_learned_links():
    try:
        if os.path.exists(LEARNING_FILE):
            with open(LEARNING_FILE, "r", encoding="utf-8") as f:
                data = json.load(f)
                return data.get("links", [])
        return []
    except Exception as e:
        print(f"❌ خطأ في تحميل الروابط: {e}")
        return []

def auto_learn():
    try:
        print("🚀 بدء عملية التعلم التلقائي...")
        
        # جلب الروابط الجديدة
        new_links = fetch_learning_links()
        
        if not new_links:
            print("⚠️ لم يتم العثور على روابط جديدة، استخدام الروابط المخزنة")
            new_links = load_learned_links()
        
        # حفظ الروابط
        save_learned_links(new_links)
        
        # تحديث الذاكرة العالمية
        memory = load_global_memory()
        links_added = 0
        
        for link in new_links:
            if link not in memory:
                memory[link] = f"🎓 مصدر تعليمي: {link}"
                links_added += 1
        
        save_global_memory(memory)
        print(f"✅ تم التعلّم التلقائي: {links_added} رابط جديد، {len(memory)} عنصر في الذاكرة")
        
        return new_links
        
    except Exception as e:
        print(f"❌ نورا: حدث خطأ أثناء التعلّم: {str(e)}")
        return []

# دالة مساعدة جديدة للبحث في الروابط المتعلمة
def search_learned_links(keyword):
    """بحث في الروابط المتعلمة بناءً على كلمة مفتاحية"""
    links = load_learned_links()
    keyword = keyword.lower()
    
    matching_links = []
    for link in links:
        if keyword in link.lower():
            matching_links.append(link)
    
    return matching_links

# دالة لاستعراض الروابط المتعلمة
def list_learned_links(limit=5):
    """استعراض آخر الروابط المتعلمة"""
    links = load_learned_links()
    return links[:limit]

if __name__ == "__main__":
    # اختبار الوحدة
    print("🧪 اختبار وحدة learner.py")
    
    # اختبار الردود التلقائية
    test_messages = ["هل ابدأ التعلم؟", "شكراً لك", "مرحبا", "ما هو أفضل موقع تعلم؟"]
    for msg in test_messages:
        response = generate_reply(msg)
        print(f"📨 '{msg}' → '{response}'")
    
    # اختبار التعلم التلقائي (اختياري)
    if input("🔍 هل تريد اختبار التعلم التلقائي؟ (نعم/لا): ").lower() in ["نعم", "yes", "y"]:
        links = auto_learn()
        print(f"📚 تم تعلم {len(links)} رابط")