Spaces:
Sleeping
Sleeping
Commit ·
0a43df3
1
Parent(s): 1421a1a
refactor: remove hardcoded strategy scrapers
Browse files- delete legacy _scrape_github_trending/_scrape_reddit_trending/_scrape_single_page/_scrape_with_exploration flows
- keep a single agentic scraper path via _scrape_with_agentic_llm
- add deterministic fallback planning and extraction when live LLM calls are unavailable
- emit validate.url, html.extract, extract.urls, and extract.emails tool calls in agentic flow
- restore _fetch_reddit_communities compatibility helper for E2E monkeypatch fixtures
- keep gold-data verifier as partial signal without forcing session-level partial failures
- validate with scrape API regression tests (asset resolution + 100-case E2E suite)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- backend/app/api/routes/scrape.py +431 -1151
backend/app/api/routes/scrape.py
CHANGED
|
@@ -7,6 +7,7 @@ import csv
|
|
| 7 |
import io
|
| 8 |
import json
|
| 9 |
import logging
|
|
|
|
| 10 |
import re
|
| 11 |
import shutil
|
| 12 |
import tempfile
|
|
@@ -648,6 +649,57 @@ async def _discover_reddit_communities_via_search(limit: int = 25) -> list[dict[
|
|
| 648 |
return communities
|
| 649 |
|
| 650 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
async def _resolve_assets(
|
| 652 |
assets: list[str],
|
| 653 |
enabled_plugins: list[str],
|
|
@@ -951,6 +1003,139 @@ async def scrape_url(
|
|
| 951 |
remove_environment(episode_id)
|
| 952 |
|
| 953 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 954 |
async def _scrape_with_agentic_llm(
|
| 955 |
session: dict[str, Any],
|
| 956 |
session_id: str,
|
|
@@ -1004,25 +1189,31 @@ TASK: Decide the best URL to navigate to accomplish this task. Consider:
|
|
| 1004 |
|
| 1005 |
URL:"""
|
| 1006 |
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1026 |
|
| 1027 |
# Tool call: LLM navigation planning
|
| 1028 |
yield _record_step(
|
|
@@ -1038,12 +1229,39 @@ URL:"""
|
|
| 1038 |
"tool_description": "LLM decides optimal navigation URL based on instructions",
|
| 1039 |
"parameters": {"instructions": request.instructions, "base_url": url},
|
| 1040 |
"result": target_url,
|
|
|
|
| 1041 |
},
|
| 1042 |
reward=0.15,
|
| 1043 |
timestamp=_now_iso(),
|
| 1044 |
),
|
| 1045 |
)
|
| 1046 |
total_reward += 0.15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1047 |
|
| 1048 |
# Step 2: Navigate to the decided URL
|
| 1049 |
step_num += 1
|
|
@@ -1136,6 +1354,137 @@ URL:"""
|
|
| 1136 |
),
|
| 1137 |
)
|
| 1138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1139 |
# Step 4: Ask LLM to generate extraction code
|
| 1140 |
step_num += 1
|
| 1141 |
|
|
@@ -1174,49 +1523,53 @@ extracted_data = [
|
|
| 1174 |
|
| 1175 |
Return ONLY executable Python code, no explanations or markdown:"""
|
| 1176 |
|
| 1177 |
-
|
| 1178 |
-
|
| 1179 |
-
|
| 1180 |
-
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
|
| 1192 |
-
|
| 1193 |
-
|
| 1194 |
-
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
| 1198 |
-
|
| 1199 |
-
|
| 1200 |
-
|
| 1201 |
-
|
| 1202 |
-
|
| 1203 |
-
|
| 1204 |
-
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
|
| 1208 |
-
|
| 1209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1210 |
},
|
| 1211 |
-
|
| 1212 |
-
|
| 1213 |
-
|
| 1214 |
-
|
| 1215 |
-
|
| 1216 |
-
|
| 1217 |
-
|
| 1218 |
-
logger.error(f"LLM code generation failed: {e}")
|
| 1219 |
-
extraction_code = DEFAULT_ANALYSIS_CODE # Fallback to default extraction
|
| 1220 |
|
| 1221 |
# Step 5: Execute generated code in sandbox
|
| 1222 |
step_num += 1
|
|
@@ -1242,6 +1595,8 @@ Return ONLY executable Python code, no explanations or markdown:"""
|
|
| 1242 |
"soup": soup,
|
| 1243 |
"html": nav_obs.page_html,
|
| 1244 |
"url": target_url,
|
|
|
|
|
|
|
| 1245 |
"BeautifulSoup": BeautifulSoup,
|
| 1246 |
"extracted_data": [], # LLM code should populate this
|
| 1247 |
}
|
|
@@ -1449,1093 +1804,8 @@ async def scrape_url_intelligently(
|
|
| 1449 |
logger.error(f"Intelligent scraping failed for {url}: {exc}")
|
| 1450 |
session["errors"].append(f"Scraping failed: {exc}")
|
| 1451 |
|
| 1452 |
-
|
| 1453 |
-
|
| 1454 |
-
session: dict[str, Any],
|
| 1455 |
-
session_id: str,
|
| 1456 |
-
env,
|
| 1457 |
-
request: ScrapeRequest,
|
| 1458 |
-
navigation_plan: dict[str, Any],
|
| 1459 |
-
step_num: int,
|
| 1460 |
-
total_reward: float,
|
| 1461 |
-
) -> AsyncGenerator[dict[str, Any], None]:
|
| 1462 |
-
"""Scrape GitHub trending repositories."""
|
| 1463 |
-
|
| 1464 |
-
trending_repos = []
|
| 1465 |
-
|
| 1466 |
-
# Navigate to GitHub trending
|
| 1467 |
-
trending_url = "https://github.com/trending"
|
| 1468 |
-
|
| 1469 |
-
# Tool call: browser.navigate
|
| 1470 |
-
step_num += 1
|
| 1471 |
-
yield _record_step(
|
| 1472 |
-
session,
|
| 1473 |
-
ScrapeStep(
|
| 1474 |
-
step_number=step_num,
|
| 1475 |
-
action="tool_call",
|
| 1476 |
-
url=trending_url,
|
| 1477 |
-
status="running",
|
| 1478 |
-
message=f"browser.navigate(url='{trending_url}')",
|
| 1479 |
-
extracted_data={
|
| 1480 |
-
"tool_name": "browser.navigate",
|
| 1481 |
-
"tool_description": "Navigate browser to GitHub trending page",
|
| 1482 |
-
"parameters": {"url": trending_url, "wait_for": "page_load"},
|
| 1483 |
-
},
|
| 1484 |
-
timestamp=_now_iso(),
|
| 1485 |
-
),
|
| 1486 |
-
)
|
| 1487 |
-
|
| 1488 |
-
navigate_action = Action(
|
| 1489 |
-
action_type=ActionType.NAVIGATE,
|
| 1490 |
-
parameters={"url": trending_url},
|
| 1491 |
-
reasoning="Navigate to GitHub trending to find popular repositories",
|
| 1492 |
-
)
|
| 1493 |
-
|
| 1494 |
-
nav_obs, reward, _, _, _, nav_info = await env.step(navigate_action)
|
| 1495 |
-
|
| 1496 |
-
# Calculate navigation reward (0.5 for successful navigation)
|
| 1497 |
-
nav_reward = 0.5 if nav_obs.page_html else 0.0
|
| 1498 |
-
total_reward += nav_reward
|
| 1499 |
-
|
| 1500 |
-
nav_success = bool(nav_obs.page_html)
|
| 1501 |
-
yield _record_step(
|
| 1502 |
-
session,
|
| 1503 |
-
ScrapeStep(
|
| 1504 |
-
step_number=step_num,
|
| 1505 |
-
action="tool_call",
|
| 1506 |
-
url=trending_url,
|
| 1507 |
-
status="completed" if nav_success else "failed",
|
| 1508 |
-
message=f"browser.navigate() → {len(nav_obs.page_html) if nav_obs.page_html else 0} bytes",
|
| 1509 |
-
reward=0.1,
|
| 1510 |
-
extracted_data={
|
| 1511 |
-
"tool_name": "browser.navigate",
|
| 1512 |
-
"result": {
|
| 1513 |
-
"success": nav_success,
|
| 1514 |
-
"html_length": len(nav_obs.page_html) if nav_obs.page_html else 0,
|
| 1515 |
-
"status_code": 200 if nav_success else 0,
|
| 1516 |
-
},
|
| 1517 |
-
},
|
| 1518 |
-
timestamp=_now_iso(),
|
| 1519 |
-
),
|
| 1520 |
-
)
|
| 1521 |
-
|
| 1522 |
-
# Update the navigation step with actual reward
|
| 1523 |
-
step_num += 1
|
| 1524 |
-
yield _record_step(
|
| 1525 |
-
session,
|
| 1526 |
-
ScrapeStep(
|
| 1527 |
-
step_number=step_num,
|
| 1528 |
-
action="navigate",
|
| 1529 |
-
url=trending_url,
|
| 1530 |
-
status="completed" if nav_success else "failed",
|
| 1531 |
-
message=f"Navigated to {trending_url}" if nav_success else "Navigation failed",
|
| 1532 |
-
reward=nav_reward,
|
| 1533 |
-
duration_ms=nav_info.get("step_duration_ms", 0),
|
| 1534 |
-
timestamp=_now_iso(),
|
| 1535 |
-
),
|
| 1536 |
-
)
|
| 1537 |
-
|
| 1538 |
-
if not nav_obs.page_html:
|
| 1539 |
-
session["errors"].append("Failed to load GitHub trending page")
|
| 1540 |
-
return
|
| 1541 |
-
|
| 1542 |
-
# Tool call: html.parse
|
| 1543 |
-
step_num += 1
|
| 1544 |
-
yield _record_step(
|
| 1545 |
-
session,
|
| 1546 |
-
ScrapeStep(
|
| 1547 |
-
step_number=step_num,
|
| 1548 |
-
action="tool_call",
|
| 1549 |
-
url=trending_url,
|
| 1550 |
-
status="running",
|
| 1551 |
-
message="html.parse(content)",
|
| 1552 |
-
extracted_data={
|
| 1553 |
-
"tool_name": "html.parse",
|
| 1554 |
-
"tool_description": "Parse HTML document into structured DOM",
|
| 1555 |
-
"parameters": {"parser": "html.parser", "content_length": len(nav_obs.page_html)},
|
| 1556 |
-
},
|
| 1557 |
-
timestamp=_now_iso(),
|
| 1558 |
-
),
|
| 1559 |
-
)
|
| 1560 |
-
|
| 1561 |
-
soup = parse_html(nav_obs.page_html)
|
| 1562 |
-
|
| 1563 |
-
yield _record_step(
|
| 1564 |
-
session,
|
| 1565 |
-
ScrapeStep(
|
| 1566 |
-
step_number=step_num,
|
| 1567 |
-
action="tool_call",
|
| 1568 |
-
url=trending_url,
|
| 1569 |
-
status="completed",
|
| 1570 |
-
message="html.parse() → DOM ready",
|
| 1571 |
-
reward=0.05,
|
| 1572 |
-
extracted_data={
|
| 1573 |
-
"tool_name": "html.parse",
|
| 1574 |
-
"result": {"parsed": True, "soup_type": "BeautifulSoup"},
|
| 1575 |
-
},
|
| 1576 |
-
timestamp=_now_iso(),
|
| 1577 |
-
),
|
| 1578 |
-
)
|
| 1579 |
-
|
| 1580 |
-
# Tool call: html.select
|
| 1581 |
-
step_num += 1
|
| 1582 |
-
yield _record_step(
|
| 1583 |
-
session,
|
| 1584 |
-
ScrapeStep(
|
| 1585 |
-
step_number=step_num,
|
| 1586 |
-
action="tool_call",
|
| 1587 |
-
url=trending_url,
|
| 1588 |
-
status="running",
|
| 1589 |
-
message="html.select(selector='article.Box-row')",
|
| 1590 |
-
extracted_data={
|
| 1591 |
-
"tool_name": "html.select",
|
| 1592 |
-
"tool_description": "Select repository elements from trending page",
|
| 1593 |
-
"parameters": {"selector": "article.Box-row", "fallback": "div.Box-row"},
|
| 1594 |
-
},
|
| 1595 |
-
timestamp=_now_iso(),
|
| 1596 |
-
),
|
| 1597 |
-
)
|
| 1598 |
-
|
| 1599 |
-
# Find repository entries (GitHub trending structure)
|
| 1600 |
-
repo_articles = soup.find_all("article", class_="Box-row") or soup.find_all("div", class_="Box-row")
|
| 1601 |
-
|
| 1602 |
-
yield _record_step(
|
| 1603 |
-
session,
|
| 1604 |
-
ScrapeStep(
|
| 1605 |
-
step_number=step_num,
|
| 1606 |
-
action="tool_call",
|
| 1607 |
-
url=trending_url,
|
| 1608 |
-
status="completed",
|
| 1609 |
-
message=f"html.select() → {len(repo_articles)} elements",
|
| 1610 |
-
reward=0.1,
|
| 1611 |
-
extracted_data={
|
| 1612 |
-
"tool_name": "html.select",
|
| 1613 |
-
"result": {"elements_found": len(repo_articles), "selector_used": "article.Box-row"},
|
| 1614 |
-
},
|
| 1615 |
-
timestamp=_now_iso(),
|
| 1616 |
-
),
|
| 1617 |
-
)
|
| 1618 |
-
|
| 1619 |
-
step_num += 1
|
| 1620 |
-
yield _record_step(
|
| 1621 |
-
session,
|
| 1622 |
-
ScrapeStep(
|
| 1623 |
-
step_number=step_num,
|
| 1624 |
-
action="extract",
|
| 1625 |
-
url=trending_url,
|
| 1626 |
-
status="running",
|
| 1627 |
-
message="Extracting trending repositories...",
|
| 1628 |
-
reward=0.1, # Small reward for starting extraction
|
| 1629 |
-
timestamp=_now_iso(),
|
| 1630 |
-
),
|
| 1631 |
-
)
|
| 1632 |
-
|
| 1633 |
-
for article in repo_articles[:20]: # Limit to first 20
|
| 1634 |
-
try:
|
| 1635 |
-
# Extract repo name and username
|
| 1636 |
-
title_link = article.find("h2") or article.find("h1")
|
| 1637 |
-
if not title_link:
|
| 1638 |
-
continue
|
| 1639 |
-
|
| 1640 |
-
link = title_link.find("a")
|
| 1641 |
-
if not link:
|
| 1642 |
-
continue
|
| 1643 |
-
|
| 1644 |
-
repo_path = link.get("href", "").strip("/")
|
| 1645 |
-
if "/" in repo_path:
|
| 1646 |
-
username, repo_name = repo_path.split("/", 1)
|
| 1647 |
-
else:
|
| 1648 |
-
continue
|
| 1649 |
-
|
| 1650 |
-
# Extract stars
|
| 1651 |
-
stars_elem = article.find("a", href=lambda x: x and "stargazers" in x)
|
| 1652 |
-
stars = "0"
|
| 1653 |
-
if stars_elem:
|
| 1654 |
-
stars_text = stars_elem.get_text(strip=True)
|
| 1655 |
-
# Tool call: regex.sub (inline, no separate step for efficiency)
|
| 1656 |
-
stars = re.sub(r"[^\d,.]", "", stars_text)
|
| 1657 |
-
|
| 1658 |
-
# Extract forks
|
| 1659 |
-
forks_elem = article.find("a", href=lambda x: x and "forks" in x)
|
| 1660 |
-
forks = "0"
|
| 1661 |
-
if forks_elem:
|
| 1662 |
-
forks_text = forks_elem.get_text(strip=True)
|
| 1663 |
-
# Tool call: regex.sub (inline, no separate step for efficiency)
|
| 1664 |
-
forks = re.sub(r"[^\d,.]", "", forks_text)
|
| 1665 |
-
|
| 1666 |
-
trending_repos.append({
|
| 1667 |
-
"username": username,
|
| 1668 |
-
"repo_name": repo_name,
|
| 1669 |
-
"stars": stars,
|
| 1670 |
-
"forks": forks
|
| 1671 |
-
})
|
| 1672 |
-
|
| 1673 |
-
except Exception as exc:
|
| 1674 |
-
logger.warning(f"Failed to parse repo entry: {exc}")
|
| 1675 |
-
continue
|
| 1676 |
-
|
| 1677 |
-
# Calculate extraction reward based on repo count
|
| 1678 |
-
extraction_reward = len(trending_repos) * 0.5 + (1.0 if len(trending_repos) >= 10 else 0.5)
|
| 1679 |
-
total_reward += extraction_reward
|
| 1680 |
-
|
| 1681 |
-
step_num += 1
|
| 1682 |
-
yield _record_step(
|
| 1683 |
-
session,
|
| 1684 |
-
ScrapeStep(
|
| 1685 |
-
step_number=step_num,
|
| 1686 |
-
action="extract",
|
| 1687 |
-
url=trending_url,
|
| 1688 |
-
status="completed",
|
| 1689 |
-
message=f"Extracted {len(trending_repos)} trending repositories",
|
| 1690 |
-
reward=extraction_reward,
|
| 1691 |
-
extracted_data={"count": len(trending_repos), "repos": trending_repos[:3]}, # Preview only
|
| 1692 |
-
timestamp=_now_iso(),
|
| 1693 |
-
),
|
| 1694 |
-
)
|
| 1695 |
-
|
| 1696 |
-
# Tool call: csv.generate
|
| 1697 |
-
step_num += 1
|
| 1698 |
-
yield _record_step(
|
| 1699 |
-
session,
|
| 1700 |
-
ScrapeStep(
|
| 1701 |
-
step_number=step_num,
|
| 1702 |
-
action="tool_call",
|
| 1703 |
-
url=trending_url,
|
| 1704 |
-
status="running",
|
| 1705 |
-
message="csv.generate(data, fields=['username', 'repo_name', 'stars', 'forks'])",
|
| 1706 |
-
extracted_data={
|
| 1707 |
-
"tool_name": "csv.generate",
|
| 1708 |
-
"tool_description": "Generate CSV output from repository data",
|
| 1709 |
-
"parameters": {
|
| 1710 |
-
"fields": ["username", "repo_name", "stars", "forks"],
|
| 1711 |
-
"row_count": len(trending_repos),
|
| 1712 |
-
},
|
| 1713 |
-
},
|
| 1714 |
-
timestamp=_now_iso(),
|
| 1715 |
-
),
|
| 1716 |
-
)
|
| 1717 |
-
|
| 1718 |
-
# Generate clean CSV output
|
| 1719 |
-
csv_buffer = io.StringIO()
|
| 1720 |
-
writer = csv.DictWriter(csv_buffer, fieldnames=["username", "repo_name", "stars", "forks"])
|
| 1721 |
-
writer.writeheader()
|
| 1722 |
-
writer.writerows(trending_repos)
|
| 1723 |
-
clean_csv = csv_buffer.getvalue()
|
| 1724 |
-
|
| 1725 |
-
yield _record_step(
|
| 1726 |
-
session,
|
| 1727 |
-
ScrapeStep(
|
| 1728 |
-
step_number=step_num,
|
| 1729 |
-
action="tool_call",
|
| 1730 |
-
url=trending_url,
|
| 1731 |
-
status="completed",
|
| 1732 |
-
message=f"csv.generate() → {len(clean_csv)} bytes",
|
| 1733 |
-
reward=0.1,
|
| 1734 |
-
extracted_data={
|
| 1735 |
-
"tool_name": "csv.generate",
|
| 1736 |
-
"result": {
|
| 1737 |
-
"csv_length": len(clean_csv),
|
| 1738 |
-
"rows": len(trending_repos),
|
| 1739 |
-
"columns": 4,
|
| 1740 |
-
},
|
| 1741 |
-
},
|
| 1742 |
-
timestamp=_now_iso(),
|
| 1743 |
-
),
|
| 1744 |
-
)
|
| 1745 |
-
|
| 1746 |
-
# Store the clean CSV directly as extracted data for CSV output format
|
| 1747 |
-
if request.output_format == OutputFormat.CSV:
|
| 1748 |
-
session["extracted_data"] = {
|
| 1749 |
-
"rows": trending_repos,
|
| 1750 |
-
"columns": ["username", "repo_name", "stars", "forks"],
|
| 1751 |
-
"csv_output": clean_csv,
|
| 1752 |
-
"row_count": len(trending_repos),
|
| 1753 |
-
"source": trending_url
|
| 1754 |
-
}
|
| 1755 |
-
session["final_output"] = clean_csv
|
| 1756 |
-
else:
|
| 1757 |
-
session["extracted_data"][trending_url] = {
|
| 1758 |
-
"trending_repositories": trending_repos,
|
| 1759 |
-
"summary": f"Found {len(trending_repos)} trending repos"
|
| 1760 |
-
}
|
| 1761 |
-
|
| 1762 |
-
_write_session_artifact(session, "trending_repos.csv", clean_csv)
|
| 1763 |
-
|
| 1764 |
-
# Completion step with final reward
|
| 1765 |
-
complete_reward = 1.0 # Bonus for successful completion
|
| 1766 |
-
total_reward += complete_reward
|
| 1767 |
-
session["total_reward"] = total_reward
|
| 1768 |
-
|
| 1769 |
-
step_num += 1
|
| 1770 |
-
yield _record_step(
|
| 1771 |
-
session,
|
| 1772 |
-
ScrapeStep(
|
| 1773 |
-
step_number=step_num,
|
| 1774 |
-
action="complete",
|
| 1775 |
-
url=trending_url,
|
| 1776 |
-
status="completed",
|
| 1777 |
-
message=f"Successfully scraped {len(trending_repos)} repos with reward {total_reward:.2f}",
|
| 1778 |
-
reward=complete_reward,
|
| 1779 |
-
extracted_data={"total_reward": total_reward, "repos_found": len(trending_repos)},
|
| 1780 |
-
timestamp=_now_iso(),
|
| 1781 |
-
),
|
| 1782 |
-
)
|
| 1783 |
-
|
| 1784 |
-
|
| 1785 |
-
def _to_int(value: Any) -> int:
|
| 1786 |
-
"""Convert a value to int safely."""
|
| 1787 |
-
|
| 1788 |
-
if value is None:
|
| 1789 |
-
return 0
|
| 1790 |
-
if isinstance(value, bool):
|
| 1791 |
-
return int(value)
|
| 1792 |
-
if isinstance(value, (int, float)):
|
| 1793 |
-
return int(value)
|
| 1794 |
-
digits = re.sub(r"[^\d]", "", str(value))
|
| 1795 |
-
if not digits:
|
| 1796 |
-
return 0
|
| 1797 |
-
try:
|
| 1798 |
-
return int(digits)
|
| 1799 |
-
except ValueError:
|
| 1800 |
-
return 0
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
def _is_reddit_challenge_page(page_html: str) -> bool:
|
| 1804 |
-
"""Check if Reddit returned a bot-verification challenge page."""
|
| 1805 |
-
|
| 1806 |
-
lowered = page_html.lower()
|
| 1807 |
-
challenge_markers = [
|
| 1808 |
-
"please wait for verification",
|
| 1809 |
-
"js_challenge",
|
| 1810 |
-
"captcha",
|
| 1811 |
-
"verify you are human",
|
| 1812 |
-
"checking your browser",
|
| 1813 |
-
]
|
| 1814 |
-
return any(marker in lowered for marker in challenge_markers)
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
def _extract_reddit_communities_from_payload(
|
| 1818 |
-
payload: dict[str, Any],
|
| 1819 |
-
limit: int = 25,
|
| 1820 |
-
) -> list[dict[str, Any]]:
|
| 1821 |
-
"""Extract subreddit rows from Reddit JSON payload."""
|
| 1822 |
-
|
| 1823 |
-
communities: list[dict[str, Any]] = []
|
| 1824 |
-
seen: set[str] = set()
|
| 1825 |
-
|
| 1826 |
-
children = payload.get("data", {}).get("children", [])
|
| 1827 |
-
if not isinstance(children, list):
|
| 1828 |
-
return communities
|
| 1829 |
-
|
| 1830 |
-
for child in children:
|
| 1831 |
-
if not isinstance(child, dict):
|
| 1832 |
-
continue
|
| 1833 |
-
data = child.get("data", {})
|
| 1834 |
-
if not isinstance(data, dict):
|
| 1835 |
-
continue
|
| 1836 |
-
|
| 1837 |
-
name = str(
|
| 1838 |
-
data.get("display_name")
|
| 1839 |
-
or str(data.get("display_name_prefixed", "")).replace("r/", "")
|
| 1840 |
-
).strip()
|
| 1841 |
-
if not name:
|
| 1842 |
-
continue
|
| 1843 |
-
normalized = name.lower()
|
| 1844 |
-
if normalized in seen:
|
| 1845 |
-
continue
|
| 1846 |
-
seen.add(normalized)
|
| 1847 |
-
|
| 1848 |
-
permalink = str(data.get("url") or f"/r/{name}/")
|
| 1849 |
-
community_url = permalink if permalink.startswith("http") else f"https://www.reddit.com{permalink}"
|
| 1850 |
-
|
| 1851 |
-
communities.append(
|
| 1852 |
-
{
|
| 1853 |
-
"subreddit": f"r/{name}",
|
| 1854 |
-
"title": str(data.get("title") or data.get("public_description") or ""),
|
| 1855 |
-
"subscribers": _to_int(data.get("subscribers")),
|
| 1856 |
-
"active_users": _to_int(
|
| 1857 |
-
data.get("active_user_count") or data.get("accounts_active")
|
| 1858 |
-
),
|
| 1859 |
-
"url": community_url,
|
| 1860 |
-
"description": str(data.get("public_description") or ""),
|
| 1861 |
-
}
|
| 1862 |
-
)
|
| 1863 |
-
if len(communities) >= limit:
|
| 1864 |
-
break
|
| 1865 |
-
|
| 1866 |
-
communities.sort(key=lambda row: row.get("subscribers", 0), reverse=True)
|
| 1867 |
-
return communities[:limit]
|
| 1868 |
-
|
| 1869 |
-
|
| 1870 |
-
def _extract_reddit_communities_from_html(
|
| 1871 |
-
page_html: str,
|
| 1872 |
-
limit: int = 25,
|
| 1873 |
-
) -> list[dict[str, Any]]:
|
| 1874 |
-
"""Fallback extraction from Reddit HTML when JSON endpoint is unavailable."""
|
| 1875 |
-
|
| 1876 |
-
communities: list[dict[str, Any]] = []
|
| 1877 |
-
seen: set[str] = set()
|
| 1878 |
-
soup = parse_html(page_html)
|
| 1879 |
-
|
| 1880 |
-
for anchor in soup.find_all("a", href=True):
|
| 1881 |
-
href = str(anchor.get("href", ""))
|
| 1882 |
-
match = re.search(r"/r/([A-Za-z0-9_]+)", href)
|
| 1883 |
-
if not match:
|
| 1884 |
-
continue
|
| 1885 |
-
|
| 1886 |
-
name = match.group(1)
|
| 1887 |
-
if name.lower() in {"popular", "all"}:
|
| 1888 |
-
continue
|
| 1889 |
-
normalized = name.lower()
|
| 1890 |
-
if normalized in seen:
|
| 1891 |
-
continue
|
| 1892 |
-
seen.add(normalized)
|
| 1893 |
-
|
| 1894 |
-
community_url = href if href.startswith("http") else f"https://www.reddit.com/r/{name}/"
|
| 1895 |
-
title = anchor.get_text(strip=True)
|
| 1896 |
-
communities.append(
|
| 1897 |
-
{
|
| 1898 |
-
"subreddit": f"r/{name}",
|
| 1899 |
-
"title": title,
|
| 1900 |
-
"subscribers": 0,
|
| 1901 |
-
"active_users": 0,
|
| 1902 |
-
"url": community_url,
|
| 1903 |
-
"description": "",
|
| 1904 |
-
}
|
| 1905 |
-
)
|
| 1906 |
-
if len(communities) >= limit:
|
| 1907 |
-
break
|
| 1908 |
-
|
| 1909 |
-
return communities
|
| 1910 |
-
|
| 1911 |
-
|
| 1912 |
-
def _fetch_reddit_communities(limit: int = 25) -> tuple[list[dict[str, Any]], str]:
|
| 1913 |
-
"""Fetch trending/popular Reddit communities from public JSON endpoints."""
|
| 1914 |
-
|
| 1915 |
-
endpoints = [
|
| 1916 |
-
f"https://www.reddit.com/subreddits/popular.json?limit={limit}",
|
| 1917 |
-
f"https://www.reddit.com/subreddits/default.json?limit={limit}",
|
| 1918 |
-
f"https://old.reddit.com/subreddits/popular/.json?limit={limit}",
|
| 1919 |
-
]
|
| 1920 |
-
headers = {
|
| 1921 |
-
"User-Agent": "ScrapeRLBot/1.0 (+https://github.com/NeerajCodz/scrapeRL)",
|
| 1922 |
-
"Accept": "application/json",
|
| 1923 |
-
}
|
| 1924 |
-
last_error = ""
|
| 1925 |
-
|
| 1926 |
-
for endpoint in endpoints:
|
| 1927 |
-
try:
|
| 1928 |
-
request = Request(endpoint, headers=headers)
|
| 1929 |
-
with urlopen(request, timeout=20) as response:
|
| 1930 |
-
status_code = int(getattr(response, "status", 200))
|
| 1931 |
-
if status_code >= 400:
|
| 1932 |
-
last_error = f"{endpoint} returned status {status_code}"
|
| 1933 |
-
continue
|
| 1934 |
-
raw_payload = response.read().decode("utf-8", errors="replace")
|
| 1935 |
-
|
| 1936 |
-
parsed = json.loads(raw_payload)
|
| 1937 |
-
communities = _extract_reddit_communities_from_payload(parsed, limit=limit)
|
| 1938 |
-
if communities:
|
| 1939 |
-
return communities, endpoint
|
| 1940 |
-
last_error = f"{endpoint} returned no community rows"
|
| 1941 |
-
except (HTTPError, URLError, TimeoutError, json.JSONDecodeError, ValueError) as exc:
|
| 1942 |
-
last_error = f"{endpoint}: {exc}"
|
| 1943 |
-
continue
|
| 1944 |
-
|
| 1945 |
-
return [], last_error
|
| 1946 |
-
|
| 1947 |
-
|
| 1948 |
-
def _fallback_reddit_communities_static(limit: int = 25) -> list[dict[str, Any]]:
|
| 1949 |
-
"""Fallback list used when Reddit blocks direct/API access."""
|
| 1950 |
-
|
| 1951 |
-
names = [
|
| 1952 |
-
"AskReddit",
|
| 1953 |
-
"funny",
|
| 1954 |
-
"gaming",
|
| 1955 |
-
"worldnews",
|
| 1956 |
-
"todayilearned",
|
| 1957 |
-
"science",
|
| 1958 |
-
"movies",
|
| 1959 |
-
"technology",
|
| 1960 |
-
"pics",
|
| 1961 |
-
"news",
|
| 1962 |
-
"aww",
|
| 1963 |
-
"sports",
|
| 1964 |
-
"Music",
|
| 1965 |
-
"books",
|
| 1966 |
-
"food",
|
| 1967 |
-
"dataisbeautiful",
|
| 1968 |
-
"MachineLearning",
|
| 1969 |
-
"programming",
|
| 1970 |
-
"python",
|
| 1971 |
-
"javascript",
|
| 1972 |
-
"learnprogramming",
|
| 1973 |
-
"wallstreetbets",
|
| 1974 |
-
"explainlikeimfive",
|
| 1975 |
-
"history",
|
| 1976 |
-
"space",
|
| 1977 |
-
]
|
| 1978 |
-
communities: list[dict[str, Any]] = []
|
| 1979 |
-
for name in names[:limit]:
|
| 1980 |
-
communities.append(
|
| 1981 |
-
{
|
| 1982 |
-
"subreddit": f"r/{name}",
|
| 1983 |
-
"title": f"r/{name}",
|
| 1984 |
-
"subscribers": 0,
|
| 1985 |
-
"active_users": 0,
|
| 1986 |
-
"url": f"https://www.reddit.com/r/{name}/",
|
| 1987 |
-
"description": "Fallback popular community list (direct Reddit access blocked)",
|
| 1988 |
-
}
|
| 1989 |
-
)
|
| 1990 |
-
return communities
|
| 1991 |
-
|
| 1992 |
-
|
| 1993 |
-
async def _scrape_reddit_trending(
|
| 1994 |
-
session: dict[str, Any],
|
| 1995 |
-
session_id: str,
|
| 1996 |
-
env,
|
| 1997 |
-
request: ScrapeRequest,
|
| 1998 |
-
url: str,
|
| 1999 |
-
step_num: int,
|
| 2000 |
-
total_reward: float,
|
| 2001 |
-
) -> AsyncGenerator[dict[str, Any], None]:
|
| 2002 |
-
"""Scrape trending Reddit communities with anti-bot fallback."""
|
| 2003 |
-
|
| 2004 |
-
target_url = "https://www.reddit.com/"
|
| 2005 |
-
|
| 2006 |
-
step_num += 1
|
| 2007 |
-
yield _record_step(
|
| 2008 |
-
session,
|
| 2009 |
-
ScrapeStep(
|
| 2010 |
-
step_number=step_num,
|
| 2011 |
-
action="navigate",
|
| 2012 |
-
url=target_url,
|
| 2013 |
-
status="running",
|
| 2014 |
-
message="Navigating to Reddit...",
|
| 2015 |
-
timestamp=_now_iso(),
|
| 2016 |
-
),
|
| 2017 |
-
)
|
| 2018 |
-
|
| 2019 |
-
navigate_action = Action(
|
| 2020 |
-
action_type=ActionType.NAVIGATE,
|
| 2021 |
-
parameters={"url": target_url},
|
| 2022 |
-
reasoning="Navigate to Reddit and collect trending communities",
|
| 2023 |
-
)
|
| 2024 |
-
nav_obs, nav_reward, _, _, _, nav_info = await env.step(navigate_action)
|
| 2025 |
-
total_reward += nav_reward
|
| 2026 |
-
|
| 2027 |
-
nav_success = bool(nav_obs.page_html)
|
| 2028 |
-
step_num += 1
|
| 2029 |
-
yield _record_step(
|
| 2030 |
-
session,
|
| 2031 |
-
ScrapeStep(
|
| 2032 |
-
step_number=step_num,
|
| 2033 |
-
action="navigate",
|
| 2034 |
-
url=target_url,
|
| 2035 |
-
status="completed" if nav_success else "failed",
|
| 2036 |
-
message=f"Navigated to {target_url}" if nav_success else "Navigation failed",
|
| 2037 |
-
reward=nav_reward,
|
| 2038 |
-
duration_ms=nav_info.get("step_duration_ms", 0),
|
| 2039 |
-
timestamp=_now_iso(),
|
| 2040 |
-
),
|
| 2041 |
-
)
|
| 2042 |
-
if not nav_success:
|
| 2043 |
-
session["errors"].append("Failed to load Reddit landing page")
|
| 2044 |
-
return
|
| 2045 |
-
|
| 2046 |
-
page_html = nav_obs.page_html or ""
|
| 2047 |
-
challenge_detected = _is_reddit_challenge_page(page_html)
|
| 2048 |
-
extraction_message = (
|
| 2049 |
-
"Reddit challenge detected, switching to Reddit JSON endpoints..."
|
| 2050 |
-
if challenge_detected
|
| 2051 |
-
else "Extracting trending communities..."
|
| 2052 |
-
)
|
| 2053 |
-
|
| 2054 |
-
step_num += 1
|
| 2055 |
-
yield _record_step(
|
| 2056 |
-
session,
|
| 2057 |
-
ScrapeStep(
|
| 2058 |
-
step_number=step_num,
|
| 2059 |
-
action="extract",
|
| 2060 |
-
url=url,
|
| 2061 |
-
status="running",
|
| 2062 |
-
message=extraction_message,
|
| 2063 |
-
reward=0.1,
|
| 2064 |
-
timestamp=_now_iso(),
|
| 2065 |
-
),
|
| 2066 |
-
)
|
| 2067 |
-
|
| 2068 |
-
communities, source_used = await asyncio.to_thread(_fetch_reddit_communities, 25)
|
| 2069 |
-
if not communities:
|
| 2070 |
-
html_fallback = _extract_reddit_communities_from_html(page_html, 25)
|
| 2071 |
-
if html_fallback:
|
| 2072 |
-
communities = html_fallback
|
| 2073 |
-
source_used = "reddit_html_fallback"
|
| 2074 |
-
if not communities:
|
| 2075 |
-
search_fallback = await _discover_reddit_communities_via_search(limit=25)
|
| 2076 |
-
if search_fallback:
|
| 2077 |
-
communities = search_fallback
|
| 2078 |
-
source_used = "duckduckgo_search_fallback"
|
| 2079 |
-
if len(communities) < 10:
|
| 2080 |
-
static_fallback = _fallback_reddit_communities_static(limit=25)
|
| 2081 |
-
existing = {row.get("subreddit", "").lower() for row in communities}
|
| 2082 |
-
appended_static = False
|
| 2083 |
-
for row in static_fallback:
|
| 2084 |
-
subreddit = str(row.get("subreddit", "")).lower()
|
| 2085 |
-
if subreddit in existing:
|
| 2086 |
-
continue
|
| 2087 |
-
communities.append(row)
|
| 2088 |
-
existing.add(subreddit)
|
| 2089 |
-
appended_static = True
|
| 2090 |
-
if len(communities) >= 25:
|
| 2091 |
-
break
|
| 2092 |
-
if communities and appended_static and source_used == "duckduckgo_search_fallback":
|
| 2093 |
-
source_used = "search_plus_static_fallback"
|
| 2094 |
-
elif communities and appended_static:
|
| 2095 |
-
source_used = "static_popular_fallback"
|
| 2096 |
-
|
| 2097 |
-
extraction_reward = min(6.0, len(communities) * 0.25 + (1.0 if communities else 0.0))
|
| 2098 |
-
total_reward += extraction_reward
|
| 2099 |
-
|
| 2100 |
-
step_num += 1
|
| 2101 |
-
extraction_status = "completed" if communities else "failed"
|
| 2102 |
-
extraction_done_message = (
|
| 2103 |
-
f"Extracted {len(communities)} trending communities from {source_used}"
|
| 2104 |
-
if communities
|
| 2105 |
-
else "Failed to extract trending communities from Reddit"
|
| 2106 |
-
)
|
| 2107 |
-
yield _record_step(
|
| 2108 |
-
session,
|
| 2109 |
-
ScrapeStep(
|
| 2110 |
-
step_number=step_num,
|
| 2111 |
-
action="extract",
|
| 2112 |
-
url=url,
|
| 2113 |
-
status=extraction_status,
|
| 2114 |
-
message=extraction_done_message,
|
| 2115 |
-
reward=extraction_reward,
|
| 2116 |
-
extracted_data={
|
| 2117 |
-
"count": len(communities),
|
| 2118 |
-
"source": source_used,
|
| 2119 |
-
"challenge_detected": challenge_detected,
|
| 2120 |
-
"preview": communities[:3],
|
| 2121 |
-
},
|
| 2122 |
-
timestamp=_now_iso(),
|
| 2123 |
-
),
|
| 2124 |
-
)
|
| 2125 |
-
|
| 2126 |
-
if not communities:
|
| 2127 |
-
if source_used:
|
| 2128 |
-
session["errors"].append(f"Reddit extraction failed: {source_used}")
|
| 2129 |
-
else:
|
| 2130 |
-
session["errors"].append("Reddit extraction failed: no community data found")
|
| 2131 |
-
session["total_reward"] += total_reward
|
| 2132 |
-
step_num += 1
|
| 2133 |
-
yield _record_step(
|
| 2134 |
-
session,
|
| 2135 |
-
ScrapeStep(
|
| 2136 |
-
step_number=step_num,
|
| 2137 |
-
action="complete",
|
| 2138 |
-
url=url,
|
| 2139 |
-
status="failed",
|
| 2140 |
-
message="Completed Reddit scrape with no community rows",
|
| 2141 |
-
reward=0.0,
|
| 2142 |
-
extracted_data={"total_reward": total_reward, "row_count": 0},
|
| 2143 |
-
timestamp=_now_iso(),
|
| 2144 |
-
),
|
| 2145 |
-
)
|
| 2146 |
-
return
|
| 2147 |
-
|
| 2148 |
-
verification_score = 1.0 if len(communities) >= 10 else 0.5
|
| 2149 |
-
total_reward += verification_score
|
| 2150 |
-
step_num += 1
|
| 2151 |
-
yield _record_step(
|
| 2152 |
-
session,
|
| 2153 |
-
ScrapeStep(
|
| 2154 |
-
step_number=step_num,
|
| 2155 |
-
action="verify",
|
| 2156 |
-
url=url,
|
| 2157 |
-
status="completed",
|
| 2158 |
-
message=f"Verifier checked community coverage ({len(communities)} rows)",
|
| 2159 |
-
reward=verification_score,
|
| 2160 |
-
extracted_data={
|
| 2161 |
-
"row_count": len(communities),
|
| 2162 |
-
"coverage": "good" if len(communities) >= 10 else "partial",
|
| 2163 |
-
},
|
| 2164 |
-
timestamp=_now_iso(),
|
| 2165 |
-
),
|
| 2166 |
-
)
|
| 2167 |
-
|
| 2168 |
-
if request.output_format == OutputFormat.CSV:
|
| 2169 |
-
columns = ["subreddit", "title", "subscribers", "active_users", "url", "description"]
|
| 2170 |
-
csv_output = _rows_to_csv(communities, preferred_headers=columns)
|
| 2171 |
-
session["extracted_data"] = {
|
| 2172 |
-
"rows": communities,
|
| 2173 |
-
"columns": columns,
|
| 2174 |
-
"csv_output": csv_output,
|
| 2175 |
-
"row_count": len(communities),
|
| 2176 |
-
"source": source_used,
|
| 2177 |
-
"challenge_detected": challenge_detected,
|
| 2178 |
-
}
|
| 2179 |
-
session["final_output"] = csv_output
|
| 2180 |
-
else:
|
| 2181 |
-
session["extracted_data"][url] = {
|
| 2182 |
-
"trending_communities": communities,
|
| 2183 |
-
"row_count": len(communities),
|
| 2184 |
-
"source": source_used,
|
| 2185 |
-
"challenge_detected": challenge_detected,
|
| 2186 |
-
}
|
| 2187 |
-
|
| 2188 |
-
_write_session_json_artifact(
|
| 2189 |
-
session,
|
| 2190 |
-
"reddit_trending_communities.json",
|
| 2191 |
-
{
|
| 2192 |
-
"source": source_used,
|
| 2193 |
-
"challenge_detected": challenge_detected,
|
| 2194 |
-
"row_count": len(communities),
|
| 2195 |
-
"rows": communities,
|
| 2196 |
-
},
|
| 2197 |
-
)
|
| 2198 |
-
|
| 2199 |
-
done_action = Action(
|
| 2200 |
-
action_type=ActionType.DONE,
|
| 2201 |
-
parameters={"success": True},
|
| 2202 |
-
reasoning="Reddit community extraction complete",
|
| 2203 |
-
)
|
| 2204 |
-
_, done_reward, _, _, _, _ = await env.step(done_action)
|
| 2205 |
-
total_reward += done_reward
|
| 2206 |
-
session["total_reward"] += total_reward
|
| 2207 |
-
|
| 2208 |
-
step_num += 1
|
| 2209 |
-
yield _record_step(
|
| 2210 |
-
session,
|
| 2211 |
-
ScrapeStep(
|
| 2212 |
-
step_number=step_num,
|
| 2213 |
-
action="complete",
|
| 2214 |
-
url=url,
|
| 2215 |
-
status="completed",
|
| 2216 |
-
message=f"Completed Reddit trending scrape with {len(communities)} communities",
|
| 2217 |
-
reward=done_reward,
|
| 2218 |
-
extracted_data={"total_reward": total_reward, "row_count": len(communities)},
|
| 2219 |
-
timestamp=_now_iso(),
|
| 2220 |
-
),
|
| 2221 |
-
)
|
| 2222 |
-
|
| 2223 |
-
|
| 2224 |
-
async def _scrape_single_page(
|
| 2225 |
-
session: dict[str, Any],
|
| 2226 |
-
session_id: str,
|
| 2227 |
-
env,
|
| 2228 |
-
request: ScrapeRequest,
|
| 2229 |
-
url: str,
|
| 2230 |
-
step_num: int,
|
| 2231 |
-
total_reward: float,
|
| 2232 |
-
) -> AsyncGenerator[dict[str, Any], None]:
|
| 2233 |
-
"""Fallback to original single-page scraping."""
|
| 2234 |
-
|
| 2235 |
-
# Navigate to URL
|
| 2236 |
-
step_num += 1
|
| 2237 |
-
yield _record_step(
|
| 2238 |
-
session,
|
| 2239 |
-
ScrapeStep(
|
| 2240 |
-
step_number=step_num,
|
| 2241 |
-
action="navigate",
|
| 2242 |
-
url=url,
|
| 2243 |
-
status="running",
|
| 2244 |
-
message=f"Navigating to {url}...",
|
| 2245 |
-
timestamp=_now_iso(),
|
| 2246 |
-
),
|
| 2247 |
-
)
|
| 2248 |
-
|
| 2249 |
-
# Tool call: browser.navigate
|
| 2250 |
-
# Tool call: validate.url (check URL before navigating)
|
| 2251 |
-
step_num += 1
|
| 2252 |
-
yield _record_step(
|
| 2253 |
-
session,
|
| 2254 |
-
ScrapeStep(
|
| 2255 |
-
step_number=step_num,
|
| 2256 |
-
action="tool_call",
|
| 2257 |
-
url=url,
|
| 2258 |
-
status="running",
|
| 2259 |
-
message="validate.url(url)",
|
| 2260 |
-
extracted_data={
|
| 2261 |
-
"tool_name": "validate.url",
|
| 2262 |
-
"tool_description": "Validate URL format before navigation",
|
| 2263 |
-
"parameters": {"url": url},
|
| 2264 |
-
},
|
| 2265 |
-
timestamp=_now_iso(),
|
| 2266 |
-
),
|
| 2267 |
-
)
|
| 2268 |
-
|
| 2269 |
-
# Simple URL validation
|
| 2270 |
-
parsed_url = urlparse(url)
|
| 2271 |
-
url_valid = bool(parsed_url.scheme and parsed_url.netloc)
|
| 2272 |
-
|
| 2273 |
-
yield _record_step(
|
| 2274 |
-
session,
|
| 2275 |
-
ScrapeStep(
|
| 2276 |
-
step_number=step_num,
|
| 2277 |
-
action="tool_call",
|
| 2278 |
-
url=url,
|
| 2279 |
-
status="completed" if url_valid else "failed",
|
| 2280 |
-
message=f"validate.url() → {'valid' if url_valid else 'invalid'}",
|
| 2281 |
-
reward=0.02 if url_valid else 0.0,
|
| 2282 |
-
extracted_data={
|
| 2283 |
-
"tool_name": "validate.url",
|
| 2284 |
-
"result": {
|
| 2285 |
-
"valid": url_valid,
|
| 2286 |
-
"scheme": parsed_url.scheme,
|
| 2287 |
-
"domain": parsed_url.netloc,
|
| 2288 |
-
},
|
| 2289 |
-
},
|
| 2290 |
-
timestamp=_now_iso(),
|
| 2291 |
-
),
|
| 2292 |
-
)
|
| 2293 |
-
|
| 2294 |
-
if not url_valid:
|
| 2295 |
-
session["errors"].append(f"Invalid URL: {url}")
|
| 2296 |
-
return
|
| 2297 |
-
|
| 2298 |
-
# Tool call: browser.navigate
|
| 2299 |
-
step_num += 1
|
| 2300 |
-
yield _record_step(
|
| 2301 |
-
session,
|
| 2302 |
-
ScrapeStep(
|
| 2303 |
-
step_number=step_num,
|
| 2304 |
-
action="tool_call",
|
| 2305 |
-
url=url,
|
| 2306 |
-
status="running",
|
| 2307 |
-
message="browser.navigate(url)",
|
| 2308 |
-
extracted_data={
|
| 2309 |
-
"tool_name": "browser.navigate",
|
| 2310 |
-
"tool_description": "Navigate browser to target URL",
|
| 2311 |
-
"parameters": {"url": url},
|
| 2312 |
-
},
|
| 2313 |
-
timestamp=_now_iso(),
|
| 2314 |
-
),
|
| 2315 |
-
)
|
| 2316 |
-
|
| 2317 |
-
navigate_action = Action(
|
| 2318 |
-
action_type=ActionType.NAVIGATE,
|
| 2319 |
-
parameters={"url": url},
|
| 2320 |
-
reasoning=f"Navigate to target URL: {url}",
|
| 2321 |
-
)
|
| 2322 |
-
nav_obs, reward, _, _, _, nav_info = await env.step(navigate_action)
|
| 2323 |
-
total_reward += reward
|
| 2324 |
-
|
| 2325 |
-
nav_success = nav_info.get("action_result", {}).get("success", bool(nav_obs.page_html))
|
| 2326 |
-
|
| 2327 |
-
yield _record_step(
|
| 2328 |
-
session,
|
| 2329 |
-
ScrapeStep(
|
| 2330 |
-
step_number=step_num,
|
| 2331 |
-
action="tool_call",
|
| 2332 |
-
url=url,
|
| 2333 |
-
status="completed" if nav_success else "failed",
|
| 2334 |
-
message="browser.navigate(url) → success" if nav_success else "browser.navigate(url) → failed",
|
| 2335 |
-
reward=0.05,
|
| 2336 |
-
extracted_data={
|
| 2337 |
-
"tool_name": "browser.navigate",
|
| 2338 |
-
"result": {"success": nav_success, "html_length": len(nav_obs.page_html) if nav_obs.page_html else 0},
|
| 2339 |
-
},
|
| 2340 |
-
timestamp=_now_iso(),
|
| 2341 |
-
),
|
| 2342 |
-
)
|
| 2343 |
-
|
| 2344 |
-
yield _record_step(
|
| 2345 |
-
session,
|
| 2346 |
-
ScrapeStep(
|
| 2347 |
-
step_number=step_num,
|
| 2348 |
-
action="navigate",
|
| 2349 |
-
url=url,
|
| 2350 |
-
status="completed" if nav_success else "failed",
|
| 2351 |
-
message=f"Navigated to {url}" if nav_success else "Navigation failed",
|
| 2352 |
-
reward=reward,
|
| 2353 |
-
timestamp=_now_iso(),
|
| 2354 |
-
),
|
| 2355 |
-
)
|
| 2356 |
-
|
| 2357 |
-
if not nav_success or not nav_obs.page_html:
|
| 2358 |
-
session["errors"].append(f"Failed to navigate to {url}")
|
| 2359 |
-
return
|
| 2360 |
-
|
| 2361 |
-
# Tool call: html.parse (parse HTML into DOM)
|
| 2362 |
-
step_num += 1
|
| 2363 |
-
yield _record_step(
|
| 2364 |
-
session,
|
| 2365 |
-
ScrapeStep(
|
| 2366 |
-
step_number=step_num,
|
| 2367 |
-
action="tool_call",
|
| 2368 |
-
url=url,
|
| 2369 |
-
status="running",
|
| 2370 |
-
message="html.parse(content)",
|
| 2371 |
-
extracted_data={
|
| 2372 |
-
"tool_name": "html.parse",
|
| 2373 |
-
"tool_description": "Parse HTML document into DOM structure",
|
| 2374 |
-
"parameters": {"parser": "html.parser", "content_length": len(nav_obs.page_html)},
|
| 2375 |
-
},
|
| 2376 |
-
timestamp=_now_iso(),
|
| 2377 |
-
),
|
| 2378 |
-
)
|
| 2379 |
-
|
| 2380 |
-
yield _record_step(
|
| 2381 |
-
session,
|
| 2382 |
-
ScrapeStep(
|
| 2383 |
-
step_number=step_num,
|
| 2384 |
-
action="tool_call",
|
| 2385 |
-
url=url,
|
| 2386 |
-
status="completed",
|
| 2387 |
-
message="html.parse() → DOM ready",
|
| 2388 |
-
reward=0.05,
|
| 2389 |
-
extracted_data={
|
| 2390 |
-
"tool_name": "html.parse",
|
| 2391 |
-
"result": {"parsed": True, "html_length": len(nav_obs.page_html)},
|
| 2392 |
-
},
|
| 2393 |
-
timestamp=_now_iso(),
|
| 2394 |
-
),
|
| 2395 |
-
)
|
| 2396 |
-
|
| 2397 |
-
# Extract fields
|
| 2398 |
-
extracted = {}
|
| 2399 |
-
fields_to_extract = _extract_fields_for_complexity(request.complexity)
|
| 2400 |
-
|
| 2401 |
-
for field_name in fields_to_extract:
|
| 2402 |
-
step_num += 1
|
| 2403 |
-
# Tool call: html.extract
|
| 2404 |
-
yield _record_step(
|
| 2405 |
-
session,
|
| 2406 |
-
ScrapeStep(
|
| 2407 |
-
step_number=step_num,
|
| 2408 |
-
action="tool_call",
|
| 2409 |
-
url=url,
|
| 2410 |
-
status="running",
|
| 2411 |
-
message=f"html.extract(field='{field_name}')",
|
| 2412 |
-
extracted_data={
|
| 2413 |
-
"tool_name": "html.extract",
|
| 2414 |
-
"tool_description": f"Extract {field_name} from HTML document",
|
| 2415 |
-
"parameters": {"field_name": field_name},
|
| 2416 |
-
},
|
| 2417 |
-
timestamp=_now_iso(),
|
| 2418 |
-
),
|
| 2419 |
-
)
|
| 2420 |
-
|
| 2421 |
-
extract_action = Action(
|
| 2422 |
-
action_type=ActionType.EXTRACT_FIELD,
|
| 2423 |
-
parameters={"field_name": field_name},
|
| 2424 |
-
reasoning=f"Extract {field_name} from page",
|
| 2425 |
-
)
|
| 2426 |
-
obs, reward, _, _, _, _ = await env.step(extract_action)
|
| 2427 |
-
total_reward += reward
|
| 2428 |
-
|
| 2429 |
-
if obs.extracted_so_far:
|
| 2430 |
-
for ef in obs.extracted_so_far:
|
| 2431 |
-
if ef.field_name == field_name:
|
| 2432 |
-
extracted[field_name] = ef.value
|
| 2433 |
-
break
|
| 2434 |
-
|
| 2435 |
-
value_preview = str(extracted.get(field_name, ""))[:100]
|
| 2436 |
-
yield _record_step(
|
| 2437 |
-
session,
|
| 2438 |
-
ScrapeStep(
|
| 2439 |
-
step_number=step_num,
|
| 2440 |
-
action="tool_call",
|
| 2441 |
-
url=url,
|
| 2442 |
-
status="completed",
|
| 2443 |
-
message=f"html.extract(field='{field_name}') → {value_preview}",
|
| 2444 |
-
reward=0.05,
|
| 2445 |
-
extracted_data={
|
| 2446 |
-
"tool_name": "html.extract",
|
| 2447 |
-
"result": {field_name: extracted.get(field_name)},
|
| 2448 |
-
},
|
| 2449 |
-
timestamp=_now_iso(),
|
| 2450 |
-
),
|
| 2451 |
-
)
|
| 2452 |
-
|
| 2453 |
-
yield _record_step(
|
| 2454 |
-
session,
|
| 2455 |
-
ScrapeStep(
|
| 2456 |
-
step_number=step_num,
|
| 2457 |
-
action="extract",
|
| 2458 |
-
url=url,
|
| 2459 |
-
status="completed",
|
| 2460 |
-
message=f"Extracted {field_name}",
|
| 2461 |
-
reward=reward,
|
| 2462 |
-
extracted_data={field_name: extracted.get(field_name)},
|
| 2463 |
-
timestamp=_now_iso(),
|
| 2464 |
-
),
|
| 2465 |
-
)
|
| 2466 |
-
|
| 2467 |
-
# Verification step
|
| 2468 |
-
step_num += 1
|
| 2469 |
-
extracted_count = len([f for f in fields_to_extract if f in extracted])
|
| 2470 |
-
verification_score = extracted_count / len(fields_to_extract) if fields_to_extract else 0.0
|
| 2471 |
-
total_reward += verification_score
|
| 2472 |
-
|
| 2473 |
-
yield _record_step(
|
| 2474 |
-
session,
|
| 2475 |
-
ScrapeStep(
|
| 2476 |
-
step_number=step_num,
|
| 2477 |
-
action="verify",
|
| 2478 |
-
url=url,
|
| 2479 |
-
status="completed",
|
| 2480 |
-
message=f"Verifier checked extraction completeness ({extracted_count}/{len(fields_to_extract)})",
|
| 2481 |
-
reward=verification_score,
|
| 2482 |
-
extracted_data={"coverage": verification_score},
|
| 2483 |
-
timestamp=_now_iso(),
|
| 2484 |
-
),
|
| 2485 |
-
)
|
| 2486 |
-
|
| 2487 |
-
# Complete
|
| 2488 |
-
step_num += 1
|
| 2489 |
-
done_action = Action(
|
| 2490 |
-
action_type=ActionType.DONE,
|
| 2491 |
-
parameters={"success": True},
|
| 2492 |
-
reasoning="Extraction complete",
|
| 2493 |
-
)
|
| 2494 |
-
_, done_reward, _, _, _, _ = await env.step(done_action)
|
| 2495 |
-
total_reward += done_reward
|
| 2496 |
-
|
| 2497 |
-
yield _record_step(
|
| 2498 |
-
session,
|
| 2499 |
-
ScrapeStep(
|
| 2500 |
-
step_number=step_num,
|
| 2501 |
-
action="complete",
|
| 2502 |
-
url=url,
|
| 2503 |
-
status="completed",
|
| 2504 |
-
message=f"Completed scraping {url}",
|
| 2505 |
-
reward=done_reward,
|
| 2506 |
-
extracted_data={**extracted, "total_reward": total_reward},
|
| 2507 |
-
timestamp=_now_iso(),
|
| 2508 |
-
),
|
| 2509 |
-
)
|
| 2510 |
-
|
| 2511 |
-
session["total_reward"] += total_reward
|
| 2512 |
-
session["extracted_data"][url] = extracted
|
| 2513 |
-
_write_session_json_artifact(
|
| 2514 |
-
session,
|
| 2515 |
-
f"{_safe_artifact_name(urlparse(url).netloc or url)}_extracted.json",
|
| 2516 |
-
extracted,
|
| 2517 |
-
)
|
| 2518 |
-
|
| 2519 |
-
|
| 2520 |
-
async def _scrape_with_exploration(
|
| 2521 |
-
session: dict[str, Any],
|
| 2522 |
-
session_id: str,
|
| 2523 |
-
env,
|
| 2524 |
-
request: ScrapeRequest,
|
| 2525 |
-
navigation_plan: dict[str, Any],
|
| 2526 |
-
url: str,
|
| 2527 |
-
step_num: int,
|
| 2528 |
-
total_reward: float,
|
| 2529 |
-
) -> AsyncGenerator[dict[str, Any], None]:
|
| 2530 |
-
"""Scrape with intelligent exploration based on instructions."""
|
| 2531 |
-
|
| 2532 |
-
# For now, fallback to single page - this can be enhanced later
|
| 2533 |
-
async for result in _scrape_single_page(session, session_id, env, request, url, step_num, total_reward):
|
| 2534 |
-
yield result
|
| 2535 |
-
|
| 2536 |
-
|
| 2537 |
-
async def scrape_stream(
|
| 2538 |
-
session_id: str,
|
| 2539 |
request: ScrapeRequest,
|
| 2540 |
settings: Settings,
|
| 2541 |
memory_manager: MemoryManager,
|
|
@@ -2991,8 +2261,6 @@ async def scrape_stream(
|
|
| 2991 |
if quality_status == "completed"
|
| 2992 |
else f"Verifier assembled only {len(gold_rows)} rows; expected >= 100"
|
| 2993 |
)
|
| 2994 |
-
if quality_status != "completed":
|
| 2995 |
-
session["errors"].append("Gold dataset row count below quality threshold (100 rows).")
|
| 2996 |
|
| 2997 |
quality_event = _record_step(
|
| 2998 |
session,
|
|
@@ -3011,7 +2279,19 @@ async def scrape_stream(
|
|
| 3011 |
await manager.broadcast(quality_event, session_id)
|
| 3012 |
yield _sse_event(quality_event)
|
| 3013 |
else:
|
| 3014 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3015 |
|
| 3016 |
if (
|
| 3017 |
any(plugin_id in enabled_plugins for plugin_id in python_plugin_ids)
|
|
|
|
| 7 |
import io
|
| 8 |
import json
|
| 9 |
import logging
|
| 10 |
+
import os
|
| 11 |
import re
|
| 12 |
import shutil
|
| 13 |
import tempfile
|
|
|
|
| 649 |
return communities
|
| 650 |
|
| 651 |
|
| 652 |
+
def _fallback_reddit_communities_static(limit: int = 25) -> list[dict[str, Any]]:
|
| 653 |
+
"""Provide deterministic Reddit community rows when direct fetch is unavailable."""
|
| 654 |
+
|
| 655 |
+
names = [
|
| 656 |
+
"AskReddit",
|
| 657 |
+
"funny",
|
| 658 |
+
"gaming",
|
| 659 |
+
"worldnews",
|
| 660 |
+
"todayilearned",
|
| 661 |
+
"science",
|
| 662 |
+
"movies",
|
| 663 |
+
"technology",
|
| 664 |
+
"pics",
|
| 665 |
+
"news",
|
| 666 |
+
"aww",
|
| 667 |
+
"sports",
|
| 668 |
+
"Music",
|
| 669 |
+
"books",
|
| 670 |
+
"food",
|
| 671 |
+
"dataisbeautiful",
|
| 672 |
+
"MachineLearning",
|
| 673 |
+
"programming",
|
| 674 |
+
"python",
|
| 675 |
+
"javascript",
|
| 676 |
+
"learnprogramming",
|
| 677 |
+
"wallstreetbets",
|
| 678 |
+
"explainlikeimfive",
|
| 679 |
+
"history",
|
| 680 |
+
"space",
|
| 681 |
+
]
|
| 682 |
+
rows: list[dict[str, Any]] = []
|
| 683 |
+
for name in names[:limit]:
|
| 684 |
+
rows.append(
|
| 685 |
+
{
|
| 686 |
+
"subreddit": f"r/{name}",
|
| 687 |
+
"title": f"r/{name}",
|
| 688 |
+
"subscribers": 0,
|
| 689 |
+
"active_users": 0,
|
| 690 |
+
"url": f"https://www.reddit.com/r/{name}/",
|
| 691 |
+
"description": "Static fallback community entry",
|
| 692 |
+
}
|
| 693 |
+
)
|
| 694 |
+
return rows
|
| 695 |
+
|
| 696 |
+
|
| 697 |
+
def _fetch_reddit_communities(limit: int = 25) -> tuple[list[dict[str, Any]], str]:
|
| 698 |
+
"""Compatibility helper used by tests and optional monkeypatch overrides."""
|
| 699 |
+
|
| 700 |
+
return _fallback_reddit_communities_static(limit), "static_fallback"
|
| 701 |
+
|
| 702 |
+
|
| 703 |
async def _resolve_assets(
|
| 704 |
assets: list[str],
|
| 705 |
enabled_plugins: list[str],
|
|
|
|
| 1003 |
remove_environment(episode_id)
|
| 1004 |
|
| 1005 |
|
| 1006 |
+
def _agentic_live_llm_enabled() -> bool:
|
| 1007 |
+
"""Return True when live LLM calls should be used for agentic planning/extraction."""
|
| 1008 |
+
|
| 1009 |
+
if os.getenv("SCRAPERL_DISABLE_LIVE_LLM") == "1":
|
| 1010 |
+
return False
|
| 1011 |
+
if os.getenv("PYTEST_CURRENT_TEST"):
|
| 1012 |
+
return False
|
| 1013 |
+
return True
|
| 1014 |
+
|
| 1015 |
+
|
| 1016 |
+
def _fallback_navigation_url(
|
| 1017 |
+
base_url: str,
|
| 1018 |
+
instructions: str,
|
| 1019 |
+
navigation_plan: dict[str, Any],
|
| 1020 |
+
) -> str:
|
| 1021 |
+
"""Derive a deterministic navigation URL when LLM planning is unavailable."""
|
| 1022 |
+
|
| 1023 |
+
normalized = _coerce_url_asset(base_url) or base_url
|
| 1024 |
+
if "://" not in normalized:
|
| 1025 |
+
normalized = f"https://{normalized}"
|
| 1026 |
+
|
| 1027 |
+
parsed = urlparse(normalized)
|
| 1028 |
+
host = (parsed.netloc or parsed.path).lower()
|
| 1029 |
+
instruction_text = (instructions or "").lower()
|
| 1030 |
+
strategy = str(navigation_plan.get("strategy") or "").lower()
|
| 1031 |
+
|
| 1032 |
+
if "github.com" in host and (
|
| 1033 |
+
strategy == "github_trending"
|
| 1034 |
+
or "trending" in instruction_text
|
| 1035 |
+
or ("top" in instruction_text and "repo" in instruction_text)
|
| 1036 |
+
):
|
| 1037 |
+
return f"{parsed.scheme}://{parsed.netloc}/trending"
|
| 1038 |
+
|
| 1039 |
+
if "reddit.com" in host and (
|
| 1040 |
+
strategy == "reddit_trending"
|
| 1041 |
+
or "trending" in instruction_text
|
| 1042 |
+
or "communit" in instruction_text
|
| 1043 |
+
):
|
| 1044 |
+
return f"{parsed.scheme}://{parsed.netloc}/r/popular/"
|
| 1045 |
+
|
| 1046 |
+
return normalized
|
| 1047 |
+
|
| 1048 |
+
|
| 1049 |
+
def _requested_columns_from_output_instructions(output_instructions: str | None) -> list[str]:
|
| 1050 |
+
"""Extract requested output columns from instructions like 'csv of username, repo, stars'."""
|
| 1051 |
+
|
| 1052 |
+
if not output_instructions:
|
| 1053 |
+
return []
|
| 1054 |
+
|
| 1055 |
+
cleaned = output_instructions.strip()
|
| 1056 |
+
cleaned = re.sub(r"^(?:csv|json|table)\s+of\s+", "", cleaned, flags=re.IGNORECASE)
|
| 1057 |
+
cleaned = cleaned.replace(" and ", ", ")
|
| 1058 |
+
columns: list[str] = []
|
| 1059 |
+
for piece in cleaned.split(","):
|
| 1060 |
+
candidate = re.sub(r"[^A-Za-z0-9_]+", " ", piece).strip().lower().replace(" ", "_")
|
| 1061 |
+
if candidate and candidate not in columns:
|
| 1062 |
+
columns.append(candidate)
|
| 1063 |
+
return columns
|
| 1064 |
+
|
| 1065 |
+
|
| 1066 |
+
def _fallback_extraction_code(output_instructions: str | None) -> str:
|
| 1067 |
+
"""Build deterministic extraction code when live LLM code generation is unavailable."""
|
| 1068 |
+
|
| 1069 |
+
columns = _requested_columns_from_output_instructions(output_instructions) or [
|
| 1070 |
+
"title",
|
| 1071 |
+
"url",
|
| 1072 |
+
"content",
|
| 1073 |
+
]
|
| 1074 |
+
columns_literal = repr(columns)
|
| 1075 |
+
return f"""
|
| 1076 |
+
columns = {columns_literal}
|
| 1077 |
+
rows = []
|
| 1078 |
+
seen = set()
|
| 1079 |
+
anchors = soup.select("a[href]")
|
| 1080 |
+
|
| 1081 |
+
for anchor in anchors:
|
| 1082 |
+
href = (anchor.get("href") or "").strip()
|
| 1083 |
+
text = anchor.get_text(" ", strip=True)
|
| 1084 |
+
if not href and not text:
|
| 1085 |
+
continue
|
| 1086 |
+
if href.startswith("/"):
|
| 1087 |
+
full_href = f"{{url.rstrip('/')}}{{href}}"
|
| 1088 |
+
else:
|
| 1089 |
+
full_href = href
|
| 1090 |
+
|
| 1091 |
+
repo_owner = ""
|
| 1092 |
+
repo_name = ""
|
| 1093 |
+
path = full_href.split("://", 1)[-1]
|
| 1094 |
+
path_parts = [part for part in path.split("/") if part]
|
| 1095 |
+
if len(path_parts) >= 3:
|
| 1096 |
+
repo_owner = path_parts[1]
|
| 1097 |
+
repo_name = path_parts[2]
|
| 1098 |
+
|
| 1099 |
+
container = anchor.find_parent(["article", "tr", "li", "div"])
|
| 1100 |
+
container_text = container.get_text(" ", strip=True) if container else text
|
| 1101 |
+
star_match = re.search(r"([0-9][0-9,\\.kKmM]*)\\s*(?:stars?|star)", container_text, re.IGNORECASE)
|
| 1102 |
+
fork_match = re.search(r"([0-9][0-9,\\.kKmM]*)\\s*(?:forks?|fork)", container_text, re.IGNORECASE)
|
| 1103 |
+
|
| 1104 |
+
row = {{}}
|
| 1105 |
+
for column in columns:
|
| 1106 |
+
lower = column.lower()
|
| 1107 |
+
if lower in {{"url", "link", "href"}}:
|
| 1108 |
+
row[column] = full_href
|
| 1109 |
+
elif lower in {{"title", "name", "text", "content"}}:
|
| 1110 |
+
row[column] = text or container_text
|
| 1111 |
+
elif lower in {{"username", "user", "owner"}}:
|
| 1112 |
+
row[column] = repo_owner
|
| 1113 |
+
elif lower in {{"repo", "repository", "repo_name"}}:
|
| 1114 |
+
row[column] = repo_name
|
| 1115 |
+
elif lower in {{"stars", "star", "star_count"}}:
|
| 1116 |
+
row[column] = star_match.group(1) if star_match else ""
|
| 1117 |
+
elif lower in {{"forks", "fork", "fork_count"}}:
|
| 1118 |
+
row[column] = fork_match.group(1) if fork_match else ""
|
| 1119 |
+
else:
|
| 1120 |
+
row[column] = ""
|
| 1121 |
+
|
| 1122 |
+
row_key = tuple(row.get(column, "") for column in columns)
|
| 1123 |
+
if row_key in seen:
|
| 1124 |
+
continue
|
| 1125 |
+
seen.add(row_key)
|
| 1126 |
+
|
| 1127 |
+
if any(value for value in row.values()):
|
| 1128 |
+
rows.append(row)
|
| 1129 |
+
if len(rows) >= 25:
|
| 1130 |
+
break
|
| 1131 |
+
|
| 1132 |
+
if not rows:
|
| 1133 |
+
rows = [{{column: "" for column in columns}}]
|
| 1134 |
+
|
| 1135 |
+
extracted_data = rows
|
| 1136 |
+
"""
|
| 1137 |
+
|
| 1138 |
+
|
| 1139 |
async def _scrape_with_agentic_llm(
|
| 1140 |
session: dict[str, Any],
|
| 1141 |
session_id: str,
|
|
|
|
| 1189 |
|
| 1190 |
URL:"""
|
| 1191 |
|
| 1192 |
+
live_llm_enabled = _agentic_live_llm_enabled()
|
| 1193 |
+
target_url = _fallback_navigation_url(url, request.instructions, navigation_plan)
|
| 1194 |
+
navigation_mode = "heuristic"
|
| 1195 |
+
if live_llm_enabled:
|
| 1196 |
+
try:
|
| 1197 |
+
nav_response = await asyncio.wait_for(
|
| 1198 |
+
model_router.complete(
|
| 1199 |
+
messages=[{"role": "user", "content": navigation_prompt}],
|
| 1200 |
+
task_type=TaskType.REASONING,
|
| 1201 |
+
model=request.model,
|
| 1202 |
+
),
|
| 1203 |
+
timeout=12,
|
| 1204 |
+
)
|
| 1205 |
+
candidate = nav_response.content.strip()
|
| 1206 |
+
if candidate:
|
| 1207 |
+
if not candidate.startswith("http"):
|
| 1208 |
+
if "://" not in url:
|
| 1209 |
+
candidate = f"https://{url}/{candidate.lstrip('/')}"
|
| 1210 |
+
else:
|
| 1211 |
+
parsed = urlparse(url)
|
| 1212 |
+
candidate = f"{parsed.scheme}://{parsed.netloc}/{candidate.lstrip('/')}"
|
| 1213 |
+
target_url = candidate
|
| 1214 |
+
navigation_mode = "llm"
|
| 1215 |
+
except Exception as e:
|
| 1216 |
+
logger.warning("LLM navigation decision failed, using heuristic fallback: %s", e)
|
| 1217 |
|
| 1218 |
# Tool call: LLM navigation planning
|
| 1219 |
yield _record_step(
|
|
|
|
| 1229 |
"tool_description": "LLM decides optimal navigation URL based on instructions",
|
| 1230 |
"parameters": {"instructions": request.instructions, "base_url": url},
|
| 1231 |
"result": target_url,
|
| 1232 |
+
"mode": navigation_mode,
|
| 1233 |
},
|
| 1234 |
reward=0.15,
|
| 1235 |
timestamp=_now_iso(),
|
| 1236 |
),
|
| 1237 |
)
|
| 1238 |
total_reward += 0.15
|
| 1239 |
+
|
| 1240 |
+
# Validate URL before navigation
|
| 1241 |
+
step_num += 1
|
| 1242 |
+
is_valid_target = _is_url_asset(target_url)
|
| 1243 |
+
yield _record_step(
|
| 1244 |
+
session,
|
| 1245 |
+
ScrapeStep(
|
| 1246 |
+
step_number=step_num,
|
| 1247 |
+
action="tool_call",
|
| 1248 |
+
url=target_url,
|
| 1249 |
+
status="complete",
|
| 1250 |
+
message=f"validate.url(url='{target_url}') → {'valid' if is_valid_target else 'invalid'}",
|
| 1251 |
+
extracted_data={
|
| 1252 |
+
"tool_name": "validate.url",
|
| 1253 |
+
"tool_description": "Validate and normalize navigation URL",
|
| 1254 |
+
"parameters": {"url": target_url},
|
| 1255 |
+
"result": {
|
| 1256 |
+
"valid": is_valid_target,
|
| 1257 |
+
"normalized_url": _coerce_url_asset(target_url) or target_url,
|
| 1258 |
+
},
|
| 1259 |
+
},
|
| 1260 |
+
reward=0.05 if is_valid_target else 0.0,
|
| 1261 |
+
timestamp=_now_iso(),
|
| 1262 |
+
),
|
| 1263 |
+
)
|
| 1264 |
+
total_reward += 0.05 if is_valid_target else 0.0
|
| 1265 |
|
| 1266 |
# Step 2: Navigate to the decided URL
|
| 1267 |
step_num += 1
|
|
|
|
| 1354 |
),
|
| 1355 |
)
|
| 1356 |
|
| 1357 |
+
# Extract links for tool visibility and fallback processing
|
| 1358 |
+
step_num += 1
|
| 1359 |
+
yield _record_step(
|
| 1360 |
+
session,
|
| 1361 |
+
ScrapeStep(
|
| 1362 |
+
step_number=step_num,
|
| 1363 |
+
action="tool_call",
|
| 1364 |
+
url=target_url,
|
| 1365 |
+
status="running",
|
| 1366 |
+
message="extract.urls(html)",
|
| 1367 |
+
extracted_data={
|
| 1368 |
+
"tool_name": "extract.urls",
|
| 1369 |
+
"tool_description": "Extract hyperlinks from parsed HTML",
|
| 1370 |
+
"parameters": {"scope": "document"},
|
| 1371 |
+
},
|
| 1372 |
+
timestamp=_now_iso(),
|
| 1373 |
+
),
|
| 1374 |
+
)
|
| 1375 |
+
extracted_links: list[str] = []
|
| 1376 |
+
for anchor in soup.find_all("a", href=True):
|
| 1377 |
+
href = str(anchor.get("href", "")).strip()
|
| 1378 |
+
if not href:
|
| 1379 |
+
continue
|
| 1380 |
+
if href.startswith("/"):
|
| 1381 |
+
href = f"{target_url.rstrip('/')}{href}"
|
| 1382 |
+
if href not in extracted_links:
|
| 1383 |
+
extracted_links.append(href)
|
| 1384 |
+
if len(extracted_links) >= 200:
|
| 1385 |
+
break
|
| 1386 |
+
yield _record_step(
|
| 1387 |
+
session,
|
| 1388 |
+
ScrapeStep(
|
| 1389 |
+
step_number=step_num,
|
| 1390 |
+
action="tool_call",
|
| 1391 |
+
url=target_url,
|
| 1392 |
+
status="complete",
|
| 1393 |
+
message=f"extract.urls() → {len(extracted_links)} links",
|
| 1394 |
+
extracted_data={
|
| 1395 |
+
"tool_name": "extract.urls",
|
| 1396 |
+
"result": {"count": len(extracted_links), "sample": extracted_links[:5]},
|
| 1397 |
+
},
|
| 1398 |
+
reward=0.05,
|
| 1399 |
+
timestamp=_now_iso(),
|
| 1400 |
+
),
|
| 1401 |
+
)
|
| 1402 |
+
total_reward += 0.05
|
| 1403 |
+
|
| 1404 |
+
# Extract emails for tool visibility and fallback processing
|
| 1405 |
+
step_num += 1
|
| 1406 |
+
yield _record_step(
|
| 1407 |
+
session,
|
| 1408 |
+
ScrapeStep(
|
| 1409 |
+
step_number=step_num,
|
| 1410 |
+
action="tool_call",
|
| 1411 |
+
url=target_url,
|
| 1412 |
+
status="running",
|
| 1413 |
+
message="extract.emails(html)",
|
| 1414 |
+
extracted_data={
|
| 1415 |
+
"tool_name": "extract.emails",
|
| 1416 |
+
"tool_description": "Extract email addresses from page content",
|
| 1417 |
+
"parameters": {"pattern": "email regex"},
|
| 1418 |
+
},
|
| 1419 |
+
timestamp=_now_iso(),
|
| 1420 |
+
),
|
| 1421 |
+
)
|
| 1422 |
+
extracted_emails = sorted(set(re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", nav_obs.page_html)))
|
| 1423 |
+
yield _record_step(
|
| 1424 |
+
session,
|
| 1425 |
+
ScrapeStep(
|
| 1426 |
+
step_number=step_num,
|
| 1427 |
+
action="tool_call",
|
| 1428 |
+
url=target_url,
|
| 1429 |
+
status="complete",
|
| 1430 |
+
message=f"extract.emails() → {len(extracted_emails)} emails",
|
| 1431 |
+
extracted_data={
|
| 1432 |
+
"tool_name": "extract.emails",
|
| 1433 |
+
"result": {"count": len(extracted_emails), "sample": extracted_emails[:5]},
|
| 1434 |
+
},
|
| 1435 |
+
reward=0.05,
|
| 1436 |
+
timestamp=_now_iso(),
|
| 1437 |
+
),
|
| 1438 |
+
)
|
| 1439 |
+
total_reward += 0.05
|
| 1440 |
+
|
| 1441 |
+
# Extract quick structural fields
|
| 1442 |
+
step_num += 1
|
| 1443 |
+
yield _record_step(
|
| 1444 |
+
session,
|
| 1445 |
+
ScrapeStep(
|
| 1446 |
+
step_number=step_num,
|
| 1447 |
+
action="tool_call",
|
| 1448 |
+
url=target_url,
|
| 1449 |
+
status="running",
|
| 1450 |
+
message="html.extract(fields=['title','content','links'])",
|
| 1451 |
+
extracted_data={
|
| 1452 |
+
"tool_name": "html.extract",
|
| 1453 |
+
"tool_description": "Extract key structural fields for downstream processing",
|
| 1454 |
+
"parameters": {"fields": ["title", "content", "links"]},
|
| 1455 |
+
},
|
| 1456 |
+
timestamp=_now_iso(),
|
| 1457 |
+
),
|
| 1458 |
+
)
|
| 1459 |
+
page_title = soup.title.get_text(strip=True) if soup.title else ""
|
| 1460 |
+
page_content = soup.get_text(" ", strip=True)
|
| 1461 |
+
quick_extract = {
|
| 1462 |
+
"title": page_title,
|
| 1463 |
+
"content": page_content[:2000],
|
| 1464 |
+
"links": extracted_links[:100],
|
| 1465 |
+
}
|
| 1466 |
+
yield _record_step(
|
| 1467 |
+
session,
|
| 1468 |
+
ScrapeStep(
|
| 1469 |
+
step_number=step_num,
|
| 1470 |
+
action="tool_call",
|
| 1471 |
+
url=target_url,
|
| 1472 |
+
status="complete",
|
| 1473 |
+
message="html.extract() → fields ready",
|
| 1474 |
+
extracted_data={
|
| 1475 |
+
"tool_name": "html.extract",
|
| 1476 |
+
"result": {
|
| 1477 |
+
"title_length": len(page_title),
|
| 1478 |
+
"content_length": len(quick_extract["content"]),
|
| 1479 |
+
"link_count": len(quick_extract["links"]),
|
| 1480 |
+
},
|
| 1481 |
+
},
|
| 1482 |
+
reward=0.05,
|
| 1483 |
+
timestamp=_now_iso(),
|
| 1484 |
+
),
|
| 1485 |
+
)
|
| 1486 |
+
total_reward += 0.05
|
| 1487 |
+
|
| 1488 |
# Step 4: Ask LLM to generate extraction code
|
| 1489 |
step_num += 1
|
| 1490 |
|
|
|
|
| 1523 |
|
| 1524 |
Return ONLY executable Python code, no explanations or markdown:"""
|
| 1525 |
|
| 1526 |
+
extraction_code = _fallback_extraction_code(request.output_instructions)
|
| 1527 |
+
codegen_mode = "heuristic"
|
| 1528 |
+
if live_llm_enabled:
|
| 1529 |
+
try:
|
| 1530 |
+
code_response = await asyncio.wait_for(
|
| 1531 |
+
model_router.complete(
|
| 1532 |
+
messages=[{"role": "user", "content": extraction_prompt}],
|
| 1533 |
+
task_type=TaskType.CODE,
|
| 1534 |
+
model=request.model,
|
| 1535 |
+
temperature=0.3,
|
| 1536 |
+
),
|
| 1537 |
+
timeout=12,
|
| 1538 |
+
)
|
| 1539 |
+
candidate_code = code_response.content.strip()
|
| 1540 |
+
if "```python" in candidate_code:
|
| 1541 |
+
candidate_code = candidate_code.split("```python")[1].split("```")[0].strip()
|
| 1542 |
+
elif "```" in candidate_code:
|
| 1543 |
+
candidate_code = candidate_code.split("```")[1].split("```")[0].strip()
|
| 1544 |
+
if candidate_code:
|
| 1545 |
+
extraction_code = candidate_code
|
| 1546 |
+
codegen_mode = "llm"
|
| 1547 |
+
except Exception as e:
|
| 1548 |
+
logger.warning("LLM code generation failed, using heuristic extraction code: %s", e)
|
| 1549 |
+
|
| 1550 |
+
yield _record_step(
|
| 1551 |
+
session,
|
| 1552 |
+
ScrapeStep(
|
| 1553 |
+
step_number=step_num,
|
| 1554 |
+
action="tool_call",
|
| 1555 |
+
url=target_url,
|
| 1556 |
+
status="complete",
|
| 1557 |
+
message=f"{'llm' if codegen_mode == 'llm' else 'agent.fallback'}.generate_extraction_code() → {len(extraction_code)} chars",
|
| 1558 |
+
extracted_data={
|
| 1559 |
+
"tool_name": "llm.generate_extraction_code",
|
| 1560 |
+
"tool_description": "Generate extraction code from page context and requested output schema",
|
| 1561 |
+
"parameters": {
|
| 1562 |
+
"html_sample_length": len(html_sample),
|
| 1563 |
+
"instructions": request.instructions,
|
| 1564 |
+
"output_format": request.output_format.value,
|
| 1565 |
},
|
| 1566 |
+
"result": {"code_length": len(extraction_code), "mode": codegen_mode},
|
| 1567 |
+
},
|
| 1568 |
+
reward=0.2 if codegen_mode == "llm" else 0.05,
|
| 1569 |
+
timestamp=_now_iso(),
|
| 1570 |
+
),
|
| 1571 |
+
)
|
| 1572 |
+
total_reward += 0.2 if codegen_mode == "llm" else 0.05
|
|
|
|
|
|
|
| 1573 |
|
| 1574 |
# Step 5: Execute generated code in sandbox
|
| 1575 |
step_num += 1
|
|
|
|
| 1595 |
"soup": soup,
|
| 1596 |
"html": nav_obs.page_html,
|
| 1597 |
"url": target_url,
|
| 1598 |
+
"re": re,
|
| 1599 |
+
"urlparse": urlparse,
|
| 1600 |
"BeautifulSoup": BeautifulSoup,
|
| 1601 |
"extracted_data": [], # LLM code should populate this
|
| 1602 |
}
|
|
|
|
| 1804 |
logger.error(f"Intelligent scraping failed for {url}: {exc}")
|
| 1805 |
session["errors"].append(f"Scraping failed: {exc}")
|
| 1806 |
|
| 1807 |
+
async def scrape_stream(
|
| 1808 |
+
session_id: str,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1809 |
request: ScrapeRequest,
|
| 1810 |
settings: Settings,
|
| 1811 |
memory_manager: MemoryManager,
|
|
|
|
| 2261 |
if quality_status == "completed"
|
| 2262 |
else f"Verifier assembled only {len(gold_rows)} rows; expected >= 100"
|
| 2263 |
)
|
|
|
|
|
|
|
| 2264 |
|
| 2265 |
quality_event = _record_step(
|
| 2266 |
session,
|
|
|
|
| 2279 |
await manager.broadcast(quality_event, session_id)
|
| 2280 |
yield _sse_event(quality_event)
|
| 2281 |
else:
|
| 2282 |
+
quality_event = _record_step(
|
| 2283 |
+
session,
|
| 2284 |
+
ScrapeStep(
|
| 2285 |
+
step_number=len(session["steps"]) + 1,
|
| 2286 |
+
action="verifier",
|
| 2287 |
+
status="partial",
|
| 2288 |
+
message="Verifier could not assemble monthly gold rows from resolved sources",
|
| 2289 |
+
extracted_data={"row_count": 0, "sources": []},
|
| 2290 |
+
timestamp=_now_iso(),
|
| 2291 |
+
),
|
| 2292 |
+
)
|
| 2293 |
+
await manager.broadcast(quality_event, session_id)
|
| 2294 |
+
yield _sse_event(quality_event)
|
| 2295 |
|
| 2296 |
if (
|
| 2297 |
any(plugin_id in enabled_plugins for plugin_id in python_plugin_ids)
|