- Deleted obsolete files including test scripts and configuration files to streamline the project. - Introduced new modules for handling Douyin functionalities, including core operations, logging, and GUI components. - Updated import paths to reflect the new module structure, enhancing code organization and maintainability. - Added configuration files and documentation to support the new structure and improve user guidance.
362 lines
12 KiB
Python
362 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Web GUI 启动器 - GUI版本
|
|
用于启动和管理抖音挂机 Web GUI 服务
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import threading
|
|
import time
|
|
import webbrowser
|
|
from pathlib import Path
|
|
import tkinter as tk
|
|
from tkinter import ttk, scrolledtext, messagebox
|
|
|
|
# 获取脚本所在目录
|
|
BASE_DIR = Path(__file__).parent.absolute()
|
|
LOG_FILE = BASE_DIR / "webgui_startup.log"
|
|
WEB_GUI_PY = BASE_DIR / "web_gui.py"
|
|
|
|
# 默认端口
|
|
DEFAULT_PORT = 8000
|
|
|
|
|
|
class WebGUILauncher:
|
|
def __init__(self, root):
|
|
self.root = root
|
|
self.root.title("抖音挂机 Web GUI 启动器")
|
|
self.root.geometry("700x550")
|
|
self.root.resizable(True, True)
|
|
|
|
# 服务进程
|
|
self.webgui_process = None
|
|
self.monitor_thread = None
|
|
self.is_running = False
|
|
self.port = DEFAULT_PORT
|
|
|
|
# 创建UI
|
|
self.create_ui()
|
|
|
|
# 检查初始状态
|
|
self.check_service_status()
|
|
|
|
# 定期检查服务状态
|
|
self.schedule_status_check()
|
|
|
|
def create_ui(self):
|
|
"""创建用户界面"""
|
|
# 主框架
|
|
main_frame = ttk.Frame(self.root, padding="10")
|
|
main_frame.grid(row=0, column=0, sticky="nsew") # type: ignore[arg-type]
|
|
|
|
# 配置网格权重
|
|
self.root.columnconfigure(0, weight=1)
|
|
self.root.rowconfigure(0, weight=1)
|
|
main_frame.columnconfigure(1, weight=1)
|
|
main_frame.rowconfigure(2, weight=1)
|
|
|
|
# 标题
|
|
title_label = ttk.Label(
|
|
main_frame, text="抖音挂机 Web GUI 启动器", font=("Arial", 16, "bold")
|
|
)
|
|
title_label.grid(row=0, column=0, columnspan=3, pady=(0, 20))
|
|
|
|
# 状态区域
|
|
status_frame = ttk.LabelFrame(main_frame, text="服务状态", padding="10")
|
|
status_frame.grid(row=1, column=0, columnspan=3, sticky="ew", pady=(0, 10)) # type: ignore[arg-type]
|
|
status_frame.columnconfigure(1, weight=1)
|
|
|
|
self.status_label = ttk.Label(
|
|
status_frame, text="检查中...", font=("Arial", 10)
|
|
)
|
|
self.status_label.grid(row=0, column=0, columnspan=2, sticky=tk.W)
|
|
|
|
self.port_label = ttk.Label(status_frame, text=f"端口: {self.port}")
|
|
self.port_label.grid(row=1, column=0, sticky=tk.W, pady=(5, 0))
|
|
|
|
# 按钮区域
|
|
button_frame = ttk.Frame(main_frame)
|
|
button_frame.grid(row=2, column=0, columnspan=3, sticky="ew", pady=(0, 10)) # type: ignore[arg-type]
|
|
|
|
self.start_button = ttk.Button(
|
|
button_frame, text="启动服务", command=self.start_service, width=15
|
|
)
|
|
self.start_button.pack(side=tk.LEFT, padx=(0, 5))
|
|
|
|
self.stop_button = ttk.Button(
|
|
button_frame,
|
|
text="停止服务",
|
|
command=self.stop_service,
|
|
width=15,
|
|
state=tk.DISABLED,
|
|
)
|
|
self.stop_button.pack(side=tk.LEFT, padx=(0, 5))
|
|
|
|
self.open_browser_button = ttk.Button(
|
|
button_frame,
|
|
text="打开浏览器",
|
|
command=self.open_browser,
|
|
width=15,
|
|
state=tk.DISABLED,
|
|
)
|
|
self.open_browser_button.pack(side=tk.LEFT, padx=(0, 5))
|
|
|
|
self.refresh_button = ttk.Button(
|
|
button_frame, text="刷新状态", command=self.check_service_status, width=15
|
|
)
|
|
self.refresh_button.pack(side=tk.LEFT)
|
|
|
|
# 日志区域
|
|
log_frame = ttk.LabelFrame(main_frame, text="启动日志", padding="10")
|
|
log_frame.grid(row=3, column=0, columnspan=3, sticky="nsew", pady=(0, 10)) # type: ignore[arg-type]
|
|
log_frame.columnconfigure(0, weight=1)
|
|
log_frame.rowconfigure(0, weight=1)
|
|
main_frame.rowconfigure(3, weight=1)
|
|
|
|
self.log_text = scrolledtext.ScrolledText(
|
|
log_frame, height=15, width=80, wrap=tk.WORD, font=("Consolas", 9)
|
|
)
|
|
self.log_text.pack(fill=tk.BOTH, expand=True)
|
|
|
|
# 底部信息
|
|
info_frame = ttk.Frame(main_frame)
|
|
info_frame.grid(row=4, column=0, columnspan=3, sticky="ew") # type: ignore[arg-type]
|
|
|
|
info_label = ttk.Label(
|
|
info_frame,
|
|
text="提示: 服务启动后会自动打开浏览器,也可以手动点击'打开浏览器'按钮",
|
|
font=("Arial", 9),
|
|
foreground="gray",
|
|
)
|
|
info_label.pack()
|
|
|
|
def log(self, message):
|
|
"""添加日志"""
|
|
timestamp = time.strftime("%H:%M:%S")
|
|
log_message = f"[{timestamp}] {message}\n"
|
|
self.log_text.insert(tk.END, log_message)
|
|
self.log_text.see(tk.END)
|
|
print(log_message.strip()) # 同时输出到控制台
|
|
|
|
def check_service_status(self):
|
|
"""检查服务状态"""
|
|
try:
|
|
import socket
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(1)
|
|
result = sock.connect_ex(("127.0.0.1", self.port))
|
|
sock.close()
|
|
|
|
if result == 0:
|
|
# 端口已监听,服务运行中
|
|
self.is_running = True
|
|
self.status_label.config(text="✓ 服务运行中", foreground="green")
|
|
self.start_button.config(state=tk.DISABLED)
|
|
self.stop_button.config(state=tk.NORMAL)
|
|
self.open_browser_button.config(state=tk.NORMAL)
|
|
else:
|
|
# 端口未监听,服务未运行
|
|
self.is_running = False
|
|
self.status_label.config(text="✗ 服务未运行", foreground="red")
|
|
self.start_button.config(state=tk.NORMAL)
|
|
self.stop_button.config(state=tk.DISABLED)
|
|
self.open_browser_button.config(state=tk.DISABLED)
|
|
except Exception as e:
|
|
self.log(f"检查服务状态时出错: {e}")
|
|
self.is_running = False
|
|
self.status_label.config(text="? 状态未知", foreground="orange")
|
|
|
|
def start_service(self):
|
|
"""启动服务"""
|
|
if self.is_running:
|
|
messagebox.showwarning("警告", "服务已经在运行中")
|
|
return
|
|
|
|
# 检查文件是否存在
|
|
if not WEB_GUI_PY.exists():
|
|
messagebox.showerror("错误", f"未找到 web_gui.py 文件\n路径: {WEB_GUI_PY}")
|
|
return
|
|
|
|
self.log("=" * 50)
|
|
self.log("开始启动 Web GUI 服务...")
|
|
|
|
# 停止旧进程
|
|
self.stop_old_processes()
|
|
|
|
# 设置环境变量
|
|
env = os.environ.copy()
|
|
env["PYTHONIOENCODING"] = "utf-8"
|
|
env["PYTHONUNBUFFERED"] = "1"
|
|
if "PORT" not in env and "WEB_GUI_PORT" not in env:
|
|
env["PORT"] = str(self.port)
|
|
|
|
try:
|
|
# 检查是否有虚拟环境
|
|
venv_python = BASE_DIR / ".venv" / "Scripts" / "python.exe"
|
|
if venv_python.exists():
|
|
python_exe = str(venv_python)
|
|
self.log("使用虚拟环境启动...")
|
|
else:
|
|
python_exe = sys.executable
|
|
self.log("使用系统Python启动...")
|
|
|
|
# 启动进程(在Windows上显示控制台窗口以便查看日志)
|
|
creation_flags = 0
|
|
if sys.platform == "win32":
|
|
# 使用普通窗口,不隐藏,这样可以看到日志
|
|
creation_flags = 0
|
|
|
|
self.webgui_process = subprocess.Popen(
|
|
[python_exe, str(WEB_GUI_PY)],
|
|
cwd=str(BASE_DIR),
|
|
env=env,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
bufsize=1,
|
|
creationflags=creation_flags,
|
|
)
|
|
|
|
self.log(f"服务进程已启动 (PID: {self.webgui_process.pid})")
|
|
|
|
# 启动监控线程
|
|
self.monitor_thread = threading.Thread(
|
|
target=self.monitor_process, daemon=True
|
|
)
|
|
self.monitor_thread.start()
|
|
|
|
# 等待服务启动
|
|
self.wait_for_service()
|
|
|
|
except Exception as e:
|
|
self.log(f"启动服务失败: {e}")
|
|
messagebox.showerror("错误", f"启动服务失败:\n{e}")
|
|
self.webgui_process = None
|
|
|
|
def stop_service(self):
|
|
"""停止服务"""
|
|
if not self.is_running and not self.webgui_process:
|
|
messagebox.showinfo("提示", "服务未运行")
|
|
return
|
|
|
|
self.log("正在停止服务...")
|
|
|
|
# 停止进程
|
|
self.stop_old_processes()
|
|
|
|
if self.webgui_process:
|
|
try:
|
|
self.webgui_process.terminate()
|
|
self.webgui_process.wait(timeout=5)
|
|
self.log("服务已停止")
|
|
except subprocess.TimeoutExpired:
|
|
self.webgui_process.kill()
|
|
self.log("服务已强制停止")
|
|
except Exception as e:
|
|
self.log(f"停止服务时出错: {e}")
|
|
finally:
|
|
self.webgui_process = None
|
|
|
|
time.sleep(1)
|
|
self.check_service_status()
|
|
|
|
def stop_old_processes(self):
|
|
"""停止旧的web_gui.py进程"""
|
|
try:
|
|
if sys.platform == "win32":
|
|
# 使用PowerShell命令停止旧进程
|
|
ps_cmd = (
|
|
"Get-CimInstance Win32_Process | "
|
|
"Where-Object { ($_.CommandLine -like '*web_gui.py*') -and ($_.Name -like 'python*') } | "
|
|
"ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }"
|
|
)
|
|
subprocess.run(
|
|
["powershell", "-NoProfile", "-Command", ps_cmd],
|
|
capture_output=True,
|
|
timeout=5,
|
|
)
|
|
else:
|
|
# Linux/Mac
|
|
subprocess.run(
|
|
["pkill", "-f", "web_gui.py"], capture_output=True, timeout=5
|
|
)
|
|
except Exception as e:
|
|
self.log(f"停止旧进程时出错: {e}")
|
|
|
|
def monitor_process(self):
|
|
"""监控进程输出"""
|
|
if not self.webgui_process:
|
|
return
|
|
|
|
try:
|
|
if self.webgui_process.stdout is None:
|
|
return
|
|
for line in iter(self.webgui_process.stdout.readline, ""):
|
|
if not line:
|
|
break
|
|
line = line.strip()
|
|
if line:
|
|
self.root.after(0, self.log, line)
|
|
except Exception:
|
|
pass
|
|
|
|
def wait_for_service(self):
|
|
"""等待服务启动"""
|
|
self.log("等待服务启动...")
|
|
max_wait = 30
|
|
for i in range(max_wait):
|
|
time.sleep(1)
|
|
self.check_service_status()
|
|
if self.is_running:
|
|
self.log("服务启动成功!")
|
|
self.open_browser()
|
|
return
|
|
self.root.update()
|
|
|
|
self.log("服务启动超时,请检查日志")
|
|
messagebox.showwarning("警告", "服务启动超时,请检查日志窗口")
|
|
|
|
def open_browser(self):
|
|
"""打开浏览器"""
|
|
url = f"http://127.0.0.1:{self.port}"
|
|
self.log(f"打开浏览器: {url}")
|
|
try:
|
|
webbrowser.open(url)
|
|
except Exception as e:
|
|
self.log(f"打开浏览器失败: {e}")
|
|
messagebox.showerror("错误", f"打开浏览器失败:\n{e}")
|
|
|
|
def schedule_status_check(self):
|
|
"""定期检查服务状态"""
|
|
self.check_service_status()
|
|
# 每5秒检查一次
|
|
self.root.after(5000, self.schedule_status_check)
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
root = tk.Tk()
|
|
app = WebGUILauncher(root)
|
|
|
|
# 处理窗口关闭事件
|
|
def on_closing():
|
|
if app.is_running:
|
|
if messagebox.askyesno("确认", "服务正在运行,是否要停止服务?"):
|
|
app.stop_service()
|
|
root.destroy()
|
|
|
|
root.protocol("WM_DELETE_WINDOW", on_closing)
|
|
|
|
try:
|
|
root.mainloop()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|