Add Scribd PDF Downloader under Scribd/
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
5d2933ea95
28 files changed
+2159
No files matched your search
@@ -0,0 +1,80 @@
|
||||
"""Load Scribd documents and capture preview screenshots."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from playwright.sync_api import Page, sync_playwright
|
||||
|
||||
from scribd_downloader.auth import open_browser
|
||||
from scribd_downloader.document import TEMP_DIR, _block_noise, _document_title
|
||||
from scribd_downloader.url import ScribdTarget
|
||||
|
||||
PREVIEW_DIR = Path("temp_preview")
|
||||
|
||||
|
||||
@dataclass
|
||||
class DocumentInfo:
|
||||
title: str
|
||||
total_pages: int
|
||||
preview_path: Path
|
||||
|
||||
|
||||
def load_document_preview(
|
||||
target: ScribdTarget,
|
||||
*,
|
||||
page_index: int = 0,
|
||||
scale: int = 2,
|
||||
use_session: bool = True,
|
||||
) -> DocumentInfo:
|
||||
"""Open the embed viewer and screenshot one page for preview."""
|
||||
PREVIEW_DIR.mkdir(exist_ok=True)
|
||||
page_index = max(0, page_index)
|
||||
|
||||
with sync_playwright() as playwright:
|
||||
browser, context = open_browser(
|
||||
playwright,
|
||||
headless=True,
|
||||
use_session=use_session,
|
||||
device_scale_factor=float(scale),
|
||||
)
|
||||
_block_noise(context)
|
||||
page = context.new_page()
|
||||
title, total, preview_path = _load_page(
|
||||
page, target, page_index=page_index
|
||||
)
|
||||
context.close()
|
||||
browser.close()
|
||||
|
||||
return DocumentInfo(title=title, total_pages=total, preview_path=preview_path)
|
||||
|
||||
|
||||
def _load_page(
|
||||
page: Page,
|
||||
target: ScribdTarget,
|
||||
*,
|
||||
page_index: int,
|
||||
) -> tuple[str, int, Path]:
|
||||
page.goto(target.embed_url, wait_until="domcontentloaded", timeout=90_000)
|
||||
try:
|
||||
page.wait_for_selector(".outer_page", timeout=20_000)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
total = page.locator(".outer_page").count()
|
||||
if total == 0:
|
||||
raise RuntimeError(
|
||||
"No pages found. The document may be private or unavailable."
|
||||
)
|
||||
|
||||
title = _document_title(page, target)
|
||||
index = min(page_index, total - 1)
|
||||
element = page.locator(".outer_page").nth(index)
|
||||
element.scroll_into_view_if_needed()
|
||||
time.sleep(0.5)
|
||||
|
||||
preview_path = PREVIEW_DIR / f"preview_{index + 1}.png"
|
||||
element.screenshot(path=str(preview_path))
|
||||
return title, total, preview_path
|
||||
Reference in new issue
Block a user