Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: add Semantic Scholar and OpenAlex PDF downloaders

LearningCircuitd095cf0f4aec
diff --git a/src/local_deep_research/research_library/downloaders/__init__.py b/src/local_deep_research/research_library/downloaders/__init__.py
index 6367912da3..3f7ddffac6 100644
--- a/src/local_deep_research/research_library/downloaders/__init__.py
+++ b/src/local_deep_research/research_library/downloaders/__init__.py
@@ -8,6 +8,8 @@ from .arxiv import ArxivDownloader
from .pubmed import PubMedDownloader
from .biorxiv import BioRxivDownloader
from .direct_pdf import DirectPDFDownloader
+from .semantic_scholar import SemanticScholarDownloader
+from .openalex import OpenAlexDownloader
from .generic import GenericDownloader
__all__ = [
@@ -18,5 +20,7 @@ __all__ = [
"PubMedDownloader",
"BioRxivDownloader",
"DirectPDFDownloader",
+ "SemanticScholarDownloader",
+ "OpenAlexDownloader",
"GenericDownloader",
]
diff --git a/src/local_deep_research/research_library/downloaders/openalex.py b/src/local_deep_research/research_library/downloaders/openalex.py
new file mode 100644
index 0000000000..b70ec7edb3
--- /dev/null
+++ b/src/local_deep_research/research_library/downloaders/openalex.py
@@ -0,0 +1,186 @@
+"""
+OpenAlex PDF Downloader
+
+Downloads PDFs from OpenAlex using their API to find open access PDFs.
+OpenAlex aggregates open access information from multiple sources.
+"""
+
+import re
+from typing import Optional
+from urllib.parse import urlparse
+from loguru import logger
+
+from .base import BaseDownloader, ContentType, DownloadResult
+
+
+class OpenAlexDownloader(BaseDownloader):
+ """Downloader for OpenAlex papers with open access PDF support."""
+
+ def __init__(
+ self, timeout: int = 30, polite_pool_email: Optional[str] = None
+ ):
+ """
+ Initialize OpenAlex downloader.
+
+ Args:
+ timeout: Request timeout in seconds
+ polite_pool_email: Optional email for polite pool (faster API access)
+ """
+ super().__init__(timeout)
+ self.polite_pool_email = polite_pool_email
+ self.base_api_url = "https://api.openalex.org"
+
+ def can_handle(self, url: str) -> bool:
+ """Check if URL is from OpenAlex."""
+ try:
+ hostname = urlparse(url).hostname
+ return bool(
+ hostname
+ and (
+ hostname == "openalex.org"
+ or hostname.endswith(".openalex.org")
+ )
+ )
+ except Exception:
+ return False
+
+ def download(
+ self, url: str, content_type: ContentType = ContentType.PDF
+ ) -> Optional[bytes]:
+ """Download content from OpenAlex."""
+ result = self.download_with_result(url, content_type)
+ return result.content if result.is_success else None
+
+ def download_with_result(
+ self, url: str, content_type: ContentType = ContentType.PDF
+ ) -> DownloadResult:
+ """Download PDF and return detailed result with skip reason."""
+ # Only support PDF downloads for now
+ if content_type != ContentType.PDF:
+ return DownloadResult(
+ skip_reason="Text extraction not yet supported for OpenAlex"
+ )
+
+ # Extract work ID from URL
+ work_id = self._extract_work_id(url)
+ if not work_id:
+ return DownloadResult(
+ skip_reason="Invalid OpenAlex URL - could not extract work ID"
+ )
+
+ logger.info(f"Looking up OpenAlex work: {work_id}")
+
+ # Get work details from API to find PDF URL
+ pdf_url = self._get_pdf_url(work_id)
+
+ if not pdf_url:
+ return DownloadResult(
+ skip_reason="Not open access - no free PDF available"
+ )
+
+ # Download the PDF from the open access URL
+ logger.info(f"Downloading open access PDF from: {pdf_url}")
+ pdf_content = super()._download_pdf(pdf_url)
+
+ if pdf_content:
+ return DownloadResult(content=pdf_content, is_success=True)
+ else:
+ return DownloadResult(
+ skip_reason="Open access PDF URL found but download failed"
+ )
+
+ def _extract_work_id(self, url: str) -> Optional[str]:
+ """
+ Extract OpenAlex work ID from URL.
+
+ Handles formats like:
+ - https://openalex.org/W123456789
+ - https://openalex.org/works/W123456789
+
+ Returns:
+ Work ID (e.g., W123456789) or None if not found
+ """
+ # Pattern to extract work ID (W followed by digits)
+ patterns = [
+ r"openalex\.org/works/(W\d+)", # /works/W123456789
+ r"openalex\.org/(W\d+)", # Direct /W123456789
+ ]
+
+ for pattern in patterns:
+ match = re.search(pattern, url)
+ if match:
+ return match.group(1)
+
+ return None
+
+ def _get_pdf_url(self, work_id: str) -> Optional[str]:
+ """
+ Get open access PDF URL from OpenAlex API.
+
+ Args:
+ work_id: OpenAlex work ID (e.g., W123456789)
+
+ Returns:
+ PDF URL if available, None otherwise
+ """
+ try:
+ # Construct API request
+ api_url = f"{self.base_api_url}/works/{work_id}"
+ params = {"select": "id,open_access,best_oa_location"}
+
+ # Add polite pool email if available (gets faster API access)
+ headers = {}
+ if self.polite_pool_email:
+ headers["User-Agent"] = f"mailto:{self.polite_pool_email}"
+
+ # Make API request
+ response = self.session.get(
+ api_url, params=params, headers=headers, timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+
+ # Check if it's open access
+ open_access_info = data.get("open_access", {})
+ is_oa = open_access_info.get("is_oa", False)
+
+ if not is_oa:
+ logger.info(f"Work {work_id} is not open access")
+ return None
+
+ # Get PDF URL from best open access location
+ best_oa_location = data.get("best_oa_location", {})
+ if best_oa_location:
+ # Try pdf_url first, fall back to landing_page_url
+ pdf_url = best_oa_location.get("pdf_url")
+ if pdf_url:
+ logger.info(
+ f"Found open access PDF for work {work_id}: {pdf_url}"
+ )
+ return pdf_url
+
+ # Some works have landing page but no direct PDF
+ landing_url = best_oa_location.get("landing_page_url")
+ if landing_url:
+ logger.info(
+ f"Found landing page for work {work_id}: {landing_url}"
+ )
+ # Try to download from landing page (might be a direct PDF)
+ return landing_url
+
+ logger.info(
+ f"No PDF URL available for open access work {work_id}"
+ )
+ return None
+
+ elif response.status_code == 404:
+ logger.warning(f"Work not found in OpenAlex: {work_id}")
+ return None
+ else:
+ logger.warning(f"OpenAlex API error: {response.status_code}")
+ return None
+
+ except Exception:
+ logger.exception("Failed to query OpenAlex API")
+ return None
diff --git a/src/local_deep_research/research_library/downloaders/semantic_scholar.py b/src/local_deep_research/research_library/downloaders/semantic_scholar.py
new file mode 100644
index 0000000000..a3aa00144d
--- /dev/null
+++ b/src/local_deep_research/research_library/downloaders/semantic_scholar.py
@@ -0,0 +1,169 @@
+"""
+Semantic Scholar PDF Downloader
+
+Downloads PDFs from Semantic Scholar using their API to find open access PDFs.
+"""
+
+import re
+from typing import Optional
+from urllib.parse import urlparse
+from loguru import logger
+
+from .base import BaseDownloader, ContentType, DownloadResult
+
+
+class SemanticScholarDownloader(BaseDownloader):
+ """Downloader for Semantic Scholar papers with open access PDF support."""
+
+ def __init__(self, timeout: int = 30, api_key: Optional[str] = None):
+ """
+ Initialize Semantic Scholar downloader.
+
+ Args:
+ timeout: Request timeout in seconds
+ api_key: Optional Semantic Scholar API key for higher rate limits
+ """
+ super().__init__(timeout)
+ self.api_key = api_key
+ self.base_api_url = "https://api.semanticscholar.org/graph/v1"
+
+ def can_handle(self, url: str) -> bool:
+ """Check if URL is from Semantic Scholar."""
+ try:
+ hostname = urlparse(url).hostname
+ return bool(
+ hostname
+ and (
+ hostname == "semanticscholar.org"
+ or hostname.endswith(".semanticscholar.org")
+ )
+ )
+ except Exception:
+ return False
+
+ def download(
+ self, url: str, content_type: ContentType = ContentType.PDF
+ ) -> Optional[bytes]:
+ """Download content from Semantic Scholar."""
+ result = self.download_with_result(url, content_type)
+ return result.content if result.is_success else None
+
+ def download_with_result(
+ self, url: str, content_type: ContentType = ContentType.PDF
+ ) -> DownloadResult:
+ """Download PDF and return detailed result with skip reason."""
+ # Only support PDF downloads for now
+ if content_type != ContentType.PDF:
+ return DownloadResult(
+ skip_reason="Text extraction not yet supported for Semantic Scholar"
+ )
+
+ # Extract paper ID from URL
+ paper_id = self._extract_paper_id(url)
+ if not paper_id:
+ return DownloadResult(
+ skip_reason="Invalid Semantic Scholar URL - could not extract paper ID"
+ )
+
+ logger.info(f"Looking up Semantic Scholar paper: {paper_id}")
+
+ # Get paper details from API to find PDF URL
+ pdf_url = self._get_pdf_url(paper_id)
+
+ if not pdf_url:
+ return DownloadResult(
+ skip_reason="Not open access - subscription required"
+ )
+
+ # Download the PDF from the open access URL
+ logger.info(f"Downloading open access PDF from: {pdf_url}")
+ pdf_content = super()._download_pdf(pdf_url)
+
+ if pdf_content:
+ return DownloadResult(content=pdf_content, is_success=True)
+ else:
+ return DownloadResult(
+ skip_reason="Open access PDF URL found but download failed"
+ )
+
+ def _extract_paper_id(self, url: str) -> Optional[str]:
+ """
+ Extract Semantic Scholar paper ID from URL.
+
+ Handles formats like:
+ - https://www.semanticscholar.org/paper/abc123...
+ - https://www.semanticscholar.org/paper/Title-Here/abc123...
+
+ Returns:
+ Paper ID (hash) or None if not found
+ """
+ # Pattern to extract paper ID (40 character hex hash)
+ patterns = [
+ r"semanticscholar\.org/paper/([a-f0-9]{40})", # Direct ID
+ r"semanticscholar\.org/paper/[^/]+/([a-f0-9]{40})", # With title slug
+ ]
+
+ for pattern in patterns:
+ match = re.search(pattern, url)
+ if match:
+ return match.group(1)
+
+ return None
+
+ def _get_pdf_url(self, paper_id: str) -> Optional[str]:
+ """
+ Get open access PDF URL from Semantic Scholar API.
+
+ Args:
+ paper_id: Semantic Scholar paper ID (hash)
+
+ Returns:
+ PDF URL if available, None otherwise
+ """
+ try:
+ # Construct API request
+ api_url = f"{self.base_api_url}/paper/{paper_id}"
+ params = {"fields": "openAccessPdf"}
+
+ # Add API key header if available
+ headers = {}
+ if self.api_key:
+ headers["x-api-key"] = self.api_key
+
+ # Make API request
+ response = self.session.get(
+ api_url, params=params, headers=headers, timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+
+ # Extract PDF URL from openAccessPdf field
+ open_access_pdf = data.get("openAccessPdf")
+ if open_access_pdf and isinstance(open_access_pdf, dict):
+ pdf_url = open_access_pdf.get("url")
+ if pdf_url:
+ logger.info(
+ f"Found open access PDF for paper {paper_id}: {pdf_url}"
+ )
+ return pdf_url
+
+ logger.info(
+ f"No open access PDF available for paper {paper_id}"
+ )
+ return None
+
+ elif response.status_code == 404:
+ logger.warning(
+ f"Paper not found in Semantic Scholar: {paper_id}"
+ )
+ return None
+ else:
+ logger.warning(
+ f"Semantic Scholar API error: {response.status_code}"
+ )
+ return None
+
+ except Exception:
+ logger.exception("Failed to query Semantic Scholar API")
+ return None
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index 6feb279560..f80aa3fb56 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -48,6 +48,8 @@ from ..downloaders import (
PubMedDownloader,
BioRxivDownloader,
DirectPDFDownloader,
+ SemanticScholarDownloader,
+ OpenAlexDownloader,
GenericDownloader,
)
@@ -58,7 +60,7 @@ class DownloadService:
def __init__(self, username: str):
"""Initialize download service for a user."""
self.username = username
- self.settings = get_settings_manager()
+ self.settings = get_settings_manager(username=username)
# Get library path from settings
self.library_root = str(
@@ -75,8 +77,23 @@ class DownloadService:
# Initialize modular downloaders
# DirectPDFDownloader first for efficiency with direct PDF links
+
+ # Get Semantic Scholar API key from settings
+ semantic_scholar_api_key = self.settings.get_setting(
+ "search.engine.web.semantic_scholar.api_key", ""
+ )
+
self.downloaders = [
DirectPDFDownloader(timeout=30), # Handle direct PDF links first
+ SemanticScholarDownloader(
+ timeout=30,
+ api_key=semantic_scholar_api_key
+ if semantic_scholar_api_key
+ else None,
+ ),
+ OpenAlexDownloader(
+ timeout=30
+ ), # OpenAlex with API lookup (no key needed)
ArxivDownloader(timeout=30),
PubMedDownloader(timeout=30, rate_limit_delay=1.0),
BioRxivDownloader(timeout=30),
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py b/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py
index a6e1c63abe..376b9f7714 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py
@@ -274,6 +274,7 @@ Return ONLY the optimized search query with no explanation.
"year",
"authors",
"citationCount", # Add citation count for ranking
+ "openAccessPdf", # PDF URL for open access papers
]
if self.get_tldr: