Skip to content

Commit 3fb194a

Browse files
fix(research): scrape discovered URLs through Firecrawl
1 parent 28f3eed commit 3fb194a

7 files changed

Lines changed: 546 additions & 37 deletions

‎services/search/__init__.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@
88
update_search_config,
99
)
1010
from .content import fetch_webpage_content
11-
from .providers import firecrawl_search, searxng_search, searxng_search_api, PROVIDER_INFO
12-
from .analytics import get_search_stats, SearchEngineError, NetworkError, ParseError, RateLimitError
11+
from .providers import (
12+
firecrawl_scrape,
13+
firecrawl_search,
14+
searxng_search,
15+
searxng_search_api,
16+
PROVIDER_INFO,
17+
)
18+
from .analytics import (
19+
get_search_stats,
20+
SearchEngineError,
21+
NetworkError,
22+
ParseError,
23+
RateLimitError,
24+
)
1325
from .service import SearchService, SearchResult, SearchResponse
1426

1527
__all__ = [
@@ -23,6 +35,7 @@
2335
"get_search_config",
2436
"get_search_stats",
2537
"invalidate_search_cache",
38+
"firecrawl_scrape",
2639
"firecrawl_search",
2740
"searxng_search",
2841
"searxng_search_api",

‎services/search/providers.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,80 @@ def _safesearch_for(provider: str) -> Optional[str]:
137137

138138
# ── Firecrawl (self-hosted) ──
139139

140+
def firecrawl_scrape(url: str, timeout: int = 60) -> dict:
141+
"""Render one public URL through the configured local Firecrawl appliance.
142+
143+
Search discovery and page extraction are separate Firecrawl operations.
144+
Deep research uses this helper after ``/v2/search`` returns each result's
145+
``url`` field. A normalized failure result lets callers retain their
146+
existing hardened HTTP fetcher as a fallback without ever reaching the
147+
hosted Firecrawl API.
148+
"""
149+
instance = _get_firecrawl_instance()
150+
if not instance:
151+
return {
152+
"url": url,
153+
"title": "",
154+
"content": "",
155+
"og_image": "",
156+
"success": False,
157+
"error": "no local Firecrawl endpoint configured",
158+
}
159+
160+
headers = {"User-Agent": WEB_FETCH_USER_AGENT}
161+
api_key = _get_provider_key("firecrawl")
162+
if api_key:
163+
headers["Authorization"] = f"Bearer {api_key}"
164+
165+
try:
166+
response = httpx.post(
167+
f"{instance}/v2/scrape",
168+
json={
169+
"url": url,
170+
"formats": ["markdown"],
171+
"onlyMainContent": True,
172+
},
173+
headers=headers,
174+
timeout=max(5, int(timeout or 60)),
175+
)
176+
response.raise_for_status()
177+
payload = response.json()
178+
data = payload.get("data", {}) if isinstance(payload, dict) else {}
179+
metadata = data.get("metadata", {}) if isinstance(data, dict) else {}
180+
if not isinstance(metadata, dict):
181+
metadata = {}
182+
content = data.get("markdown", "") if isinstance(data, dict) else ""
183+
if not isinstance(content, str) or not content.strip():
184+
raise ValueError("Firecrawl returned no markdown content")
185+
186+
logger.info("Firecrawl scraped %d characters from: %s", len(content), url)
187+
return {
188+
"url": metadata.get("sourceURL") or metadata.get("url") or url,
189+
"title": metadata.get("title") or "",
190+
"content": content,
191+
"og_image": (
192+
metadata.get("ogImage")
193+
or metadata.get("og:image")
194+
or metadata.get("image")
195+
or ""
196+
),
197+
"success": True,
198+
"error": "",
199+
"provider": "firecrawl",
200+
}
201+
except Exception as exc:
202+
logger.warning("Local Firecrawl scrape failed for %s: %s", url, exc)
203+
return {
204+
"url": url,
205+
"title": "",
206+
"content": "",
207+
"og_image": "",
208+
"success": False,
209+
"error": str(exc),
210+
"provider": "firecrawl",
211+
}
212+
213+
140214
def firecrawl_search(
141215
query: str,
142216
count: Optional[int] = None,

0 commit comments

Comments
 (0)