收集指定日期范围内的论文 Args: start_date: 开始日期 (YYYYMMDD) end_date: 结束日期 (YYYYMMDD) categories: arXiv 类别列表 limit: 最大抓取数量 Returns: 新增论文数量
(
start_date: str,
end_date: str,
categories: List[str] = None,
limit: int = 500,
)
| 75 | |
| 76 | |
| 77 | def collect_papers( |
| 78 | start_date: str, |
| 79 | end_date: str, |
| 80 | categories: List[str] = None, |
| 81 | limit: int = 500, |
| 82 | ) -> int: |
| 83 | """ |
| 84 | 收集指定日期范围内的论文 |
| 85 | |
| 86 | Args: |
| 87 | start_date: 开始日期 (YYYYMMDD) |
| 88 | end_date: 结束日期 (YYYYMMDD) |
| 89 | categories: arXiv 类别列表 |
| 90 | limit: 最大抓取数量 |
| 91 | |
| 92 | Returns: |
| 93 | 新增论文数量 |
| 94 | """ |
| 95 | conn = init_db() |
| 96 | |
| 97 | # 获取当前论文数量 |
| 98 | before_count = conn.execute("SELECT COUNT(*) FROM papers").fetchone()[0] |
| 99 | print(f"Before: {before_count} papers in database") |
| 100 | |
| 101 | # 抓取论文 |
| 102 | print(f"\nFetching papers from {start_date} to {end_date}...") |
| 103 | print(f"Categories: {categories or 'all'}") |
| 104 | print(f"Limit: {limit}") |
| 105 | |
| 106 | papers = fetch_by_date( |
| 107 | start_date=start_date, |
| 108 | end_date=end_date, |
| 109 | categories=categories, |
| 110 | limit=limit, |
| 111 | ) |
| 112 | |
| 113 | if papers is None: |
| 114 | papers = [] |
| 115 | |
| 116 | print(f"\nFetched {len(papers)} papers from arXiv") |
| 117 | |
| 118 | # 保存到数据库(去重) |
| 119 | new_count = 0 |
| 120 | for paper in papers: |
| 121 | if not paper_exists(conn, paper["arxiv_id"]): |
| 122 | save_paper(conn, paper) |
| 123 | new_count += 1 |
| 124 | print(f" + {paper['arxiv_id']}: {paper['title'][:50]}...") |
| 125 | else: |
| 126 | print(f" ~ {paper['arxiv_id']}: already exists") |
| 127 | |
| 128 | # 统计结果 |
| 129 | after_count = conn.execute("SELECT COUNT(*) FROM papers").fetchone()[0] |
| 130 | print(f"\nAfter: {after_count} papers in database") |
| 131 | print(f"New papers added: {new_count}") |
| 132 | |
| 133 | conn.close() |
| 134 | return new_count |
no test coverage detected