LDownloader/ui/app.py
2026-10-27 20:55:29 +03:00

615 lines
25 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
import ctypes
import threading
import tkinter as tk
from tkinter import messagebox
from ui.theme import (
BG, SURFACE, SURFACE_LIGHT, ENTRY_BG, BORDER,
ACCENT, ACCENT_LIGHT, ACCENT_MUTED, TEXT, SUBTEXT, MUTED, SUCCESS, WARNING, ERROR,
TAB_BG,
FONT_HERO, FONT_TITLE, FONT_SUBTITLE, FONT_REGULAR, FONT_BOLD, FONT_SMALL, FONT_SMALL_BOLD,
apply_theme
)
from ui.components import TabButton, PillButton, ContextualDownloadDrawer
from ui.tabs.single_tab import SingleTab
from ui.tabs.playlist_tab import PlaylistTab
from ui.tabs.batch_tab import BatchTab
from ui.tabs.history_tab import HistoryTab
from ui.tabs.settings_tab import SettingsTab
from core.config import config, get_app_dir
from core.downloader import engine, build_command
from core.history import history
from core.dependencies import get_ytdlp_status, get_ffmpeg_status, get_js_status
class LDownloaderApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("LDownloader v2.0")
self.geometry("1080x800")
self.minsize(900, 680)
self.configure(bg=BG)
# Windows AppUserModelID
if sys.platform == "win32":
try:
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("ldownloader.modern.gui.2")
except Exception:
pass
# Ekranı ortala
self.update_idletasks()
sw = self.winfo_screenwidth()
sh = self.winfo_screenheight()
width = min(1080, sw - 80)
height = min(800, sh - 100)
x = max(0, (sw - width) // 2)
y = max(0, (sh - height) // 2)
self.geometry(f"{width}x{height}+{x}+{y}")
# İkon ve Logo
self._load_icons()
# Tema ve stiller
apply_theme(self, config.get("theme", "dark"))
# Durum değişkenleri
self.active_tab_name = "single"
self.current_download_file = None
self._batch_running = False
# Ana Düzen
self._build_layout()
self.protocol("WM_DELETE_WINDOW", self._on_close)
# Sistem durumunu arka planda kontrol et
self._update_system_status()
def _load_icons(self):
app_dir = get_app_dir()
ico_path = os.path.join(app_dir, "logo.ico")
if os.path.isfile(ico_path):
try:
self.iconbitmap(ico_path)
except Exception:
pass
self.logo_img = None
self.window_icon_img = None
png_path = os.path.join(app_dir, "logo.png")
if os.path.isfile(png_path):
try:
raw = tk.PhotoImage(file=png_path)
self.window_icon_img = raw
self.iconphoto(True, raw)
factor = max(1, raw.width() // 28)
self.logo_img = raw.subsample(factor, factor)
except Exception:
pass
def _build_layout(self):
# ── Üst Başlık Çubuğu (Top Bar) ─────────────────────────────
top_bar = tk.Frame(self, bg=TAB_BG, padx=24, pady=14)
top_bar.pack(fill="x")
# Sol: Logo + Başlık
brand_frame = tk.Frame(top_bar, bg=TAB_BG)
brand_frame.pack(side="left")
if self.logo_img:
tk.Label(brand_frame, image=self.logo_img, bg=TAB_BG).pack(side="left", padx=(0, 8))
tk.Label(brand_frame, text="LDownloader", font=FONT_TITLE, bg=TAB_BG, fg=TEXT).pack(side="left")
badge = tk.Label(
brand_frame, text="2.0", font=FONT_SMALL_BOLD, bg=ACCENT_MUTED, fg=ACCENT_LIGHT,
padx=8, pady=3
)
badge.pack(side="left", padx=(10, 0))
# Sağ: Sistem durumu
self.lbl_sys_status = tk.Label(
top_bar, text="● Sistem kontrol ediliyor", bg=ACCENT_MUTED, fg=ACCENT_LIGHT,
font=FONT_SMALL_BOLD, anchor="e", padx=12, pady=6
)
self.lbl_sys_status.pack(side="right")
self.btn_theme = PillButton(
top_bar,
text="Açık tema" if config.get("theme", "dark") == "dark" else "Koyu tema",
command=self._toggle_theme,
small=True,
style="secondary",
)
self.btn_theme.pack(side="right", padx=(0, 10))
# ── Tab Navigasyon Çubuğu ────────────────────────────────────
tab_bar = tk.Frame(self, bg=TAB_BG)
tab_bar.pack(fill="x")
# Ince ayraç
tk.Frame(tab_bar, bg=BORDER, height=1).pack(fill="x", side="top")
tab_inner = tk.Frame(tab_bar, bg=TAB_BG, padx=20, pady=8)
tab_inner.pack(fill="x")
self.nav_buttons = {}
nav_items = [
("single", "▶", "Tekli indirme"),
("playlist", "≡", "Oynatma listesi"),
("batch", "+", "Toplu indirme"),
("history", "↺", "Geçmiş"),
("settings", "⚙", "Ayarlar")
]
for key, icon, label in nav_items:
btn = TabButton(
tab_inner,
icon=icon,
text=label,
command=lambda k=key: self.switch_tab(k),
is_active=(key == "single")
)
btn.pack(side="left", padx=(0, 2))
self.nav_buttons[key] = btn
# Alttaki ince ayraç
tk.Frame(self, bg=BORDER, height=1).pack(fill="x")
# ── Ana İçerik Alanı ─────────────────────────────────────────
self.main_area = tk.Frame(self, bg=BG)
self.main_area.pack(fill="both", expand=True)
# İçerik Kapsayıcısı (Scrollable alan)
self.content_container = tk.Frame(self.main_area, bg=BG)
self.content_container.pack(fill="both", expand=True)
# Alt İndirme Çekmecesi
self.drawer = ContextualDownloadDrawer(
self.main_area,
on_cancel=self._on_cancel_download,
on_open_file=self._open_downloaded_file
)
# Sekmeleri Başlat
self.tabs = {
"single": SingleTab(self.content_container, on_download_requested=self.start_single_download),
"playlist": PlaylistTab(self.content_container, on_download_requested=self.start_playlist_download),
"batch": BatchTab(self.content_container, on_batch_requested=self.start_batch_download),
"history": HistoryTab(self.content_container),
"settings": SettingsTab(self.content_container, on_dependency_updated=self._on_deps_updated)
}
self.switch_tab("single")
def _capture_layout_state(self):
state = {"active_tab": self.active_tab_name}
single = self.tabs.get("single")
if single:
state["single"] = {
"url": single.url_entry.get_clean_text(),
"dir": single.dir_var.get(),
"media": single.media_seg.get(),
"video_format": single.video_ext_var.get(),
"video_quality": single.res_var.get(),
"audio_format": single.audio_ext_var.get(),
"audio_quality": single.audio_qual_var.get(),
"subtitles": single.chip_sub.get(),
"thumbnail": single.chip_thumb.get(),
"turbo": single.chip_turbo.get(),
"metadata": single.current_metadata,
}
playlist = self.tabs.get("playlist")
if playlist:
state["playlist"] = {
"url": playlist.url_entry.get_clean_text(),
"dir": playlist.dir_var.get(),
"media": playlist.media_seg.get(),
"video_quality": playlist.res_var.get(),
"audio_quality": playlist.audio_qual_var.get(),
"start": playlist.start_var.get(),
"end": playlist.end_var.get(),
"ignore_errors": playlist.chip_ignore.get(),
"turbo": playlist.chip_turbo.get(),
}
batch = self.tabs.get("batch")
if batch:
state["batch"] = {
"input": batch.text_input.get("1.0", "end-1c"),
"media": batch.media_seg.get(),
"queue": [dict(item) for item in batch.queue],
}
history_tab = self.tabs.get("history")
if history_tab:
state["history_search"] = history_tab.search_var.get()
state["drawer"] = {
"visible": bool(self.drawer.winfo_manager()),
"title": self.drawer.lbl_title.cget("text"),
"progress": self.drawer.pbar["value"],
"logs": self.drawer.log_text.get("1.0", "end-1c"),
"console_open": self.drawer.console_open,
"completed": bool(self.drawer.btn_open.winfo_manager()),
"cancelled": self.drawer.lbl_title.cget("text").startswith("⛔"),
"failed": self.drawer.lbl_title.cget("text").startswith("❌"),
}
return state
def _restore_layout_state(self, state):
single_state = state.get("single", {})
single = self.tabs["single"]
single.dir_var.set(single_state.get("dir", single.dir_var.get()))
single.media_seg.select(single_state.get("media", "video"))
single.video_ext_var.set(single_state.get("video_format", single.video_ext_var.get()))
single.res_var.set(single_state.get("video_quality", single.res_var.get()))
single.audio_ext_var.set(single_state.get("audio_format", single.audio_ext_var.get()))
single.audio_qual_var.set(single_state.get("audio_quality", single.audio_qual_var.get()))
single.chip_sub.set(single_state.get("subtitles", single.chip_sub.get()))
single.chip_thumb.set(single_state.get("thumbnail", single.chip_thumb.get()))
single.chip_turbo.set(single_state.get("turbo", single.chip_turbo.get()))
single.url_var.set(single_state.get("url", ""))
if single_state.get("url"):
single.url_entry.has_placeholder = False
single.url_entry.config(fg=TEXT)
single.current_metadata = None
playlist_state = state.get("playlist", {})
playlist = self.tabs["playlist"]
playlist.dir_var.set(playlist_state.get("dir", playlist.dir_var.get()))
playlist.media_seg.select(playlist_state.get("media", "video"))
playlist.res_var.set(playlist_state.get("video_quality", playlist.res_var.get()))
playlist.audio_qual_var.set(playlist_state.get("audio_quality", playlist.audio_qual_var.get()))
playlist.start_var.set(playlist_state.get("start", ""))
playlist.end_var.set(playlist_state.get("end", ""))
playlist.chip_ignore.set(playlist_state.get("ignore_errors", True))
playlist.chip_turbo.set(playlist_state.get("turbo", True))
playlist.url_var.set(playlist_state.get("url", ""))
if playlist_state.get("url"):
playlist.url_entry.has_placeholder = False
playlist.url_entry.config(fg=TEXT)
batch_state = state.get("batch", {})
batch = self.tabs["batch"]
batch.media_seg.select(batch_state.get("media", "video"))
batch.text_input.insert("1.0", batch_state.get("input", ""))
batch.queue = [dict(item) for item in batch_state.get("queue", [])]
for item in batch.queue:
batch.tree.insert(
"", "end", iid=str(item["idx"]),
values=(item["idx"], item["url"], item["status"])
)
batch.lbl_queue_count.config(text=f"Kuyruk · {len(batch.queue)} öğe")
self.switch_tab(state.get("active_tab", "single"))
if single_state.get("metadata") and single_state.get("url"):
single._metadata_url = single_state["url"]
single.current_metadata = single_state["metadata"]
single.preview_card.update_metadata(single_state["metadata"])
single.preview_card.pack(
fill="x", padx=32, pady=(0, 16), before=single.format_card
)
resolutions = single_state["metadata"].get("resolutions")
if resolutions:
single.res_cb.config(values=resolutions)
single.res_var.set(single_state.get("video_quality", resolutions[0]))
history_state = state.get("history_search")
if history_state is not None:
history_tab = self.tabs["history"]
history_tab.search_var.set(history_state)
history_tab.search_query = history_state.lower().strip()
history_tab.refresh()
drawer_state = state.get("drawer", {})
if drawer_state.get("visible"):
title = drawer_state.get("title", "İndirme")
if title.startswith("⏳ "):
title = title[2:]
self.drawer.show(title)
self.drawer.pbar["value"] = drawer_state.get("progress", 0)
self.drawer.pct_var.set(f"{float(drawer_state.get('progress', 0)):.1f}%")
self.drawer.log_text.config(state="normal")
self.drawer.log_text.insert("end", drawer_state.get("logs", ""))
self.drawer.log_text.config(state="disabled")
if engine.is_running or self._batch_running:
if drawer_state.get("console_open"):
self.drawer._toggle_console()
elif drawer_state.get("completed"):
self.drawer.on_complete_status(True, self.current_download_file)
elif drawer_state.get("cancelled"):
self.drawer.on_complete_status(False, cancelled=True)
elif drawer_state.get("failed"):
self.drawer.on_complete_status(False, error_msg=title)
def _toggle_theme(self):
state = self._capture_layout_state()
next_theme = "light" if config.get("theme", "dark") == "dark" else "dark"
config.set("theme", next_theme)
apply_theme(self, next_theme)
for child in self.winfo_children():
child.destroy()
self._build_layout()
self.btn_theme.config(text="Açık tema" if next_theme == "dark" else "Koyu tema")
self._restore_layout_state(state)
self._update_system_status()
def switch_tab(self, key):
self.active_tab_name = key
for k, btn in self.nav_buttons.items():
btn.set_active(k == key)
for k, frame in self.tabs.items():
if k == key:
frame.pack(fill="both", expand=True)
if hasattr(frame, "refresh"):
frame.refresh()
else:
frame.pack_forget()
def _update_system_status(self):
def _worker():
yt_ok, _, _ = get_ytdlp_status()
ff_ok, _, _ = get_ffmpeg_status()
js_ok, _, _ = get_js_status()
def _apply():
if yt_ok and ff_ok and js_ok:
self.lbl_sys_status.config(text="● Motorlar Aktif (Deno + FFmpeg)", fg=SUCCESS)
elif yt_ok and ff_ok:
self.lbl_sys_status.config(text="● Motorlar Aktif", fg=SUCCESS)
elif yt_ok and not ff_ok:
self.lbl_sys_status.config(text="● FFmpeg Eksik", fg=WARNING)
else:
self.lbl_sys_status.config(text="● yt-dlp Eksik", fg=ERROR)
self.after(0, _apply)
threading.Thread(target=_worker, daemon=True).start()
def _on_deps_updated(self):
self._update_system_status()
self.drawer.log_line("✅ Bağımlılıklar güncellendi ve hazır.", "success")
# ── İndirme İşlemleri ───────────────────────────────────────────
def start_single_download(self, url, out_dir, options):
if engine.is_running:
messagebox.showinfo("Bilgi", "Zaten devam eden bir indirme işlemi var.", parent=self)
return
cookies_args = self._get_download_cookies_args()
if cookies_args is None:
return
if not self._ensure_output_dir(out_dir):
return
options["cookies_args"] = cookies_args
options["proxy"] = config.get("proxy")
try:
cmd = build_command(url, out_dir, options)
except ValueError as e:
messagebox.showwarning("İndirme Ayarları", str(e), parent=self)
return
title = options.get("title") or url
fmt_desc = f"{options['format']} ({options['quality']})"
self.drawer.show(title=title)
self.drawer.log_line(f"▶ Başlatıldı: {url}", "accent")
self._start_engine(cmd, title=title, url=url, fmt_desc=fmt_desc)
def start_playlist_download(self, url, out_dir, options):
if engine.is_running:
messagebox.showinfo("Bilgi", "Zaten devam eden bir indirme işlemi var.", parent=self)
return
cookies_args = self._get_download_cookies_args()
if cookies_args is None:
return
if not self._ensure_output_dir(out_dir):
return
options["cookies_args"] = cookies_args
options["proxy"] = config.get("proxy")
try:
cmd = build_command(url, out_dir, options)
except ValueError as e:
messagebox.showwarning("İndirme Ayarları", str(e), parent=self)
return
self.drawer.show(title="Playlist İndiriliyor...")
self.drawer.log_line(f"▶ Playlist Başlatıldı: {url}", "accent")
self._start_engine(cmd, title="Playlist", url=url, fmt_desc=f"{options['format']} ({options['quality']})")
def _get_download_cookies_args(self):
try:
return self.tabs["settings"].get_cookies_args()
except (OSError, ValueError) as e:
messagebox.showerror("Çerez Ayarı Hatası", str(e), parent=self)
return None
def _ensure_output_dir(self, out_dir):
try:
os.makedirs(out_dir, exist_ok=True)
if not os.path.isdir(out_dir):
raise NotADirectoryError(f"Seçilen kayıt yolu bir klasör değil: {out_dir}")
return True
except OSError as e:
messagebox.showerror("Hata", f"Hedef klasör kullanılamıyor:\n{e}", parent=self)
return False
def start_batch_download(self, urls, out_dir, options, status_cb):
if engine.is_running or self._batch_running:
messagebox.showinfo("Bilgi", "Zaten devam eden bir indirme var.", parent=self)
return
cookies_args = self._get_download_cookies_args()
if cookies_args is None:
return
if not self._ensure_output_dir(out_dir):
return
options = dict(options)
options["cookies_args"] = cookies_args
options["proxy"] = config.get("proxy")
options["speed_limit"] = config.get("speed_limit")
options["ignore_errors"] = config.get("ignore_errors", True)
options["add_metadata"] = config.get("add_metadata", True)
options["embed_subtitles"] = config.get("embed_subtitles", False)
options["subtitle_lang"] = config.get("subtitle_lang", "tr,en")
options["concurrent_fragments"] = config.get("concurrent_fragments", 4)
self._batch_running = True
self.drawer.show(title=f"Toplu İndirme (0/{len(urls)})")
def _batch_worker():
successes = 0
failures = 0
cancelled = False
last_processed = 0
for idx, u in enumerate(urls, 1):
if not self._batch_running:
cancelled = True
break
last_processed = idx
self.after(0, lambda i=idx: status_cb(i, "⏳ İndiriliyor..."))
self.after(0, lambda i=idx: self.drawer.lbl_title.config(text=f"Toplu İndirme [{i}/{len(urls)}]"))
single_opts = dict(options)
event = threading.Event()
batch_success = False
batch_dest = None
batch_cancelled = False
batch_error = None
def _on_done(success, output_path, err, cancelled):
nonlocal batch_success, batch_dest, batch_cancelled, batch_error
batch_success = success
batch_dest = output_path
batch_cancelled = cancelled
batch_error = err
event.set()
try:
cmd = build_command(u, out_dir, single_opts)
except ValueError as e:
failures += 1
self.after(0, lambda i=idx: status_cb(i, "❌ Hatalı ayar"))
self.after(0, lambda error=str(e): self.drawer.log_line(error, "error"))
continue
def _start_item():
if not self._batch_running:
_on_done(False, None, None, True)
return
if not self._start_engine_raw(cmd, done_callback=_on_done):
_on_done(False, None, "İndirme motoru başka bir işlemle meşgul.", False)
self.after(0, _start_item)
event.wait()
if batch_success:
successes += 1
self.after(0, lambda i=idx: status_cb(i, "✅ Tamamlandı"))
history.add(os.path.basename(batch_dest or u), u, batch_dest, single_opts["format"])
elif batch_cancelled:
cancelled = True
self.after(0, lambda i=idx: status_cb(i, "⛔ İptal edildi"))
break
else:
failures += 1
self.after(0, lambda i=idx: status_cb(i, "❌ Hata"))
if batch_error:
self.after(0, lambda error=batch_error: self.drawer.log_line(error, "error"))
if cancelled:
for idx in range(last_processed + 1, len(urls) + 1):
self.after(0, lambda i=idx: status_cb(i, "⏭ Atlandı"))
self._batch_running = False
overall_success = successes == len(urls) and not cancelled
self.after(
0,
lambda: self.drawer.on_complete_status(
overall_success,
error_msg=f"{successes} başarılı, {failures} hatalı." if failures else None,
cancelled=cancelled
)
)
threading.Thread(target=_batch_worker, daemon=True).start()
def _start_engine(self, cmd, title, url, fmt_desc):
def _on_progress(pct, speed, total, eta):
self.after(0, lambda: self.drawer.update_stats(pct, speed, total, eta))
def _on_log(line, tag):
self.after(0, lambda: self.drawer.log_line(line, tag))
def _on_complete(success, output_path, err, cancelled):
self.current_download_file = output_path
self.after(0, lambda: self.drawer.on_complete_status(success, output_path, err, cancelled))
if success:
history.add(title, url, output_path, fmt_desc, status="completed")
if "history" in self.tabs:
self.after(0, self.tabs["history"].refresh)
callbacks = {
"on_progress": _on_progress,
"on_log": _on_log,
"on_complete": _on_complete
}
engine.start_download(cmd, callbacks)
def _start_engine_raw(self, cmd, done_callback):
def _on_progress(pct, speed, total, eta):
self.after(0, lambda: self.drawer.update_stats(pct, speed, total, eta))
def _on_log(line, tag):
self.after(0, lambda: self.drawer.log_line(line, tag))
def _on_complete(success, output_path, err, cancelled):
if done_callback:
done_callback(success, output_path, err, cancelled)
callbacks = {
"on_progress": _on_progress,
"on_log": _on_log,
"on_complete": _on_complete
}
return engine.start_download(cmd, callbacks)
def _on_cancel_download(self):
if engine.is_running:
self.drawer.log_line("⛔ İptal ediliyor...", "warn")
engine.cancel()
self._batch_running = False
def _open_downloaded_file(self):
if self.current_download_file:
history.show_in_folder(self.current_download_file)
def _on_close(self):
if engine.is_running or self._batch_running:
if messagebox.askokcancel("Çıkış", "Devam eden bir indirme işlemi var. İptal edip çıkmak istiyor musunuz?", parent=self):
engine.cancel()
self._batch_running = False
self.destroy()
else:
self.destroy()
def main():
app = LDownloaderApp()
app.mainloop()
if __name__ == "__main__":
main()