MCPcopy Create free account
hub / github.com/PrathamLearnsToCode/paper2code / main

Function main

skills/paper2code/scripts/fetch_paper.py:339–432  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

337
338
339def main():
340 if len(sys.argv) < 3:
341 print(f"Usage: {sys.argv[0]} <arxiv_id_or_url> <output_dir>", file=sys.stderr)
342 sys.exit(1)
343
344 raw_input = sys.argv[1]
345 output_dir = Path(sys.argv[2])
346 output_dir.mkdir(parents=True, exist_ok=True)
347
348 # Step 1: Normalize ID
349 arxiv_id = normalize_arxiv_id(raw_input)
350 print(f"Arxiv ID: {arxiv_id}")
351
352 # Step 2: Fetch metadata
353 print("\n--- Fetching metadata ---")
354 metadata = fetch_metadata(arxiv_id)
355 metadata_path = output_dir / "paper_metadata.json"
356 with open(metadata_path, "w", encoding="utf-8") as f:
357 json.dump(metadata, f, indent=2, ensure_ascii=False)
358 print(f" Title: {metadata['title']}")
359 print(f" Authors: {', '.join(metadata['authors'][:5])}{'...' if len(metadata['authors']) > 5 else ''}")
360 print(f" Categories: {', '.join(metadata['categories'])}")
361
362 # Step 3: Download and extract PDF
363 paper_text = None
364 pdf_path = output_dir / "paper.pdf"
365
366 print("\n--- Downloading PDF ---")
367 if download_pdf(arxiv_id, pdf_path):
368 # Try pymupdf4llm first
369 print("\n--- Extracting text ---")
370 paper_text = extract_with_pymupdf4llm(pdf_path)
371
372 # Check quality
373 if paper_text and not check_text_quality(paper_text):
374 print(" pymupdf4llm text quality poor, trying pdfplumber...")
375 paper_text = None
376
377 # Fallback to pdfplumber
378 if paper_text is None:
379 paper_text = extract_with_pdfplumber(pdf_path)
380
381 if paper_text and not check_text_quality(paper_text):
382 print(" pdfplumber text quality poor, trying ar5iv HTML...")
383 paper_text = None
384
385 # Step 4: Fallback to ar5iv HTML
386 if paper_text is None:
387 print("\n--- Trying ar5iv HTML fallback ---")
388 paper_text = fetch_ar5iv_html(arxiv_id)
389
390 # Step 5: Save results
391 if paper_text is None:
392 print("\nERROR: All extraction methods failed.", file=sys.stderr)
393 print("Please download the paper manually and provide the text.", file=sys.stderr)
394 sys.exit(1)
395
396 text_path = output_dir / "paper_text.md"

Callers 1

fetch_paper.pyFile · 0.70

Calls 8

normalize_arxiv_idFunction · 0.85
fetch_metadataFunction · 0.85
download_pdfFunction · 0.85
extract_with_pymupdf4llmFunction · 0.85
check_text_qualityFunction · 0.85
extract_with_pdfplumberFunction · 0.85
fetch_ar5iv_htmlFunction · 0.85
find_official_codeFunction · 0.85

Tested by

no test coverage detected