mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-10-21 14:22:19 +02:00
Preserve date and category constraints across HTML search fallback
This commit is contained in:
@@ -305,23 +305,28 @@ def searxng_search_api(query: str, count: Optional[int] = None, categories: str
|
|||||||
return parsed
|
return parsed
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"SearXNG JSON API search failed: {e}")
|
logger.warning(f"SearXNG JSON API search failed: {e}")
|
||||||
html_results = searxng_search(query, max_results=count)
|
html_results = searxng_search(query, max_results=count, search_params=active_params)
|
||||||
if html_results:
|
if html_results:
|
||||||
logger.info(f"SearXNG HTML fallback returned {len(html_results)} results for: {query}")
|
logger.info(f"SearXNG HTML fallback returned {len(html_results)} results for: {query}")
|
||||||
return html_results
|
return html_results
|
||||||
|
|
||||||
|
|
||||||
def searxng_search(query, max_results=10):
|
def searxng_search(query, max_results=10, *, search_params=None):
|
||||||
"""Search using SearXNG instance - parsing HTML."""
|
"""Search using SearXNG instance - parsing HTML."""
|
||||||
instance = _get_search_instance()
|
instance = _get_search_instance()
|
||||||
api_key = ""
|
api_key = ""
|
||||||
req_headers = {"User-Agent": WEB_FETCH_USER_AGENT}
|
req_headers = {"User-Agent": WEB_FETCH_USER_AGENT}
|
||||||
if api_key:
|
if api_key:
|
||||||
req_headers["Authorization"] = f"Bearer {api_key}"
|
req_headers["Authorization"] = f"Bearer {api_key}"
|
||||||
|
# Transport fallback must not change the user's retrieval constraints.
|
||||||
|
# In particular omit only JSON formatting, not publication time/category.
|
||||||
|
params = {key: value for key, value in (search_params or {}).items()
|
||||||
|
if key in {'categories', 'engines', 'language', 'time_range'}}
|
||||||
|
params.update({'q': query, 'safesearch': _safesearch_for('searxng')})
|
||||||
try:
|
try:
|
||||||
response = httpx.get(
|
response = httpx.get(
|
||||||
f"{instance}/search",
|
f"{instance}/search",
|
||||||
params={"q": query, "safesearch": _safesearch_for("searxng")},
|
params=params,
|
||||||
headers=req_headers,
|
headers=req_headers,
|
||||||
timeout=10,
|
timeout=10,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,6 +11,27 @@ from services.search import core
|
|||||||
from services.search import providers
|
from services.search import providers
|
||||||
|
|
||||||
|
|
||||||
|
def test_html_transport_fallback_preserves_query_constraints(monkeypatch):
|
||||||
|
seen = []
|
||||||
|
class Response:
|
||||||
|
is_success = True
|
||||||
|
text = '<article class="result"><h3><a href="https://example.org/report">Report</a></h3><p class="content">Evidence</p></article>'
|
||||||
|
def get(*args, **kwargs):
|
||||||
|
seen.append(dict(kwargs['params']))
|
||||||
|
if len(seen) == 1:
|
||||||
|
raise ValueError('JSON transport unavailable')
|
||||||
|
return Response()
|
||||||
|
monkeypatch.setattr(providers, '_get_search_instance', lambda: 'http://searx.test')
|
||||||
|
monkeypatch.setattr(providers, '_get_search_settings', lambda: {'search_safesearch': 'strict'})
|
||||||
|
monkeypatch.setattr(providers, '_get_provider_key', lambda name: '')
|
||||||
|
monkeypatch.setattr(providers.httpx, 'get', get)
|
||||||
|
results = providers.searxng_search_api('AI news site:example.org', time_filter='week', engines='test-engine')
|
||||||
|
assert len(results) == 1
|
||||||
|
assert len(seen) == 2
|
||||||
|
assert seen[1] == {k: v for k, v in seen[0].items() if k != 'format'}
|
||||||
|
assert seen[1]['time_range'] == 'week'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('query,category', [
|
@pytest.mark.parametrize('query,category', [
|
||||||
('AI developments this week', 'news'),
|
('AI developments this week', 'news'),
|
||||||
('recent developments in battery manufacturing', 'news'),
|
('recent developments in battery manufacturing', 'news'),
|
||||||
|
|||||||
Reference in New Issue
Block a user