|
1 | 1 | import json |
| 2 | +import os |
2 | 3 | import subprocess |
3 | 4 | import sys |
4 | 5 | import time |
5 | 6 | from random import randint |
6 | 7 | from datetime import datetime |
7 | 8 | from json import JSONDecodeError |
8 | | -from urllib.parse import urlencode |
9 | | - |
| 9 | +import shutil |
10 | 10 | import qrcode |
11 | 11 | from loguru import logger |
12 | 12 |
|
@@ -145,7 +145,6 @@ def buy_stream( |
145 | 145 | if err == 100051: |
146 | 146 | break |
147 | 147 | yield f"[尝试 {attempt}/60] [{err}]({ERRNO_DICT.get(err, '未知错误码')}) | {ret}" |
148 | | - |
149 | 148 |
|
150 | 149 | time.sleep(interval / 1000) |
151 | 150 |
|
@@ -257,9 +256,25 @@ def buy_new_terminal( |
257 | 256 | show_random_message=True, |
258 | 257 | terminal_ui="网页", |
259 | 258 | ) -> subprocess.Popen: |
260 | | - command = [sys.executable] |
261 | | - if not getattr(sys, "frozen", False): |
262 | | - command.extend(["main.py"]) |
| 259 | + command = None |
| 260 | + |
| 261 | + # 1️⃣ PyInstaller / frozen |
| 262 | + if getattr(sys, "frozen", False): |
| 263 | + command = [sys.executable] |
| 264 | + else: |
| 265 | + # 2️⃣ 源码模式:检查「当前脚本目录」是否有 main.py |
| 266 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 267 | + main_py = os.path.join(script_dir, "main.py") |
| 268 | + |
| 269 | + if os.path.exists(main_py): |
| 270 | + command = [sys.executable, main_py] |
| 271 | + # 3️⃣ 兜底:使用 btb(pip / pipx) |
| 272 | + else: |
| 273 | + btb_path = shutil.which("btb") |
| 274 | + if not btb_path: |
| 275 | + raise RuntimeError("Cannot find main.py or btb command") |
| 276 | + |
| 277 | + command = [btb_path] |
263 | 278 | command.extend(["buy", tickets_info]) |
264 | 279 | if interval is not None: |
265 | 280 | command.extend(["--interval", str(interval)]) |
|
0 commit comments