35 lines
1.5 KiB
Python
35 lines
1.5 KiB
Python
import sys, os, time, socket, threading, webbrowser
|
|
if getattr(sys, 'frozen', False):
|
|
BASE_DIR = sys._MEIPASS
|
|
APP_DIR = os.path.dirname(sys.executable)
|
|
else:
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
APP_DIR = BASE_DIR
|
|
BACKEND_DIR = os.path.join(BASE_DIR,'backend') if os.path.isdir(os.path.join(BASE_DIR,'backend')) else BASE_DIR
|
|
sys.path.insert(0, BACKEND_DIR)
|
|
sys.path.insert(0, BASE_DIR)
|
|
APPDATA_DIR = os.path.join(os.environ.get('APPDATA', APP_DIR), 'BatchTube')
|
|
os.makedirs(APPDATA_DIR, exist_ok=True)
|
|
os.environ['BATCHTUBE_DB_PATH'] = os.path.join(APPDATA_DIR, 'batchtube.db')
|
|
for c in [os.path.join(APP_DIR,'yt-dlp.exe'), os.path.join(BASE_DIR,'yt-dlp.exe')]:
|
|
if os.path.exists(c): os.environ['YTDLP_PATH'] = c; break
|
|
for d in [os.path.join(APP_DIR,'ffmpeg'), os.path.join(BASE_DIR,'ffmpeg')]:
|
|
if os.path.isdir(d): os.environ['PATH'] = d + os.pathsep + os.environ.get('PATH',''); break
|
|
HOST, PORT = '127.0.0.1', 8765
|
|
URL = f'http://{HOST}:{PORT}'
|
|
def is_open(h, p, t=0.5):
|
|
try:
|
|
with socket.create_connection((h,p), timeout=t): return True
|
|
except: return False
|
|
def open_browser():
|
|
for _ in range(60):
|
|
if is_open(HOST, PORT): time.sleep(0.3); webbrowser.open(URL); return
|
|
time.sleep(0.5)
|
|
def run_server():
|
|
import uvicorn
|
|
os.chdir(BACKEND_DIR)
|
|
uvicorn.run('api_server:app', host=HOST, port=PORT, log_level='warning', access_log=False)
|
|
if __name__ == '__main__':
|
|
threading.Thread(target=open_browser, daemon=True).start()
|
|
run_server()
|