File size: 15,759 Bytes
fea1bd1 |
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
"""
Real Web Search Tools - Updated for duckduckgo-search v8.1.1 (2025)
HASHIRU 6.1 - Internet access with real search capabilities
"""
import asyncio
import json
import httpx
from typing import Dict, Any, List, Optional
from datetime import datetime
from pathlib import Path
# Import DuckDuckGo search (latest version)
try:
from duckduckgo_search import DDGS
DDG_AVAILABLE = True
print("✅ DuckDuckGo search library v8.1.1+ loaded")
except ImportError:
DDG_AVAILABLE = False
print("⚠️ DuckDuckGo search not available. Install: pip install duckduckgo-search")
async def handle_search(query: str, max_results: int = 10) -> Dict[str, Any]:
"""
Real internet search using DuckDuckGo (v8.1.1+ API)
Usage: /search <query> [max_results]
"""
if not query.strip():
return {"error": "Search query cannot be empty"}
if not DDG_AVAILABLE:
return {"error": "DuckDuckGo search library not installed. Run: pip install duckduckgo-search"}
try:
# Use context manager for DDGS (recommended in v8+)
with DDGS() as ddgs:
# Perform search - new API doesn't use max_results in function call
search_results = ddgs.text(
keywords=query,
region="us-en",
safesearch="moderate"
)
# Limit results manually (new API behavior)
results = []
for i, result in enumerate(search_results):
if i >= max_results:
break
results.append({
"position": i + 1,
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", ""),
"domain": result.get("href", "").split('/')[2] if result.get("href") else ""
})
# Save search results to free path
search_data = {
"query": query,
"timestamp": datetime.now().isoformat(),
"total_results": len(results),
"results": results,
"api_version": "duckduckgo-search v8.1.1+"
}
# Save to free project folder
free_path = Path(r"C:\meu_projeto_livre")
free_path.mkdir(exist_ok=True)
search_file = free_path / f"search_{query.replace(' ', '_')[:20]}_{int(datetime.now().timestamp())}.json"
with open(search_file, 'w', encoding='utf-8') as f:
json.dump(search_data, f, indent=2, ensure_ascii=False)
return {
"success": True,
"query": query,
"results": results,
"total_found": len(results),
"search_engine": "DuckDuckGo v8.1.1+",
"saved_to": str(search_file),
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"Search failed: {str(e)}"}
async def handle_news(topic: str = "technology", max_results: int = 5) -> Dict[str, Any]:
"""
Search for news using updated API
Usage: /news [topic] [max_results]
"""
if not DDG_AVAILABLE:
return {"error": "DuckDuckGo search library not installed"}
try:
with DDGS() as ddgs:
# News search with new API
news_results = ddgs.news(
keywords=topic,
region="us-en",
safesearch="moderate"
)
articles = []
for i, article in enumerate(news_results):
if i >= max_results:
break
articles.append({
"position": i + 1,
"title": article.get("title", ""),
"url": article.get("url", ""),
"source": article.get("source", ""),
"published": article.get("date", ""),
"snippet": article.get("body", "")
})
# Save news to free path
news_data = {
"topic": topic,
"timestamp": datetime.now().isoformat(),
"articles": articles,
"total_articles": len(articles),
"api_version": "duckduckgo-search v8.1.1+"
}
free_path = Path(r"C:\meu_projeto_livre")
news_file = free_path / f"news_{topic.replace(' ', '_')[:15]}_{int(datetime.now().timestamp())}.json"
with open(news_file, 'w', encoding='utf-8') as f:
json.dump(news_data, f, indent=2, ensure_ascii=False)
return {
"success": True,
"topic": topic,
"articles": articles,
"total_found": len(articles),
"saved_to": str(news_file),
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"News search failed: {str(e)}"}
async def handle_images(query: str, max_results: int = 10) -> Dict[str, Any]:
"""
Search for images using updated API
Usage: /images <query> [max_results]
"""
if not query.strip():
return {"error": "Image search query cannot be empty"}
if not DDG_AVAILABLE:
return {"error": "DuckDuckGo search library not installed"}
try:
with DDGS() as ddgs:
# Image search with new API
image_results = ddgs.images(
keywords=query,
region="us-en",
safesearch="moderate"
)
images = []
for i, img in enumerate(image_results):
if i >= max_results:
break
images.append({
"position": i + 1,
"title": img.get("title", ""),
"image_url": img.get("image", ""),
"thumbnail": img.get("thumbnail", ""),
"source_url": img.get("url", ""),
"width": img.get("width", 0),
"height": img.get("height", 0)
})
return {
"success": True,
"query": query,
"images": images,
"total_found": len(images),
"search_engine": "DuckDuckGo Images v8.1.1+",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"Image search failed: {str(e)}"}
async def handle_videos(query: str, max_results: int = 10) -> Dict[str, Any]:
"""
Search for videos using updated API
Usage: /videos <query> [max_results]
"""
if not query.strip():
return {"error": "Video search query cannot be empty"}
if not DDG_AVAILABLE:
return {"error": "DuckDuckGo search library not installed"}
try:
with DDGS() as ddgs:
# Video search with new API
video_results = ddgs.videos(
keywords=query,
region="us-en",
safesearch="moderate"
)
videos = []
for i, video in enumerate(video_results):
if i >= max_results:
break
videos.append({
"position": i + 1,
"title": video.get("title", ""),
"video_url": video.get("content", ""),
"thumbnail": video.get("image", ""),
"duration": video.get("duration", ""),
"published": video.get("published", ""),
"publisher": video.get("publisher", "")
})
return {
"success": True,
"query": query,
"videos": videos,
"total_found": len(videos),
"search_engine": "DuckDuckGo Videos v8.1.1+",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"Video search failed: {str(e)}"}
async def handle_instant_answer(query: str) -> Dict[str, Any]:
"""
Get instant answers/facts from DuckDuckGo
Usage: /instant <query>
"""
if not query.strip():
return {"error": "Query cannot be empty"}
if not DDG_AVAILABLE:
return {"error": "DuckDuckGo search library not installed"}
try:
with DDGS() as ddgs:
# Try to get instant answer
answer_results = ddgs.answers(query)
answers = []
for answer in answer_results:
answers.append({
"text": answer.get("text", ""),
"url": answer.get("url", ""),
"source": answer.get("source", "")
})
break # Usually just one answer
if not answers:
return {"error": "No instant answer found", "query": query}
return {
"success": True,
"query": query,
"instant_answer": answers[0],
"search_engine": "DuckDuckGo Instant Answers",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"Instant answer failed: {str(e)}"}
async def handle_browse(url: str) -> Dict[str, Any]:
"""
Fetch content from a specific URL
Usage: /browse <URL>
"""
if not url.strip():
return {"error": "URL cannot be empty"}
# Add https:// if no protocol
if not url.startswith(('http://', 'https://')):
url = f"https://{url}"
try:
headers = {
"User-Agent": "HASHIRU-6.1-Agent",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
}
async with httpx.AsyncClient(timeout=30.0, headers=headers) as client:
response = await client.get(url)
response.raise_for_status()
content = response.text
content_preview = content[:2000] + "..." if len(content) > 2000 else content
# Save content to free path
free_path = Path(r"C:\meu_projeto_livre")
domain = url.split('/')[2] if '/' in url else url.replace(':', '_')
content_file = free_path / f"browsed_{domain}_{int(datetime.now().timestamp())}.html"
with open(content_file, 'w', encoding='utf-8') as f:
f.write(content)
return {
"success": True,
"url": url,
"status_code": response.status_code,
"content_length": len(content),
"content_preview": content_preview,
"saved_to": str(content_file),
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"Failed to browse URL: {str(e)}"}
async def handle_research(topic: str, depth: str = "basic") -> Dict[str, Any]:
"""
Comprehensive research using multiple search types
Usage: /research <topic> [basic|detailed]
"""
if not topic.strip():
return {"error": "Research topic cannot be empty"}
if not DDG_AVAILABLE:
return {"error": "DuckDuckGo search library not installed"}
try:
research_data = {
"topic": topic,
"depth": depth,
"timestamp": datetime.now().isoformat(),
"web_results": [],
"news_results": [],
"instant_answers": [],
"api_version": "duckduckgo-search v8.1.1+"
}
with DDGS() as ddgs:
# 1. Web search
web_results = ddgs.text(keywords=topic, region="us-en", safesearch="moderate")
for i, result in enumerate(web_results):
if i >= 5: # Limit for research
break
research_data["web_results"].append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
# 2. News search
try:
news_results = ddgs.news(keywords=topic, region="us-en", safesearch="moderate")
for i, news in enumerate(news_results):
if i >= 3: # Limit for research
break
research_data["news_results"].append({
"title": news.get("title", ""),
"url": news.get("url", ""),
"source": news.get("source", ""),
"date": news.get("date", "")
})
except:
pass # News might not be available for all topics
# 3. Try instant answers
try:
answers = ddgs.answers(topic)
for answer in answers:
research_data["instant_answers"].append({
"text": answer.get("text", ""),
"url": answer.get("url", ""),
"source": answer.get("source", "")
})
break # Usually just one answer
except:
pass # Instant answers might not be available
# Save comprehensive research
free_path = Path(r"C:\meu_projeto_livre")
research_file = free_path / f"research_{topic.replace(' ', '_')[:20]}_{int(datetime.now().timestamp())}.json"
with open(research_file, 'w', encoding='utf-8') as f:
json.dump(research_data, f, indent=2, ensure_ascii=False)
# Generate summary
total_sources = len(research_data["web_results"]) + len(research_data["news_results"])
has_instant = len(research_data["instant_answers"]) > 0
return {
"success": True,
"topic": topic,
"depth": depth,
"total_sources": total_sources,
"has_instant_answer": has_instant,
"saved_to": str(research_file),
"summary": f"Research complete: {total_sources} sources found for '{topic}'",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {"error": f"Research failed: {str(e)}"}
# Updated command registry
WEB_COMMANDS = {
"/search": handle_search,
"/news": handle_news,
"/images": handle_images,
"/videos": handle_videos,
"/instant": handle_instant_answer,
"/browse": handle_browse,
"/research": handle_research,
}
# Register commands function
def register_web_commands():
"""Register web commands in the main system"""
try:
from tools.registry import register_handler
for command, handler in WEB_COMMANDS.items():
register_handler(command, handler)
print("✅ Real web search commands registered (v8.1.1+):")
for cmd in WEB_COMMANDS.keys():
print(f" {cmd}")
if DDG_AVAILABLE:
print("🌐 DuckDuckGo search engine ready (latest API)!")
else:
print("⚠️ Install duckduckgo-search for full functionality")
except Exception as e:
print(f"⚠️ Error registering web commands: {e}")
# Auto-register on import
register_web_commands() |