jscmp4 commited on
Commit
fa75915
·
verified ·
1 Parent(s): 1b29f3e

Update eh_logic.py

Browse files
Files changed (1) hide show
  1. eh_logic.py +58 -10
eh_logic.py CHANGED
@@ -1,14 +1,25 @@
1
  import os
2
  import shutil
3
  import subprocess
 
4
  from utils import BASE_DIR, PDF_FINAL_DIR, sanitize_filename, manual_merge_pdf
5
 
6
- def run_eh_download(eh_url, cookies_str):
 
 
 
 
 
 
 
 
7
  # 1. 检查参数
8
  if not eh_url:
9
  yield None, "❌ 请输入画廊链接", "参数缺失"
10
  return
11
 
 
 
12
  # 2. 目录准备
13
  eh_base_dir = os.path.join(BASE_DIR, "eh_temp")
14
  if os.path.exists(eh_base_dir):
@@ -19,46 +30,80 @@ def run_eh_download(eh_url, cookies_str):
19
  os.makedirs(eh_base_dir, exist_ok=True)
20
 
21
  # 3. 处理 Cookies
 
22
  cookie_file_path = os.path.join(BASE_DIR, "eh_cookies.txt")
23
  with open(cookie_file_path, "w", encoding="utf-8") as f:
24
  f.write(cookies_str)
25
 
26
- yield None, "🚀 启动 gallery-dl 解析...", "开始解析"
27
-
28
  # 4. 构建命令
 
29
  cmd = [
30
  "gallery-dl",
 
31
  "--directory", eh_base_dir,
32
  "--cookies", cookie_file_path,
33
  eh_url
34
  ]
35
 
 
 
 
 
 
 
 
36
  # 5. 执行下载
37
  try:
 
38
  process = subprocess.Popen(
39
  cmd,
40
  stdout=subprocess.PIPE,
41
  stderr=subprocess.STDOUT,
42
- text=True
 
43
  )
44
 
45
  logs = ""
 
 
 
46
  for line in process.stdout:
47
- logs += line
48
- if "Example" in line or "http" in line or "#" in line:
49
- yield None, f"📥 下载中...\n{line.strip()}", "下载中"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  process.wait()
52
 
53
  if process.returncode != 0:
54
- yield None, f" 下载失败,请检查 Cookies 或链接。\n日志片段:\n{logs[-500:]}", "下载失败"
 
55
  return
56
 
57
  except Exception as e:
 
58
  yield None, f"❌ 调用出错: {e}", "系统错误"
59
  return
60
 
61
  # 6. 寻找图片目录
 
62
  target_img_dir = None
63
  for root, dirs, files in os.walk(eh_base_dir):
64
  if any(f.lower().endswith(('.jpg', '.png', '.jpeg')) for f in files):
@@ -66,7 +111,7 @@ def run_eh_download(eh_url, cookies_str):
66
  break
67
 
68
  if not target_img_dir:
69
- yield None, "❌ 未找到下载的图片 (可能是 Sad Panda 问题)", "未找到图片"
70
  return
71
 
72
  # 7. 合并 PDF
@@ -75,11 +120,14 @@ def run_eh_download(eh_url, cookies_str):
75
  final_pdf_name = f"[EH] {safe_title}.pdf"
76
  final_pdf_path = os.path.join(PDF_FINAL_DIR, final_pdf_name)
77
 
78
- yield None, "🔨 正在合并 PDF...", "正在合并"
 
 
79
  try:
80
  manual_merge_pdf(target_img_dir, final_pdf_path)
81
  except Exception as e:
82
  yield None, f"❌ 合并失败: {e}", "合并失败"
83
  return
84
 
 
85
  yield [final_pdf_path], f"✅ 处理完成!\n文件: {final_pdf_name}", "完成"
 
1
  import os
2
  import shutil
3
  import subprocess
4
+ import gradio as gr # 引入 gradio 用于类型提示和进度条
5
  from utils import BASE_DIR, PDF_FINAL_DIR, sanitize_filename, manual_merge_pdf
6
 
7
+ def run_eh_download(eh_url, cookies_str, progress=gr.Progress()):
8
+ """
9
+ progress=gr.Progress() 是 Gradio 的魔法参数。
10
+ 只要函数参数里写了这个,Gradio 就会自动在界面显示进度条。
11
+ """
12
+
13
+ # --- Debug: 打印到后台控制台,确认函数被调用 ---
14
+ print(f"[-] Debug:收到任务 URL: {eh_url}")
15
+
16
  # 1. 检查参数
17
  if not eh_url:
18
  yield None, "❌ 请输入画廊链接", "参数缺失"
19
  return
20
 
