76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
"""
|
|
Example script for using the news search handler.
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
import os
|
|
from datetime import datetime
|
|
|
|
# Add the project root to the Python path
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
|
|
|
from execution.search_executor import SearchExecutor
|
|
from query.query_processor import get_query_processor
|
|
from config.config import get_config
|
|
|
|
|
|
async def main():
|
|
"""Run a sample news search."""
|
|
# Initialize components
|
|
query_processor = get_query_processor()
|
|
search_executor = SearchExecutor()
|
|
|
|
# Get a list of available search engines
|
|
available_engines = search_executor.get_available_search_engines()
|
|
print(f"Available search engines: {', '.join(available_engines)}")
|
|
|
|
# Check if news search is available
|
|
if "news" not in available_engines:
|
|
print("News search is not available. Please check your NewsAPI configuration.")
|
|
return
|
|
|
|
# Prompt for the query
|
|
query = input("Enter your query about recent events: ") or "Trump tariffs latest announcement"
|
|
|
|
print(f"\nProcessing query: {query}")
|
|
|
|
# Process the query
|
|
start_time = datetime.now()
|
|
structured_query = await query_processor.process_query(query)
|
|
|
|
# Generate search queries optimized for each engine
|
|
structured_query = await query_processor.generate_search_queries(
|
|
structured_query, ["news"]
|
|
)
|
|
|
|
# Print the optimized queries
|
|
print("\nOptimized queries for news search:")
|
|
for i, q in enumerate(structured_query.get("search_queries", {}).get("news", [])):
|
|
print(f"{i+1}. {q}")
|
|
|
|
# Execute the search
|
|
results = await search_executor.execute_search_async(
|
|
structured_query,
|
|
search_engines=["news"],
|
|
num_results=10
|
|
)
|
|
|
|
# Print the results
|
|
news_results = results.get("news", [])
|
|
print(f"\nFound {len(news_results)} news results:")
|
|
|
|
for i, result in enumerate(news_results):
|
|
print(f"\n--- Result {i+1} ---")
|
|
print(f"Title: {result.get('title', 'No title')}")
|
|
print(f"Source: {result.get('source', 'Unknown')}")
|
|
print(f"Date: {result.get('published_date', 'Unknown date')}")
|
|
print(f"URL: {result.get('url', 'No URL')}")
|
|
print(f"Snippet: {result.get('snippet', 'No snippet')[0:200]}...")
|
|
|
|
end_time = datetime.now()
|
|
print(f"\nSearch completed in {(end_time - start_time).total_seconds():.2f} seconds")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |