-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy path0_crawl_paper.py
369 lines (308 loc) · 12.7 KB
/
0_crawl_paper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import arxiv
import json
import os
from pathlib import Path
import time
import logging
from typing import List, Dict
import requests.exceptions
import requests
from datetime import datetime
import asyncio
from typing import Dict, Optional
from collections import Counter
import string
from urllib.parse import urlparse
from utils.openai_utils import GPTClient
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('arxiv_search.log'),
logging.StreamHandler()
]
)
class VenueAPI:
def __init__(self):
self.semantic_scholar_url = "http://api.semanticscholar.org/graph/v1/paper/search"
self.crossref_url = "https://api.crossref.org/works"
self.cache_file = Path('paper_titles/venue_cache.json')
self.cache = self._load_cache()
self.citation_cache = {}
def _load_cache(self) -> Dict:
try:
if self.cache_file.exists():
with open(self.cache_file, 'r') as f:
return json.load(f)
except Exception as e:
logging.warning(f"Error loading cache: {str(e)}")
return {}
def _save_cache(self):
try:
self.cache_file.parent.mkdir(exist_ok=True)
with open(self.cache_file, 'w') as f:
json.dump(self.cache, f, indent=2)
except Exception as e:
logging.warning(f"Error saving cache: {str(e)}")
def get_semantic_scholar_venue(self, title: str) -> str:
try:
params = {
'query': title,
'fields': 'venue,title'
}
response = requests.get(self.semantic_scholar_url, params=params)
if response.status_code == 200:
data = response.json()
if data['data'] and len(data['data']) > 0:
venue = data['data'][0].get('venue', '')
return venue if venue else ''
except Exception as e:
logging.warning(f"Semantic Scholar API error: {str(e)}")
return ''
def get_crossref_venue(self, title: str) -> str:
try:
params = {
'query.title': title,
'select': 'container-title',
'rows': 1
}
headers = {
'User-Agent': 'YourEmailAddress'
}
response = requests.get(self.crossref_url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
if data['message']['items']:
venue = data['message']['items'][0].get('container-title', [''])[0]
return venue if venue else ''
except Exception as e:
logging.warning(f"Crossref API error: {str(e)}")
return ''
def get_venue(self, title: str) -> Dict:
if title in self.cache:
return self.cache[title]
venue_info = {
'venue': '',
'source': '',
'timestamp': datetime.now().isoformat()
}
venue = self.get_semantic_scholar_venue(title)
time.sleep(1)
if venue:
venue_info['venue'] = venue
venue_info['source'] = 'Semantic Scholar'
else:
venue = self.get_crossref_venue(title)
time.sleep(1)
if venue:
venue_info['venue'] = venue
venue_info['source'] = 'Crossref'
else:
venue_info['venue'] = 'arXiv'
venue_info['source'] = 'Default'
self.cache[title] = venue_info
self._save_cache()
return venue_info
def get_citations(self, title: str) -> int:
"""Get citation count from Semantic Scholar API"""
if title in self.citation_cache:
return self.citation_cache[title]
try:
params = {
"query": title,
"fields": "citationCount",
"limit": 1
}
response = requests.get(self.semantic_scholar_url, params=params)
if response.status_code == 200:
data = response.json()
if data["data"]:
citations = data["data"][0]["citationCount"]
self.citation_cache[title] = citations
return citations
except Exception as e:
logging.warning(f"Error fetching citations for {title}: {e}")
return 0
class SearchProgress:
def __init__(self, output_dir: Path):
self.progress_file = output_dir / 'search_progress.json'
self.intermediate_dir = output_dir / 'intermediate'
self.intermediate_dir.mkdir(exist_ok=True)
self.progress = self._load_progress()
def _load_progress(self) -> Dict:
try:
if self.progress_file.exists():
with open(self.progress_file, 'r') as f:
return json.load(f)
except Exception as e:
logging.warning(f"Error loading progress: {str(e)}")
return {}
def save_progress(self):
try:
with open(self.progress_file, 'w') as f:
json.dump(self.progress, f, indent=2)
except Exception as e:
logging.warning(f"Error saving progress: {str(e)}")
def is_keyword_completed(self, file_name: str, keyword: str) -> bool:
return self.progress.get(file_name, {}).get(keyword, False)
def mark_keyword_completed(self, file_name: str, keyword: str):
if file_name not in self.progress:
self.progress[file_name] = {}
self.progress[file_name][keyword] = True
self.save_progress()
def save_intermediate_result(self, file_name: str, keyword: str, papers: List[Dict]):
intermediate_file = self.intermediate_dir / f"{file_name}_{keyword.replace(' ', '_')}.json"
try:
result = {
'keyword': keyword,
'timestamp': datetime.now().isoformat(),
'papers': papers
}
with open(intermediate_file, 'w', encoding='utf-8') as f:
json.dump(result, f, indent=2, ensure_ascii=False)
except Exception as e:
logging.error(f"Error saving intermediate result: {str(e)}")
async def search_arxiv(keyword: str, venue_api: VenueAPI, exact_match: bool = True, max_results: int = 100) -> List[Dict]:
try:
search = arxiv.Search(
query=keyword,
max_results=max_results,
)
papers = []
for result in search.results():
should_include = (
result.title.lower().strip() == keyword.lower().strip()
if exact_match
else True
)
if should_include:
paper_info = {
'title': result.title,
'authors': [author.name for author in result.authors],
'year': result.published.year,
'url': result.entry_id,
'abstract': result.summary
}
venue_info = venue_api.get_venue(result.title)
citations = venue_api.get_citations(result.title)
paper_info.update({
'venue': venue_info['venue'],
'venue_source': venue_info['source'],
'venue_lookup_time': venue_info['timestamp'],
'citations': citations
})
papers.append(paper_info)
if exact_match:
break
elif len(papers) == 1: # If not exact match, just get the first result
break
return papers
except Exception as e:
logging.error(f"Error while searching for '{keyword}': {str(e)}")
return []
async def process_input_file(file_path: Path, venue_api: VenueAPI, progress: SearchProgress, exact_match: bool = True) -> List[Dict]:
results = [] # Changed from dict to list
not_found = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
keywords = [line.strip() for line in f if line.strip()]
for keyword in keywords:
if progress.is_keyword_completed('initial', keyword):
logging.info(f"Skipping already processed paper: {keyword}")
continue
logging.info(f"Searching for paper: {keyword}")
papers = await search_arxiv(keyword, venue_api, exact_match)
if papers:
results.extend(papers)
progress.save_intermediate_result('initial', keyword, papers)
match_type = "exact" if exact_match else "relevant"
logging.info(f"Found {match_type} match for paper: {keyword}")
else:
logging.warning(f"No match found for paper: {keyword}")
not_found.append(keyword)
progress.mark_keyword_completed('initial', keyword)
await asyncio.sleep(3)
if not_found:
with open('papers_not_found.md', 'w', encoding='utf-8') as f:
for title in not_found:
f.write(f"{title}\n")
return results
except Exception as e:
logging.error(f"Error processing file {file_path}: {str(e)}")
return []
def clean_title(title: str) -> str:
"""Replace all punctuation with spaces and clean up multiple spaces"""
translator = str.maketrans(string.punctuation, ' ' * len(string.punctuation))
cleaned = title.translate(translator)
cleaned = ' '.join(cleaned.split())
return cleaned
def download_pdf(url: str, output_path: str) -> bool:
"""Download PDF from URL with error handling"""
try:
if 'arxiv.org' in url and not url.endswith('.pdf'):
parsed = urlparse(url)
paper_id = parsed.path.split('/')[-1]
url = f"http://arxiv.org/pdf/{paper_id}.pdf"
response = requests.get(url, stream=True)
response.raise_for_status()
if 'application/pdf' not in response.headers.get('content-type', '').lower():
raise ValueError("URL does not point to a PDF file")
with open(output_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return True
except Exception as e:
logging.error(f"Error downloading {url}: {str(e)}")
return False
async def download_papers(papers: List[Dict], output_dir: Path):
"""Download PDFs for all papers"""
pdf_dir = output_dir / 'pdfs'
pdf_dir.mkdir(exist_ok=True)
logging.info(f"Starting download of {len(papers)} papers")
for i, paper in enumerate(papers, 1):
title = paper['title']
url = paper['url']
clean_name = clean_title(title)
pdf_path = pdf_dir / f"{clean_name}.pdf"
if pdf_path.exists():
logging.info(f"[{i}/{len(papers)}] Already exists: {clean_name}")
continue
logging.info(f"[{i}/{len(papers)}] Downloading: {clean_name}")
if download_pdf(url, str(pdf_path)):
logging.info(f"Successfully downloaded: {clean_name}")
else:
logging.error(f"Failed to download: {clean_name}")
await asyncio.sleep(3)
async def main():
output_dir = Path('paper_titles')
output_dir.mkdir(exist_ok=True)
venue_api = VenueAPI()
progress = SearchProgress(output_dir)
file_path = Path('papers_to_search')
if not file_path.exists():
logging.error(f"Input file '{file_path}' does not exist")
return
logging.info(f"Processing file: {file_path}")
results = await process_input_file(file_path, venue_api, progress, exact_match=True)
if results:
# Save search results
output_file = output_dir / "paper_titles.json"
try:
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=2, ensure_ascii=False)
logging.info(f"Results saved to {output_file}")
# Download papers
await download_papers(results, output_dir)
except Exception as e:
logging.error(f"Error saving results to {output_file}: {str(e)}")
else:
logging.warning("No results to save")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
logging.info("Process interrupted by user")
except Exception as e:
logging.error(f"Unexpected error: {str(e)}")