21
+ progress(0, desc="正在初始化环境...")
22
+
23
  # 2. 目录准备
24
  eh_base_dir = os.path.join(BASE_DIR, "eh_temp")
25
  if os.path.exists(eh_base_dir):
 
30
  os.makedirs(eh_base_dir, exist_ok=True)
31
 
32
  # 3. 处理 Cookies
33
+ progress(0.1, desc="正在写入 Cookies...")
34
  cookie_file_path = os.path.join(BASE_DIR, "eh_cookies.txt")
35
  with open(cookie_file_path, "w", encoding="utf-8") as f:
36
  f.write(cookies_str)
37
 
 
 
38
  # 4. 构建命令
39
+ # 增加 --verbose 参数,这样如果卡住,日志会显示更多细节
40
  cmd = [
41
  "gallery-dl",
42
+ "--verbose",
43
  "--directory", eh_base_dir,
44
  "--cookies", cookie_file_path,
45
  eh_url
46
  ]
47
 
48
+ # --- Debug: 打印完整命令 ---
49
+ # 你可以复制这行命令直接在终端运行,看看是不是环境问题
50
+ print(f"[-] Debug: 即将执行命令: {' '.join(cmd)}")
51
+
52
+ yield None, f"🚀 正在启动进程...\n命令: {' '.join(cmd)}", "启动中"
53
+ progress(0.2, desc="正在连接 E站 (若卡住请检查Cookies)...")
54
+
55
  # 5. 执行下载
56
  try:
57
+ # bufsize=0 和 universal_newlines=True 确保输出不被缓存,实时显示
58
  process = subprocess.Popen(
59
  cmd,
60
  stdout=subprocess.PIPE,
61
  stderr=subprocess.STDOUT,
62
+ text=True,
63
+ bufsize=1
64
  )
65
 
66
  logs = ""
67
+ img_count = 0
68
+
69
+ # 逐行读取日志
70
  for line in process.stdout:
71
+ line = line.strip()
72
+ if not line: continue
73
+
74
+ logs += line + "\n"
75
+
76
+ # --- 进度条逻辑 ---
77
+ # gallery-dl 的 verbose 输出里,下载图片通常包含 "#" 或 "http"
78
+ if "#" in line and "http" in line:
79
+ img_count += 1
80
+ # 更新进度条描述
81
+ progress(None, desc=f"正在下载第 {img_count} 张图片...")
82
+
83
+ # 遇到特定关键词更新状态
84
+ if "Connection" in line:
85
+ progress(None, desc="正在建立连接...")
86
+ elif "403 Forbidden" in line:
87
+ progress(None, desc="❌ 403 拒绝访问 (Cookies无效)")
88
+
89
+ # 实时推送到前端日志框
90
+ # 限制日志长度防止前端卡顿,只显示最近 10 行
91
+ yield None, f"{line}", f"已下载: {img_count}"
92
 
93
  process.wait()
94
 
95
  if process.returncode != 0:
96
+ print(f"[-] Debug: 进程退出码: {process.returncode}")
97
+ yield None, f"❌ 下载失败 (Code {process.returncode})。\n请查看上方日志检查 Cookies 是否过期。", "失败"
98
  return
99
 
100
  except Exception as e:
101
+ print(f"[-] Debug: Python 异常: {e}")
102
  yield None, f"❌ 调用出错: {e}", "系统错误"
103
  return
104
 
105
  # 6. 寻找图片目录
106
+ progress(0.9, desc="下载完成,正在搜索文件...")
107
  target_img_dir = None
108
  for root, dirs, files in os.walk(eh_base_dir):
109
  if any(f.lower().endswith(('.jpg', '.png', '.jpeg')) for f in files):
 
111
  break
112
 
113
  if not target_img_dir:
114
+ yield None, "❌ 未找到下载的图片可能是 Sad Panda 或下载被中断。", "空目录"
115
  return
116
 
117
  # 7. 合并 PDF
 
120
  final_pdf_name = f"[EH] {safe_title}.pdf"
121
  final_pdf_path = os.path.join(PDF_FINAL_DIR, final_pdf_name)
122
 
123
+ progress(0.95, desc="正在合并 PDF (大文件可能较慢)...")
124
+ yield None, "🔨 正在合并 PDF...", "合并中"
125
+
126
  try:
127
  manual_merge_pdf(target_img_dir, final_pdf_path)
128
  except Exception as e:
129
  yield None, f"❌ 合并失败: {e}", "合并失败"
130
  return
131
 
132
+ progress(1.0, desc="全部完成!")
133
  yield [final_pdf_path], f"✅ 处理完成!\n文件: {final_pdf_name}", "完成"