aboutsummaryrefslogtreecommitdiffstats
path: root/hypervideo_gui.py
blob: 89bfa850db2bce5fe2cccedf7ff620557c2eb2c5 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
''' Hypervideo GUI '''

import sys
import os
import re
import threading
import json

import hypervideo

from PyQt5.QtWidgets import (
    QAction,
    QApplication,
    QComboBox,
    QFileDialog,
    QGridLayout,
    QLabel,
    QLineEdit,
    QMainWindow,
    QMessageBox,
    QPushButton,
    QWidget,
)

from PyQt5.QtCore import (
    pyqtSlot,
)

__version__ = "0.1"
__license__ = "GPL-3"

__path__ = os.environ['HOME']
__pathBASE__ = r'%s/.config/hypervideo_gui/' % __path__
if not os.path.exists(__pathBASE__):
    os.makedirs(__pathBASE__)

GUI_STATE_JSON_FILE = '%s/.config/hypervideo_gui/hypervideo_gui_state.json' % __path__


class App(QMainWindow):
    """
    Simple applicaction for download using hypervideo
    """
    _VALID_URL = r"""^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"""

    def __init__(self):
        super().__init__()

        # load GUI state from file, load default state if save file not found
        gui_state = self.load_gui_state()
        self.download_folder_list = gui_state['DownloadFolderList']
        self.default_formats_menu_items = ['Video/Audio - Best Quality',
                                           'Audio Only - Best Quality',
                                           'Detect All Available Formats']

        # initialize window dimensions
        self.left = 100
        self.top = 100
        self.width = 640
        self.height = 200
        self.setFixedSize(self.width, self.height)

        self._init_ui()

    def _init_ui(self):
        ''' Initial UI '''
        self.setWindowTitle('Simple Hypervideo Downloader GUI')
        self.setGeometry(self.left, self.top, self.width, self.height)
        # Initialize status bar
        self.statusBar().showMessage('Welcome to Simple Hypervideo Downloader GUI!')

        # Menu
        main_menu = self.menuBar()
        file_menu = main_menu.addMenu('File')
        help_menu = main_menu.addMenu('Help')

        # Exit button
        exit_button = QAction('Exit', self)
        exit_button.setShortcut('Ctrl+Q')
        exit_button.setStatusTip('Exit application')
        exit_button.triggered.connect(self.close)

        # About button
        about_button = QAction('About', self)
        about_button.triggered.connect(self.on_button_clicked)

        # Adding buttons to Menu
        help_menu.addAction(about_button)
        file_menu.addAction(exit_button)

        # Create URL entry buttons and entry textbox
        url_entry_label = QLabel('Enter URL:')
        self.url_entry_text = QLineEdit()
        self.url_entry_text.setPlaceholderText('https://invidio.us/watch?v=8SdPLG-_wtA')
        # set up callback to update video formats when URL is changed
        self.url_entry_text.textChanged.connect(self.reset_video_formats)

        # create output folder button and entry textbox
        output_folder_button = QPushButton('Select Output Folder')
        output_folder_button.setToolTip('Select output folder')
        output_folder_button.clicked.connect(self.update_output_folder)
        self.output_entry_combobox = QComboBox()
        self.output_entry_combobox.setEditable(True)

        for item in self.download_folder_list:
            # set default output folder to be downloads folder
            self.output_entry_combobox.addItem(item)
        # self.output_entry_combobox.editTextChanged[str].connect(self.download_text_changed)

        # add combobox for video download format and detect formats button
        detect_formats_label = QLabel('Download Format:')

        self.video_format_combobox = QComboBox()
        # set default values for format select combobox
        self.populate_video_format_combobox(self.default_formats_menu_items)
        self.video_format_combobox.activated[str].connect(self.video_format_change)

        # add download button
        download_button = QPushButton('Download')
        download_button.clicked.connect(self.download_video_callback)

        # create grid layout
        layout = QGridLayout()

        # add widgets to the layout
        layout.addWidget(url_entry_label, 1, 0)
        layout.addWidget(self.url_entry_text, 1, 1)
        layout.addWidget(output_folder_button, 2, 0)
        layout.addWidget(self.output_entry_combobox, 2, 1)
        layout.addWidget(detect_formats_label, 3, 0)
        layout.addWidget(self.video_format_combobox, 3, 1)
        layout.addWidget(download_button, 5, 0)

        # add grid layout as central widget for main window
        main_widget = QWidget()
        main_widget.setLayout(layout)
        self.setCentralWidget(main_widget)

        self.show()

    def on_button_clicked(self):
        """ button about """
        msg = QMessageBox()

        msg.setWindowTitle('About us')
        msg.setText("<p align='center'>Written with Python3 and PyQt5<br>"
                    "Version: %s <br> License: %s </p>" % (__version__, __license__))
        msg.setIcon(QMessageBox.Information)
        self.show()

        msg.exec_()

    def url_catch(self):
        ''' Return URL EntryText'''
        url = self.url_entry_text.text()
        url = str(url.strip())
        return url

    def url_check_valid(self):
        ''' Check valid URL '''
        if re.match(self._VALID_URL, self.url_catch()) is None:
            check = False
        else:
            check = True
        return check

    def download_video_callback(self):
        ''' Callback for the "Download Video" button
        '''

        def download_video_thread_helper(self, ydl_opts):
            '''Download the video. Meant to be called in a background daemon thread
            '''
            with hypervideo.YoutubeDL(ydl_opts) as ydl:
                ydl.download([self.url_catch()])

            self.statusBar().showMessage('Downloading Video... Done!', msecs=0)

        # make sure a valid output directory was entered
        if not os.path.isdir(self.output_entry_combobox.currentText()):
            self.statusBar().showMessage('Invalid download directory!')
            return

        # make sure the Download Folder List combobox is populated with the latest entry
        # this covers the case where the user uses the edittext portion of the combobox
        self.add_item_to_downloads_combobox(self.output_entry_combobox.currentText())

        # set output path/format
        outtmpl = os.path.join(self.output_entry_combobox.currentText(), r'%(title)s.%(ext)s')

        # create the youtube downloader options based on video format combobox selection
        if self.video_format_combobox.currentText() == self.default_formats_menu_items[0]:
            # download best video quality
            ydl_opts = {
                'format': 'bestvideo+bestaudio/best',
                'outtmpl': outtmpl,
            }
        elif self.video_format_combobox.currentText() == self.default_formats_menu_items[1]:
            # for downloading best audio and converting to mp3
            ydl_opts = {
                'format': 'bestaudio/best',
                'outtmpl': outtmpl,
                'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',  # not actually best quality...
                }],
            }
        else:
            # grab video format from the dropdown string:
            # ie. "135 - some video metadata here" -> "135"
            video_format = self.video_format_combobox.currentText()[0:self.video_format_combobox.currentText().find('-')-1]
            # set output path/format
            outformat = os.path.join(self.output_entry_combobox.currentText(),
                                     r'%(title)s.f%(format_id)s.%(ext)s')
            ydl_opts = {
                'format': video_format,
                'outtmpl': outformat,
            }

        # download the video in daemon thread
        if self.url_check_valid() is False:
            self.statusBar().showMessage('Please add a URL...')
        else:
            self.statusBar().showMessage('Downloading Video...')
            thread = threading.Thread(target=download_video_thread_helper, args=(self, ydl_opts, ))
            thread.daemon = True
            thread.start()

    def update_video_formats(self):
        '''Grabs the list of available video formats in background thread and populates
        video format combobox with results when complete.
        '''

        def get_video_formats_thread_helper(self, url):
            ''' Grabs the available video formats. Intended to be run as background thread.
            '''
            self.options = {
                'socket_timeout': 30,
                'format': 'best',
                'noplaylist': True,  # only download single song, not playlist
            }

            if self.url_check_valid() is True:
                try:
                    with hypervideo.YoutubeDL(self.options) as ydl:
                        meta = ydl.extract_info(url, download=False)
                        formats = meta.get('formats', [meta])
                except TypeError:
                    self.statusBar().showMessage('Problem downloading %s' % url)
                    return None

                # add formats to combobox
                if formats is None:
                    self.statusBar().showMessage('Formats not found')
                else:
                    item_list = self.default_formats_menu_items[0:2]
                    data_list = [f['format_id'] + ' - ' + f['ext'] for f in formats]
                    item_list.extend(data_list)
                    self.populate_video_format_combobox(item_list)
                    self.statusBar().showMessage('Finished Downloading Video Formats', msecs=0)
                    self.video_format_combobox.setCurrentIndex(0)

        # check if is valid url
        # should probably be reworked to be compatible with non-YouTube websites
        if self.url_check_valid() is False:
            self.populate_video_format_combobox(self.default_formats_menu_items)
            return
        else:
            # valid url - fetch the video formats in background daemon thread
            self.statusBar().showMessage('Downloading Video Formats')
            thread = threading.Thread(target=get_video_formats_thread_helper,
                                      args=(self, self.url_catch(), ))
            thread.daemon = True
            thread.start()

    def video_format_change(self, text):
        ''' Video Format Change '''
        if text == self.default_formats_menu_items[2]:
            # detect video formats was selected
            # update statusbar to let user know something is happening
            if self.url_check_valid() is False:
                self.statusBar().showMessage('Please add a URL...')
            else:
                # update video formats
                self.update_video_formats()

    def populate_video_format_combobox(self, labels):
        '''Populate the video format combobox with video formats. Clear the previous labels.
        labels {list} -- list of strings representing the video format combobox options
        '''
        self.video_format_combobox.clear()
        for label in labels:
            self.video_format_combobox.addItem(label)

    def reset_video_formats(self):
        ''' Clean video formast '''
        idx = self.video_format_combobox.currentIndex()

        self.populate_video_format_combobox(self.default_formats_menu_items)

        # preserve combobox index if possible
        if idx > 1:
            self.video_format_combobox.setCurrentIndex(0)
        else:
            self.video_format_combobox.setCurrentIndex(idx)

    @pyqtSlot()
    def update_output_folder(self):
        ''' Callback for "Update Output Folder" button. Allows user to select
        output directory via standard UI.
        https://stackoverflow.com/questions/43121340/why-is-the-use-of-lensequence-in-condition-values-considered-incorrect-by-pyli
        '''
        file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))

        if file:
            self.add_item_to_downloads_combobox(file)
        else:
            self.statusBar().showMessage('Select a folder!')

    def download_text_changed(self, text):
        ''' download text changed '''
        # function not used right now
        if text not in self.download_folder_list and os.path.isdir(text):
            self.add_item_to_downloads_combobox(text)

    def add_item_to_downloads_combobox(self, text):
        ''' Add item to download comboboc '''
        if text not in self.download_folder_list:
            # if it's not in the list, add it to the list
            self.download_folder_list = [text]+self.download_folder_list
        else:
            # if it's in the list already, move it to the top of the list
            self.download_folder_list = [text]+[folder for folder in self.download_folder_list if not folder == text]

        # maximum download folder history of 5
        if len(self.download_folder_list) > 5:
            self.download_folder_list = self.download_folder_list[0:6]

        # update the combobox
        self.output_entry_combobox.clear()
        for item in self.download_folder_list:
            self.output_entry_combobox.addItem(item)

        self.output_entry_combobox.setCurrentIndex(0)  # reset index - just in case

    def save_gui_state(self):
        ''' Save GUI State '''
        save_dict = {'DownloadFolderList': self.download_folder_list, }

        if not GUI_STATE_JSON_FILE:
            newf = open(GUI_STATE_JSON_FILE, "a+")
            newf.close()

        with open(GUI_STATE_JSON_FILE, 'w') as file:
            json.dump(save_dict, file)

    def load_gui_state(self):
        ''' Load GUI state '''
        if os.path.isfile(GUI_STATE_JSON_FILE):
            with open(GUI_STATE_JSON_FILE, 'r') as file:
                save_data = json.load(file)

            if isinstance(save_data['DownloadFolderList'], str):
                # convert to list
                save_data['DownloadFolderList'] = [save_data['DownloadFolderList']]

        else:
            save_data = {'DownloadFolderList': [get_default_download_path()], }

        return save_data

    def closeEvent(self, event):
        '''Protected Function for PyQt5
           gets called when the user closes the GUI.
           It saves the GUI state to the json file
           GUI_STATE_JSON_FILE.
        '''
        self.save_gui_state()

        close = QMessageBox()
        close.setIcon(QMessageBox.Question)
        close.setWindowTitle('Exit')
        close.setText('You sure?')
        close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
        close = close.exec()

        if close == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


def get_default_download_path():
    """Returns the path for the "Downloads" folder in GNU
    Used as default directory for videos to be saved to.
    #From: https://stackoverflow.com/questions/35851281/python-finding-the-users-downloads-folder
    """
    if os.name == 'posix':
        dpath = '/tmp/hypervideo-gui/'
        if not os.path.exists(dpath):
            os.makedirs(dpath)
        return os.path.join(dpath)

if __name__ == '__main__':
    APP = QApplication(sys.argv)
    APP.setStyle('Fusion')
    ex = App()
    sys.exit(APP.exec_())