From 419731448cc6dea9a2f96bf011e901e6c6cf36dc Mon Sep 17 00:00:00 2001 From: "guorong.zheng" <360996299@qq.com> Date: Fri, 15 Nov 2024 14:09:31 +0800 Subject: [PATCH] chore:run service --- main.py | 27 +++++++++++++++++---------- tkinter_ui/tkinter_ui.py | 10 ++++++++-- utils/tools.py | 15 +++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 7088327d40..a75188fca1 100644 --- a/main.py +++ b/main.py @@ -224,8 +224,6 @@ async def main(self): True, url=f"{get_ip_address()}" if open_service else None, ) - if open_service: - run_service() except asyncio.exceptions.CancelledError: print("Update cancelled!") @@ -253,14 +251,23 @@ def scheduled_task(): def run_service(): - if not os.environ.get("GITHUB_ACTIONS"): - ip_address = get_ip_address() - print(f"📄 Result detail: {ip_address}/result") - print(f"📄 Log detail: {ip_address}/log") - print(f"✅ You can use this url to watch IPTV 📺: {ip_address}") - app.run(host="0.0.0.0", port=8000) + try: + if not os.environ.get("GITHUB_ACTIONS"): + ip_address = get_ip_address() + print(f"📄 Result detail: {ip_address}/result") + print(f"📄 Log detail: {ip_address}/log") + print(f"✅ You can use this url to watch IPTV 📺: {ip_address}") + app.run(host="0.0.0.0", port=8000) + except Exception as e: + print(f"❌ Service start failed: {e}") if __name__ == "__main__": - if len(sys.argv) == 1 or (len(sys.argv) > 1 and sys.argv[1] == "scheduled_task"): - scheduled_task() + if len(sys.argv) == 1 and config.open_service: + loop = asyncio.new_event_loop() + + async def run_service_async(): + loop.run_in_executor(None, run_service) + + asyncio.run(run_service_async()) + scheduled_task() diff --git a/tkinter_ui/tkinter_ui.py b/tkinter_ui/tkinter_ui.py index f538808800..b5905282a0 100644 --- a/tkinter_ui/tkinter_ui.py +++ b/tkinter_ui/tkinter_ui.py @@ -7,7 +7,7 @@ from PIL import Image, ImageTk from utils.config import config from utils.tools import resource_path -from main import UpdateSource +from main import UpdateSource, run_service import asyncio import threading import webbrowser @@ -113,11 +113,17 @@ async def run_update(self): self.progress_label.pack_forget() def on_run_update(self): + loop = asyncio.new_event_loop() + + async def run_service_async(): + loop.run_in_executor(None, run_service) + def run_loop(): - loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(self.run_update()) + if config.open_service: + asyncio.run(run_service_async()) self.thread = threading.Thread(target=run_loop, daemon=True) self.thread.start() diff --git a/utils/tools.py b/utils/tools.py index f38f205294..db93397c85 100644 --- a/utils/tools.py +++ b/utils/tools.py @@ -400,12 +400,15 @@ def get_result_file_content(show_result=False): Get the content of the result file """ user_final_file = resource_path(config.final_file) - if config.open_m3u_result: - user_final_file = os.path.splitext(user_final_file)[0] + ".m3u" - if show_result == False: - return send_file(user_final_file, as_attachment=True) - with open(user_final_file, "r", encoding="utf-8") as file: - content = file.read() + if os.path.exists(user_final_file): + if config.open_m3u_result: + user_final_file = os.path.splitext(user_final_file)[0] + ".m3u" + if show_result == False: + return send_file(user_final_file, as_attachment=True) + with open(user_final_file, "r", encoding="utf-8") as file: + content = file.read() + else: + content = "🔍️正在更新,请耐心等待更新完成..." return render_template_string( "
{{ content }}
", content=content,