2026-05-16 20:26:51 +08:00
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
2026-06-15 00:16:44 +08:00
|
|
|
from pathlib import Path
|
2026-05-16 20:26:51 +08:00
|
|
|
import click
|
2026-08-25 17:46:48 +08:00
|
|
|
import platformdirs
|
2026-06-18 21:16:09 +08:00
|
|
|
|
|
|
|
|
# Qt
|
2026-08-25 17:46:48 +08:00
|
|
|
from PySide6.QtWidgets import QApplication, QMessageBox, QFileDialog, QMainWindow
|
|
|
|
|
from PySide6.QtGui import QIcon
|
2026-05-16 20:26:51 +08:00
|
|
|
|
2026-08-25 17:46:48 +08:00
|
|
|
from app import PlaylistSaver
|
|
|
|
|
from constant import PROGRAM_DIR
|
|
|
|
|
from pl_lib import messagebox
|
2026-05-16 20:26:51 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-08-25 17:46:48 +08:00
|
|
|
def show_error(message):
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
app.setApplicationName("Launcher Bootstrapper")
|
|
|
|
|
window = QMainWindow()
|
|
|
|
|
messagebox.error(window, "Error", message)
|
|
|
|
|
app.exit()
|
2026-05-16 20:26:51 +08:00
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
|
@click.argument("url_schema", default=None, required=False)
|
|
|
|
|
@click.option("--cookie-file", "-cf", required=False, default=None, help="Cookies file",
|
|
|
|
|
type=click.Path(exists=True))
|
2026-08-25 17:46:48 +08:00
|
|
|
@click.option("--work-dir", "-wd", required=False, default=None, help="Custom working directory",
|
2026-06-18 21:16:09 +08:00
|
|
|
type=click.Path(exists=True))
|
2026-08-25 17:46:48 +08:00
|
|
|
def main(url_schema, cookie_file: str, work_dir: str | None):
|
2026-06-18 21:16:09 +08:00
|
|
|
icon_path = PROGRAM_DIR / "assets" / "icon.ico"
|
2026-05-16 20:26:51 +08:00
|
|
|
|
2026-08-25 17:46:48 +08:00
|
|
|
# Default working directory
|
|
|
|
|
if not work_dir:
|
|
|
|
|
try:
|
|
|
|
|
# Use user application data directory
|
|
|
|
|
work_dir = work_dir = platformdirs.user_data_dir("PlaylistSaver", appauthor=False, roaming=True)
|
|
|
|
|
os.makedirs(work_dir, exist_ok=True)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning("Failed to get application directory: %s", e)
|
|
|
|
|
show_error("Unable to get application directory. Try specifying 'work_dir' argument.")
|
|
|
|
|
sys.exit(-1)
|
2026-05-16 20:26:51 +08:00
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
if work_dir is None:
|
|
|
|
|
app = QApplication(sys.argv)
|
2026-05-16 20:26:51 +08:00
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
if icon_path.exists():
|
|
|
|
|
app.setWindowIcon(QIcon(icon_path.as_posix()))
|
2026-05-16 20:26:51 +08:00
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
folder = QFileDialog.getExistingDirectory(
|
|
|
|
|
None, "Select a folder as the new working directory"
|
2026-05-16 20:26:51 +08:00
|
|
|
)
|
|
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
if folder:
|
|
|
|
|
work_dir = Path(folder)
|
|
|
|
|
else:
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
None,
|
|
|
|
|
"Warning",
|
|
|
|
|
"PlaylistSaver requires a working directory to store files. Please specify a working directory"
|
2026-08-25 17:46:48 +08:00
|
|
|
" using argument \"--work-dir\".",
|
2026-06-15 01:16:26 +08:00
|
|
|
)
|
2026-06-18 21:16:09 +08:00
|
|
|
sys.exit(1)
|
2026-06-15 01:16:26 +08:00
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
app.shutdown()
|
2026-06-15 01:16:26 +08:00
|
|
|
|
2026-08-25 17:46:48 +08:00
|
|
|
logger.info(f"Working directory: {work_dir}")
|
|
|
|
|
|
|
|
|
|
# Create app and set icon
|
2026-06-18 21:16:09 +08:00
|
|
|
app = PlaylistSaver(
|
2026-08-25 17:46:48 +08:00
|
|
|
work_dir,
|
2026-06-18 21:16:09 +08:00
|
|
|
url_schema,
|
2026-08-25 17:46:48 +08:00
|
|
|
cookie_file=cookie_file,
|
2026-06-18 21:16:09 +08:00
|
|
|
)
|
2026-08-25 17:46:48 +08:00
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
if icon_path.exists():
|
|
|
|
|
app.setWindowIcon(QIcon(icon_path.as_posix()))
|
2026-08-25 17:46:48 +08:00
|
|
|
else:
|
|
|
|
|
logger.warning("Application icon not found.")
|
|
|
|
|
|
2026-06-18 21:16:09 +08:00
|
|
|
sys.exit(app.exec())
|
2026-06-15 01:16:26 +08:00
|
|
|
|
2026-05-16 20:26:51 +08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|