import logging import requests from bs4 import BeautifulSoup logger = logging.getLogger("ONYX") class WebEngine: def __init__(self): self.headers = { "User-Agent": "Mozilla/5.0 (ONYX AI Assistant)" } # ========================= # SEARCH WEB # ========================= def search(self, query: str): try: url = f"https://duckduckgo.com/html/?q={query}" response = requests.get( url, headers=self.headers, timeout=10 ) soup = BeautifulSoup(response.text, "html.parser") results = [] for result in soup.select(".result__a")[:5]: title = result.get_text() link = result.get("href") results.append({ "title": title, "link": link }) return results except Exception as e: logger.error(f"Web search error: {e}") return [] # ========================= # EXTRACT PAGE CONTENT # ========================= def extract_text(self, url: str): try: response = requests.get( url, headers=self.headers, timeout=10 ) soup = BeautifulSoup(response.text, "html.parser") paragraphs = soup.find_all("p") text = "" for p in paragraphs[:10]: text += p.get_text() + "\n" return text.strip() except Exception as e: logger.error(f"Web extract error: {e}") return "" # ========================= # ANALYZE TREND # ========================= def analyze_trend(self, query: str): results = self.search(query) if not results: return "No trend data found." analysis = f"Trend analysis for: {query}\n\n" for r in results: analysis += f"- {r['title']}\n" return analysis