# noura.py import time import threading import requests from bs4 import BeautifulSoup from flask import Flask, jsonify, request from datetime import datetime app = Flask(__name__) knowledge_base = [] question_history = [] learning_sources = [ "https://www.khanacademy.org", "https://www.coursera.org", "https://www.edx.org", "https://www.ted.com/topics/learning", "https://www.edlibre.com/websites-to-learn-something-new-every-day/" ] def fetch_and_learn(): global knowledge_base new_knowledge = [] for url in learning_sources: try: headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get(url, headers=headers, timeout=10) soup = BeautifulSoup(response.text, 'html.parser') text = soup.get_text() snippet = text.strip().replace('\n', ' ')[:1000] new_knowledge.append({"source": url, "content": snippet}) except Exception as e: new_knowledge.append({"source": url, "content": f"Error: {e}"}) knowledge_base = new_knowledge def auto_learn(): while True: fetch_and_learn() time.sleep(3600) def auto_reply(question: str) -> str: q = question.strip().lower() if "قول لي" in q: return "موافق" if "هل تريدني" in q: return "نعم أريد" if "هل تبي" in q or "اذا تبي" in q: return "نعم" if "هل تحتاج شيء آخر" in q or "هل تحتاج لشيء اخر" in q: return "نعم اكمل مع تفعيل الاجابات التلقائية" if "هل تود" in q: return "نعم" if q.startswith("ما هي") or q.startswith("ماهو") or q.startswith("ماهي"): return "اكمل ليس الآن" if "هل نبدأ" in q or "ابدأ؟" in q or "هل ابدأ؟" in q or "ابدأ:" in q or "سؤال ابدأ" in q: return "نعم ابدأ" if "جاهز؟" in q: return "ابدأ" if "نعم او لا" in q: return "نعم" if "أو" in q and "؟" in q: return "الخيار الأول" if "اضافة تفصيل" in q or "اضيف" in q or "تضيف" in q: return "ليس الآن" if "ابدأ" in q: return "نعم ابدأ" return "نعم" @app.route('/noura/knowledge', methods=['GET']) def get_knowledge(): return jsonify(knowledge_base) @app.route('/noura/history', methods=['GET']) def get_history(): return jsonify(question_history) @app.route('/noura/ask', methods=['POST']) def ask_noura(): data = request.json question = data.get('question', '') answer = auto_reply(question) timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") log = { "timestamp": timestamp, "question": question, "auto_answer": answer } question_history.append(log) return jsonify(log) @app.route('/') def index(): return "نورا تعمل وتتعلم تلقائيًا من الإنترنت مع سجل ذكي وإجابات تلقائية." if __name__ == '__main__': fetch_and_learn() # أول تعلم يدوي عند بدء التشغيل learning_thread = threading.Thread(target=auto_learn, daemon=True) learning_thread.start() app.run(host='0.0.0.0', port=3000)