Save a paper to database Returns: Paper ID (existing or new)
(
arxiv_id: str,
doi: str = None,
title: str = "",
authors: List[str] = None,
abstract: str = "",
categories: List[str] = None,
source: str = "arxiv",
institution: str = None,
venue: str = None,
publish_date: str = None,
embedding: List[float] = None,
embedding_model: str = None,
)
| 554 | |
| 555 | |
| 556 | def save_paper( |
| 557 | arxiv_id: str, |
| 558 | doi: str = None, |
| 559 | title: str = "", |
| 560 | authors: List[str] = None, |
| 561 | abstract: str = "", |
| 562 | categories: List[str] = None, |
| 563 | source: str = "arxiv", |
| 564 | institution: str = None, |
| 565 | venue: str = None, |
| 566 | publish_date: str = None, |
| 567 | embedding: List[float] = None, |
| 568 | embedding_model: str = None, |
| 569 | ) -> int: |
| 570 | """ |
| 571 | Save a paper to database |
| 572 | |
| 573 | Returns: |
| 574 | Paper ID (existing or new) |
| 575 | """ |
| 576 | conn = get_connection() |
| 577 | cursor = conn.cursor() |
| 578 | |
| 579 | normalized_arxiv_id = _normalize_identifier(arxiv_id) |
| 580 | normalized_doi = _normalize_identifier(doi) |
| 581 | normalized_title = (title or "").strip() |
| 582 | |
| 583 | # 检查是否已存在(优先使用 arxiv_id) |
| 584 | if normalized_arxiv_id: |
| 585 | cursor.execute("SELECT id FROM papers WHERE arxiv_id = ?", (normalized_arxiv_id,)) |
| 586 | row = cursor.fetchone() |
| 587 | if row: |
| 588 | conn.close() |
| 589 | return row['id'] |
| 590 | |
| 591 | # 如果 arxiv_id 为空,使用 doi 检查 |
| 592 | if normalized_doi: |
| 593 | cursor.execute("SELECT id FROM papers WHERE doi = ?", (normalized_doi,)) |
| 594 | row = cursor.fetchone() |
| 595 | if row: |
| 596 | conn.close() |
| 597 | return row['id'] |
| 598 | |
| 599 | # 如果 arxiv_id 和 doi 都为空,使用 title 检查(避免完全重复) |
| 600 | if normalized_title: |
| 601 | cursor.execute("SELECT id FROM papers WHERE title = ?", (normalized_title,)) |
| 602 | row = cursor.fetchone() |
| 603 | if row: |
| 604 | conn.close() |
| 605 | return row['id'] |
| 606 | |
| 607 | try: |
| 608 | # 插入新论文 |
| 609 | cursor.execute(""" |
| 610 | INSERT INTO papers ( |
| 611 | arxiv_id, doi, title, authors, institution, abstract, |
| 612 | venue, publish_date, embedding, embedding_model, fetched_at |
| 613 | ) |
nothing calls this directly
no test coverage detected