"""Bulk download PDFs from EuropePMC for all open-access articles in the index."""
import csv
import subprocess
import time
from pathlib import Path

base = Path(r'C:\Users\dfduq\OneDrive\Documents\ganglio sergioc')
csv_path = base / 'indice_metadatos' / 'INDICE_MAESTRO.csv'
pdf_dir = base / 'pdfs_originales'
pdf_dir.mkdir(exist_ok=True)

ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

def safe_name(filename):
    return Path(filename).stem.replace('.md', '')

def try_download(pmcid, out_path):
    """Try EuropePMC PDF render URL. Returns size in bytes."""
    url = f"https://europepmc.org/articles/{pmcid}?pdf=render"
    referer = f"https://europepmc.org/article/PMC/{pmcid}"
    try:
        subprocess.run([
            'curl', '-sL', '--tlsv1.2', '--max-time', '90',
            '-A', ua,
            '-e', referer,
            '-o', str(out_path),
            url
        ], check=False, timeout=120, capture_output=True)
        return out_path.stat().st_size if out_path.exists() else 0
    except Exception:
        return 0

def try_fallback(pmcid, out_path):
    """Fallback: try old PMC PDF discovery via NCBI eutils."""
    # Try the standard PMC PDF URL pattern by querying eutils for the PDF filename
    url = f"https://www.ebi.ac.uk/europepmc/webservices/rest/{pmcid}/fullTextXML"
    # Or try EuropePMC's RESTful PDF endpoint
    url2 = f"https://europepmc.org/backend/ptpmcrender.fcgi?accid={pmcid}&blobtype=pdf"
    referer = f"https://europepmc.org/article/PMC/{pmcid}"
    try:
        subprocess.run([
            'curl', '-sL', '--tlsv1.2', '--max-time', '90',
            '-A', ua, '-e', referer,
            '-o', str(out_path),
            url2
        ], check=False, timeout=120, capture_output=True)
        return out_path.stat().st_size if out_path.exists() else 0
    except Exception:
        return 0

# Read CSV
oa_articles = []
with csv_path.open(encoding='utf-8') as f:
    reader = csv.DictReader(f)
    for row in reader:
        if row['pmcid'] and row['pmcid'].startswith('PMC'):
            oa_articles.append(row)

print(f"Open-access articles to download: {len(oa_articles)}")

success, failed, skipped = [], [], []
for i, art in enumerate(oa_articles, 1):
    pmcid = art['pmcid']
    name_base = Path(art['filename']).stem
    out_path = pdf_dir / f"{name_base}.pdf"
    if out_path.exists() and out_path.stat().st_size > 50000:
        skipped.append((pmcid, name_base, out_path.stat().st_size))
        continue
    size = try_download(pmcid, out_path)
    if size < 50000:
        if out_path.exists():
            out_path.unlink()
        size = try_fallback(pmcid, out_path)
        if size < 50000 and out_path.exists():
            out_path.unlink()
    if size >= 50000:
        success.append((pmcid, name_base, size))
        print(f"  [{i}/{len(oa_articles)}] OK  {pmcid:>11} {size:>9}b  {name_base[:50]}")
    else:
        failed.append((pmcid, name_base))
        print(f"  [{i}/{len(oa_articles)}] FAIL {pmcid:>10} ----  {name_base[:50]}")
    time.sleep(0.5)  # polite delay

print()
print(f"Success: {len(success)} | Failed: {len(failed)} | Skipped (already exist): {len(skipped)}")
print()
if failed:
    print("Failed PMCIDs (will need manual download):")
    for pmcid, name in failed:
        print(f"  - {pmcid}  ({name})")
