summaryrefslogtreecommitdiff
path: root/yt_dlp_qt6/main_window.py
blob: dd956644a5d424a0b4866aaef9a05ce8490c17b9 (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
__license__ = 'GPL-3.0-only'

from pathlib import Path
from PyQt6.QtWidgets import (QMainWindow, QMessageBox, QFileDialog,
                             QGridLayout, QSpacerItem, QSizePolicy,
                             QWidget, QLabel, QLineEdit, QPushButton)
import yt_dlp
from yt_dlp import YoutubeDL

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("yt-dlp")
        self.resize(600, 160)

        layout = QGridLayout()

        self.url = QLineEdit()
        self.download_path = QLineEdit()
        self.download_path.setText(str(Path.home()/'Downloads'))
        browse_btn = QPushButton("...")
        browse_btn.clicked.connect(self.browse)
        download_btn = QPushButton("Download")

        layout.addWidget(QLabel("URL:"), 0, 0)
        layout.addWidget(self.url, 0, 1, 1, 2)

        layout.addWidget(QLabel("Download:"), 1, 0)
        layout.addWidget(self.download_path, 1, 1)
        layout.addWidget(browse_btn, 1, 2)

        layout.addItem(QSpacerItem(0, 0,
                                   QSizePolicy.Policy.Minimum,
                                   QSizePolicy.Policy.Expanding),
                       2, 1)

        layout.addWidget(download_btn, 3, 1)
        download_btn.clicked.connect(self.download)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def on_finish_hook(self, data):
        if data['status'] == 'finished':
            print(f"\n\nfinished: {data['filename']}\n")

    def browse(self):
        dirpath = QFileDialog.getExistingDirectory(self)
        if len(dirpath) > 0:
            self.download_path.setText(dirpath)

    def download(self):
        ydl_opts = {
                'paths': {'home': self.download_path.text()},
                'progress_hooks': [self.on_finish_hook],
        }

        with YoutubeDL(ydl_opts) as ydl:
            try:
                ydl.download([self.url.text()])
            except yt_dlp.utils.DownloadError as ex:
                box = QMessageBox(self)
                box.setWindowTitle(self.windowTitle())
                box.setText('Error downloading using yt-dlp')
                box.setDetailedText(f'{ex}')
                box.exec()
            finally:
                QMessageBox.information(self, self.windowTitle(), 'Finished')
                self.url.clear()