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
+386
@@ -0,0 +1,386 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Scribd PDF Downloader — desktop GUI."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import queue
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import tkinter as tk
|
||||
from pathlib import Path
|
||||
from tkinter import filedialog, messagebox, ttk
|
||||
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
from scribd_downloader.auth import session_exists
|
||||
from scribd_downloader.document import download_document
|
||||
from scribd_downloader.preview import load_document_preview
|
||||
from scribd_downloader.url import ContentType, parse_scribd_url
|
||||
|
||||
APP_TITLE = "Scribd PDF Downloader"
|
||||
PREVIEW_MAX_WIDTH = 720
|
||||
|
||||
|
||||
class ScribdDownloaderApp(tk.Tk):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.title(APP_TITLE)
|
||||
self.minsize(900, 700)
|
||||
self.geometry("1000x780")
|
||||
|
||||
self._queue: queue.Queue = queue.Queue()
|
||||
self._photo: ImageTk.PhotoImage | None = None
|
||||
self._last_pdf: Path | None = None
|
||||
self._doc_total_pages = 0
|
||||
self._busy = False
|
||||
|
||||
self._build_ui()
|
||||
self._apply_style()
|
||||
self.after(100, self._poll_queue)
|
||||
|
||||
def _apply_style(self) -> None:
|
||||
style = ttk.Style(self)
|
||||
if sys.platform == "win32":
|
||||
style.theme_use("vista")
|
||||
style.configure("Title.TLabel", font=("Segoe UI", 16, "bold"))
|
||||
style.configure("Status.TLabel", font=("Segoe UI", 10))
|
||||
style.configure("Big.TButton", padding=(12, 8))
|
||||
|
||||
def _build_ui(self) -> None:
|
||||
root = ttk.Frame(self, padding=16)
|
||||
root.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
ttk.Label(root, text=APP_TITLE, style="Title.TLabel").pack(anchor=tk.W)
|
||||
ttk.Label(
|
||||
root,
|
||||
text="Paste a Scribd document URL, preview it, then download as PDF.",
|
||||
style="Status.TLabel",
|
||||
).pack(anchor=tk.W, pady=(4, 12))
|
||||
|
||||
url_frame = ttk.LabelFrame(root, text="Scribd URL", padding=10)
|
||||
url_frame.pack(fill=tk.X, pady=(0, 10))
|
||||
|
||||
self.url_var = tk.StringVar()
|
||||
url_entry = ttk.Entry(url_frame, textvariable=self.url_var, font=("Segoe UI", 11))
|
||||
url_entry.pack(fill=tk.X, side=tk.LEFT, expand=True, padx=(0, 8))
|
||||
url_entry.bind("<Return>", lambda _e: self._start_preview())
|
||||
|
||||
ttk.Button(url_frame, text="Paste", command=self._paste_url).pack(side=tk.RIGHT)
|
||||
|
||||
opts = ttk.Frame(root)
|
||||
opts.pack(fill=tk.X, pady=(0, 10))
|
||||
|
||||
ttk.Label(opts, text="Pages:").grid(row=0, column=0, sticky=tk.W, padx=(0, 6))
|
||||
self.pages_var = tk.StringVar(value="all")
|
||||
ttk.Entry(opts, textvariable=self.pages_var, width=12).grid(row=0, column=1, sticky=tk.W)
|
||||
|
||||
ttk.Label(opts, text="Delay (s):").grid(row=0, column=2, sticky=tk.W, padx=(16, 6))
|
||||
self.delay_var = tk.DoubleVar(value=2.0)
|
||||
ttk.Spinbox(
|
||||
opts, from_=0.5, to=10.0, increment=0.5, width=6, textvariable=self.delay_var
|
||||
).grid(row=0, column=3, sticky=tk.W)
|
||||
|
||||
ttk.Label(opts, text="Quality:").grid(row=0, column=4, sticky=tk.W, padx=(16, 6))
|
||||
self.scale_var = tk.IntVar(value=2)
|
||||
ttk.Combobox(
|
||||
opts,
|
||||
textvariable=self.scale_var,
|
||||
values=[1, 2, 3],
|
||||
width=4,
|
||||
state="readonly",
|
||||
).grid(row=0, column=5, sticky=tk.W)
|
||||
|
||||
ttk.Label(opts, text="Preview page:").grid(row=0, column=6, sticky=tk.W, padx=(16, 6))
|
||||
self.preview_page_var = tk.IntVar(value=1)
|
||||
self.preview_spin = ttk.Spinbox(
|
||||
opts,
|
||||
from_=1,
|
||||
to=1,
|
||||
width=5,
|
||||
textvariable=self.preview_page_var,
|
||||
command=self._on_preview_page_change,
|
||||
)
|
||||
self.preview_spin.grid(row=0, column=7, sticky=tk.W)
|
||||
|
||||
btn_row = ttk.Frame(root)
|
||||
btn_row.pack(fill=tk.X, pady=(0, 10))
|
||||
|
||||
self.preview_btn = ttk.Button(
|
||||
btn_row, text="Load Preview", style="Big.TButton", command=self._start_preview
|
||||
)
|
||||
self.preview_btn.pack(side=tk.LEFT, padx=(0, 8))
|
||||
|
||||
self.download_btn = ttk.Button(
|
||||
btn_row, text="Download PDF", style="Big.TButton", command=self._start_download
|
||||
)
|
||||
self.download_btn.pack(side=tk.LEFT, padx=(0, 8))
|
||||
|
||||
self.open_btn = ttk.Button(
|
||||
btn_row, text="Open PDF", command=self._open_pdf, state=tk.DISABLED
|
||||
)
|
||||
self.open_btn.pack(side=tk.LEFT, padx=(0, 8))
|
||||
|
||||
self.folder_btn = ttk.Button(
|
||||
btn_row, text="Open Folder", command=self._open_folder, state=tk.DISABLED
|
||||
)
|
||||
self.folder_btn.pack(side=tk.LEFT)
|
||||
|
||||
meta = ttk.Frame(root)
|
||||
meta.pack(fill=tk.X, pady=(0, 8))
|
||||
self.title_var = tk.StringVar(value="Title: —")
|
||||
self.pages_info_var = tk.StringVar(value="Pages: —")
|
||||
ttk.Label(meta, textvariable=self.title_var, font=("Segoe UI", 11, "bold")).pack(
|
||||
anchor=tk.W
|
||||
)
|
||||
ttk.Label(meta, textvariable=self.pages_info_var, style="Status.TLabel").pack(
|
||||
anchor=tk.W
|
||||
)
|
||||
|
||||
preview_frame = ttk.LabelFrame(root, text="Preview", padding=8)
|
||||
preview_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
|
||||
|
||||
self.canvas = tk.Canvas(preview_frame, bg="#2b2b2b", highlightthickness=0)
|
||||
scroll_y = ttk.Scrollbar(preview_frame, orient=tk.VERTICAL, command=self.canvas.yview)
|
||||
self.canvas.configure(yscrollcommand=scroll_y.set)
|
||||
scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
|
||||
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
|
||||
|
||||
self.preview_label = ttk.Label(
|
||||
self.canvas,
|
||||
text="Enter a URL and click Load Preview",
|
||||
anchor=tk.CENTER,
|
||||
background="#2b2b2b",
|
||||
foreground="#cccccc",
|
||||
)
|
||||
self.canvas_window = self.canvas.create_window(
|
||||
0, 0, window=self.preview_label, anchor=tk.NW
|
||||
)
|
||||
self.canvas.bind("<Configure>", self._on_canvas_resize)
|
||||
|
||||
prog = ttk.Frame(root)
|
||||
prog.pack(fill=tk.X)
|
||||
self.progress = ttk.Progressbar(prog, mode="determinate", maximum=100)
|
||||
self.progress.pack(fill=tk.X, pady=(0, 4))
|
||||
self.status_var = tk.StringVar(value="Ready")
|
||||
ttk.Label(prog, textvariable=self.status_var, style="Status.TLabel").pack(anchor=tk.W)
|
||||
|
||||
def _on_canvas_resize(self, event: tk.Event) -> None:
|
||||
self.canvas.itemconfig(self.canvas_window, width=event.width)
|
||||
if self._photo:
|
||||
self._center_preview()
|
||||
|
||||
def _center_preview(self) -> None:
|
||||
self.canvas.update_idletasks()
|
||||
cw = self.canvas.winfo_width()
|
||||
ch = self.canvas.winfo_height()
|
||||
if self._photo:
|
||||
x = max(0, (cw - self._photo.width()) // 2)
|
||||
y = max(0, (ch - self._photo.height()) // 2)
|
||||
self.canvas.coords(self.canvas_window, x, y)
|
||||
|
||||
def _paste_url(self) -> None:
|
||||
try:
|
||||
text = self.clipboard_get().strip()
|
||||
if text:
|
||||
self.url_var.set(text)
|
||||
except tk.TclError:
|
||||
pass
|
||||
|
||||
def _parse_url(self) -> object:
|
||||
url = self.url_var.get().strip()
|
||||
if not url:
|
||||
raise ValueError("Please enter a Scribd URL.")
|
||||
return parse_scribd_url(url)
|
||||
|
||||
def _set_busy(self, busy: bool) -> None:
|
||||
self._busy = busy
|
||||
state = tk.DISABLED if busy else tk.NORMAL
|
||||
self.preview_btn.configure(state=state)
|
||||
self.download_btn.configure(state=state)
|
||||
|
||||
def _start_preview(self) -> None:
|
||||
if self._busy:
|
||||
return
|
||||
try:
|
||||
target = self._parse_url()
|
||||
except ValueError as exc:
|
||||
messagebox.showerror(APP_TITLE, str(exc))
|
||||
return
|
||||
|
||||
if target.content_type == ContentType.BOOK:
|
||||
messagebox.showinfo(
|
||||
APP_TITLE,
|
||||
"Ebooks need the command-line login.\n"
|
||||
"Run: .venv\\Scripts\\python.exe main.py login",
|
||||
)
|
||||
return
|
||||
|
||||
page_num = max(1, self.preview_page_var.get())
|
||||
self._set_busy(True)
|
||||
self.status_var.set("Loading preview…")
|
||||
self.progress.configure(mode="indeterminate")
|
||||
self.progress.start(12)
|
||||
|
||||
def worker() -> None:
|
||||
try:
|
||||
info = load_document_preview(
|
||||
target,
|
||||
page_index=page_num - 1,
|
||||
scale=self.scale_var.get(),
|
||||
use_session=session_exists(),
|
||||
)
|
||||
self._queue.put(
|
||||
("preview", info.title, info.total_pages, str(info.preview_path))
|
||||
)
|
||||
except Exception as exc:
|
||||
self._queue.put(("error", str(exc)))
|
||||
|
||||
threading.Thread(target=worker, daemon=True).start()
|
||||
|
||||
def _on_preview_page_change(self) -> None:
|
||||
if self._doc_total_pages > 0 and not self._busy:
|
||||
self._start_preview()
|
||||
|
||||
def _start_download(self) -> None:
|
||||
if self._busy:
|
||||
return
|
||||
try:
|
||||
target = self._parse_url()
|
||||
except ValueError as exc:
|
||||
messagebox.showerror(APP_TITLE, str(exc))
|
||||
return
|
||||
|
||||
if target.content_type == ContentType.BOOK:
|
||||
messagebox.showinfo(
|
||||
APP_TITLE,
|
||||
"Ebooks need the command-line login.\n"
|
||||
"Run: .venv\\Scripts\\python.exe main.py login",
|
||||
)
|
||||
return
|
||||
|
||||
self._set_busy(True)
|
||||
self.open_btn.configure(state=tk.DISABLED)
|
||||
self.folder_btn.configure(state=tk.DISABLED)
|
||||
self.progress.stop()
|
||||
self.progress.configure(mode="determinate", value=0)
|
||||
self.status_var.set("Starting download…")
|
||||
|
||||
pages = self.pages_var.get().strip() or "all"
|
||||
delay = float(self.delay_var.get())
|
||||
scale = int(self.scale_var.get())
|
||||
|
||||
def on_progress(done: int, total: int, message: str) -> None:
|
||||
self._queue.put(("progress", done, total, message))
|
||||
|
||||
def on_page(path: Path, page_no: int, total: int) -> None:
|
||||
self._queue.put(("page", str(path), page_no, total))
|
||||
|
||||
def worker() -> None:
|
||||
try:
|
||||
pdf_path = download_document(
|
||||
target,
|
||||
pages=pages,
|
||||
delay=delay,
|
||||
scale=scale,
|
||||
use_session=session_exists(),
|
||||
on_progress=on_progress,
|
||||
on_page=on_page,
|
||||
)
|
||||
self._queue.put(("done", str(pdf_path)))
|
||||
except Exception as exc:
|
||||
self._queue.put(("error", str(exc)))
|
||||
|
||||
threading.Thread(target=worker, daemon=True).start()
|
||||
|
||||
def _show_preview_image(self, path: str) -> None:
|
||||
img = Image.open(path)
|
||||
if img.width > PREVIEW_MAX_WIDTH:
|
||||
ratio = PREVIEW_MAX_WIDTH / img.width
|
||||
img = img.resize(
|
||||
(PREVIEW_MAX_WIDTH, int(img.height * ratio)), Image.Resampling.LANCZOS
|
||||
)
|
||||
self._photo = ImageTk.PhotoImage(img)
|
||||
self.preview_label.configure(image=self._photo, text="")
|
||||
self._center_preview()
|
||||
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
|
||||
|
||||
def _poll_queue(self) -> None:
|
||||
try:
|
||||
while True:
|
||||
msg = self._queue.get_nowait()
|
||||
self._handle_message(msg)
|
||||
except queue.Empty:
|
||||
pass
|
||||
self.after(100, self._poll_queue)
|
||||
|
||||
def _handle_message(self, msg: tuple) -> None:
|
||||
kind = msg[0]
|
||||
|
||||
if kind == "preview":
|
||||
_, title, total, path = msg
|
||||
self._doc_total_pages = total
|
||||
self.preview_spin.configure(to=max(1, total))
|
||||
self.title_var.set(f"Title: {title}")
|
||||
self.pages_info_var.set(f"Pages: {total}")
|
||||
self._show_preview_image(path)
|
||||
self.status_var.set(f"Preview loaded (page {self.preview_page_var.get()} of {total})")
|
||||
self._finish_busy()
|
||||
|
||||
elif kind == "progress":
|
||||
_, done, total, message = msg
|
||||
if total > 0:
|
||||
pct = min(100, int(100 * done / total))
|
||||
self.progress.configure(value=pct)
|
||||
self.status_var.set(message)
|
||||
|
||||
elif kind == "page":
|
||||
_, path, page_no, total = msg
|
||||
self._show_preview_image(path)
|
||||
self.pages_info_var.set(f"Pages: {total} — capturing {page_no}/{total}")
|
||||
if total > 0:
|
||||
self.progress.configure(value=min(100, int(100 * page_no / total)))
|
||||
|
||||
elif kind == "done":
|
||||
_, pdf_path = msg
|
||||
self._last_pdf = Path(pdf_path)
|
||||
self.progress.configure(value=100)
|
||||
self.status_var.set(f"Saved: {self._last_pdf.name}")
|
||||
self.open_btn.configure(state=tk.NORMAL)
|
||||
self.folder_btn.configure(state=tk.NORMAL)
|
||||
self._finish_busy()
|
||||
messagebox.showinfo(APP_TITLE, f"Download complete:\n{self._last_pdf}")
|
||||
|
||||
elif kind == "error":
|
||||
_, text = msg
|
||||
self.status_var.set(f"Error: {text}")
|
||||
self._finish_busy()
|
||||
messagebox.showerror(APP_TITLE, text)
|
||||
|
||||
def _finish_busy(self) -> None:
|
||||
self.progress.stop()
|
||||
self.progress.configure(mode="determinate")
|
||||
self._set_busy(False)
|
||||
|
||||
def _open_pdf(self) -> None:
|
||||
if self._last_pdf and self._last_pdf.is_file():
|
||||
os.startfile(self._last_pdf)
|
||||
|
||||
def _open_folder(self) -> None:
|
||||
if self._last_pdf and self._last_pdf.is_file():
|
||||
subprocess.run(
|
||||
["explorer", "/select,", str(self._last_pdf.resolve())],
|
||||
check=False,
|
||||
)
|
||||
else:
|
||||
os.startfile(Path.cwd())
|
||||
|
||||
|
||||
def main() -> None:
|
||||
app = ScribdDownloaderApp()
|
||||
app.mainloop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user