summarylogtreecommitdiffstats
path: root/main.py
blob: 03c53b0b948275b6e0375ec9eb1a9c3905b33197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
import sys
import shutil
import subprocess
from pathlib import Path

from PyQt6.QtWidgets import (
    QApplication, QWidget, QLabel, QLineEdit,
    QPushButton, QVBoxLayout, QHBoxLayout,
    QComboBox, QTextEdit, QMessageBox
)
from PyQt6.QtCore import QThread, pyqtSignal


DOWNLOAD_DIR = Path.home() / "Pobrane" / "OneTube"


class DownloadThread(QThread):
    log = pyqtSignal(str)
    finished_ok = pyqtSignal(bool)

    def __init__(self, url: str, fmt: str):
        super().__init__()
        self.url = url
        self.fmt = fmt

    def run(self):
        try:
            DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)

            out_template = str(DOWNLOAD_DIR / "%(title)s.%(ext)s")

            if self.fmt == "mp4":
                cmd = [
                    "yt-dlp",
                    "-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best",
                    "-o", out_template,
                    self.url
                ]
            else:
                cmd = [
                    "yt-dlp",
                    "--extract-audio",
                    "--audio-format", "mp3",
                    "--audio-quality", "0",
                    "-o", out_template,
                    self.url
                ]

            self.log.emit("Uruchamiam:\n" + " ".join(cmd) + "\n")

            proc = subprocess.Popen(
                cmd,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                text=True
            )

            for line in proc.stdout:
                self.log.emit(line.rstrip())

            proc.wait()
            self.finished_ok.emit(proc.returncode == 0)

        except Exception as e:
            self.log.emit(f"Błąd: {e}")
            self.finished_ok.emit(False)


class OneTubeGUI(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OneTube")
        self.setMinimumWidth(500)

        self.url_input = QLineEdit()
        self.url_input.setPlaceholderText("Wklej link (YouTube / inne)")

        self.format_box = QComboBox()
        self.format_box.addItems(["mp4", "mp3"])

        self.download_btn = QPushButton("Pobierz")
        self.download_btn.clicked.connect(self.start_download)

        self.log_box = QTextEdit()
        self.log_box.setReadOnly(True)

        top = QVBoxLayout()
        top.addWidget(QLabel("URL:"))
        top.addWidget(self.url_input)

        row = QHBoxLayout()
        row.addWidget(QLabel("Format:"))
        row.addWidget(self.format_box)
        row.addStretch()
        row.addWidget(self.download_btn)

        top.addLayout(row)
        top.addWidget(QLabel("Log:"))
        top.addWidget(self.log_box)

        self.setLayout(top)

        self.check_deps()

    def check_deps(self):
        if shutil.which("yt-dlp") is None:
            QMessageBox.critical(
                self,
                "Brak yt-dlp",
                "Nie znaleziono yt-dlp.\nZainstaluj: pip install -U yt-dlp"
            )
            sys.exit(1)

    def start_download(self):
        url = self.url_input.text().strip()
        if not url:
            QMessageBox.warning(self, "Błąd", "Podaj URL.")
            return

        fmt = self.format_box.currentText()
        self.log_box.clear()
        self.download_btn.setEnabled(False)

        self.thread = DownloadThread(url, fmt)
        self.thread.log.connect(self.log_box.append)
        self.thread.finished_ok.connect(self.download_finished)
        self.thread.start()

    def download_finished(self, ok: bool):
        self.download_btn.setEnabled(True)
        if ok:
            QMessageBox.information(
                self,
                "Gotowe",
                f"Pobrano do:\n{DOWNLOAD_DIR}"
            )
        else:
            QMessageBox.critical(self, "Błąd", "Pobieranie nie powiodło się.")


def main():
    app = QApplication(sys.argv)
    win = OneTubeGUI()
    win.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()