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,102 @@
|
||||
"""Parse Scribd URLs and build viewer URLs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from urllib.parse import unquote
|
||||
|
||||
|
||||
class ContentType(str, Enum):
|
||||
DOCUMENT = "document"
|
||||
BOOK = "book"
|
||||
UNKNOWN = "unknown"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ScribdTarget:
|
||||
content_id: str
|
||||
content_type: ContentType
|
||||
original_url: str
|
||||
title_slug: str | None = None
|
||||
|
||||
@property
|
||||
def embed_url(self) -> str:
|
||||
return (
|
||||
f"https://www.scribd.com/embeds/{self.content_id}/content"
|
||||
f"?start_page=1&view_mode=scroll"
|
||||
)
|
||||
|
||||
@property
|
||||
def read_url(self) -> str | None:
|
||||
if self.content_type != ContentType.BOOK:
|
||||
return None
|
||||
return self.original_url.replace("/book/", "/read/").replace(
|
||||
"scribd.com/book", "scribd.com/read"
|
||||
)
|
||||
|
||||
|
||||
_ID_PATTERN = re.compile(
|
||||
r"/(?:document|presentation|doc|book|article|listen)/(\d+)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
def parse_scribd_url(url: str) -> ScribdTarget:
|
||||
"""Extract document/book id and type from a Scribd URL."""
|
||||
url = url.strip()
|
||||
if url.isdigit():
|
||||
return ScribdTarget(
|
||||
content_id=url,
|
||||
content_type=ContentType.DOCUMENT,
|
||||
original_url=f"https://www.scribd.com/document/{url}",
|
||||
)
|
||||
|
||||
match = _ID_PATTERN.search(url)
|
||||
if not match:
|
||||
raise ValueError(
|
||||
"Invalid Scribd URL. Expected formats like:\n"
|
||||
" https://www.scribd.com/document/123456/Title\n"
|
||||
" https://www.scribd.com/book/123456/Title"
|
||||
)
|
||||
|
||||
content_id = match.group(1)
|
||||
path_lower = url.lower()
|
||||
if "/book/" in path_lower:
|
||||
content_type = ContentType.BOOK
|
||||
elif any(
|
||||
segment in path_lower
|
||||
for segment in ("/document/", "/doc/", "/presentation/", "/article/")
|
||||
):
|
||||
content_type = ContentType.DOCUMENT
|
||||
else:
|
||||
content_type = ContentType.DOCUMENT
|
||||
|
||||
slug = _title_slug_from_url(url)
|
||||
return ScribdTarget(
|
||||
content_id=content_id,
|
||||
content_type=content_type,
|
||||
original_url=url.split("#")[0].split("?")[0],
|
||||
title_slug=slug,
|
||||
)
|
||||
|
||||
|
||||
def _title_slug_from_url(url: str) -> str | None:
|
||||
clean = url.split("#")[0].split("?")[0].rstrip("/")
|
||||
parts = clean.split("/")
|
||||
if len(parts) < 2:
|
||||
return None
|
||||
last = parts[-1]
|
||||
if last.isdigit():
|
||||
return None
|
||||
return unquote(last).replace("-", " ").replace("_", " ")
|
||||
|
||||
|
||||
def sanitize_filename(name: str, max_length: int = 120) -> str:
|
||||
cleaned = "".join(c for c in name if c.isalnum() or c in (" ", "-", "_")).strip()
|
||||
if not cleaned:
|
||||
cleaned = "scribd_download"
|
||||
if len(cleaned) > max_length:
|
||||
cleaned = cleaned[:max_length].rstrip()
|
||||
return cleaned
|
||||
Reference in new issue
Block a user