aboutsummaryrefslogtreecommitdiffstats
path: root/src/view/app.py
blob: 98c784278bdf2cdf3fc9d7feb6c33a69e36f177c (plain) (blame)
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
#! /usr/bin/python3

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy import platform

import threading

from model.youload_playlist import YouLoadPlayList
from view.file_chooser import YouloadFileChooser
from view.download_type_chooser import YouloadDownloadTypeChooser
import util

class YouloadApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')

        # Data members
        self.url = "https://youtube.com/playlist?list=PLXbRgJ9vx_KuA5CljV3BpiV58qx6znmRk&si=JFaVyspkeoMWUDNL"
        self.is_downloading = False
        self.should_stop_download = False

        # File chooser.
        self.file_chooser = YouloadFileChooser()
        self.file_chooser.set_app(self)

        # Download type chooser.
        self.download_type_chooser = YouloadDownloadTypeChooser()
        self.download_type_chooser.set_app(self)

        # Url input.
        url_input = TextInput(text=self.url, multiline=False, size_hint=(0.8, 1.0))
        url_input.bind(text=self.uid_url_input)

        # Submit button.
        self.submit = Button(text="Download", size_hint=(0.2, 1.0))
        self.submit.bind(on_press=self.submit_cb)

        # Url and submit layout.
        url_and_submit = BoxLayout(size_hint=(1.0, 0.1))
        url_and_submit.add_widget(url_input)
        url_and_submit.add_widget(self.submit)

        # Download status.
        self.download_status = Label(text="Nothing downloading", size_hint=(0.3, 1.0))

        # Download type chooser button.
        self.download_type_button = Button(text=self.download_type_chooser.current_type, size_hint=(0.1, 1.0))
        self.download_type_button.bind(on_press=self.download_type_chooser.open)

        # Folder display.
        self.folder_display = Label(text=util.get_default_download_dir(), size_hint=(0.4, 1.0))

        # choose folder button.
        self.choose_folder_button = Button(text="Folder", size_hint=(0.2, 1.0))
        self.choose_folder_button.bind(on_press=self.choose_folder_cb)

        # Info line.
        info_line = BoxLayout(size_hint=(1.0, 0.1))
        info_line.add_widget(self.download_status)
        info_line.add_widget(self.download_type_button)
        info_line.add_widget(self.folder_display)
        info_line.add_widget(self.choose_folder_button)

        # Info display.
        self.downloads_display = Label(text="", size_hint=(1.0, 0.8))

        # Everything else (:
        layout.add_widget(url_and_submit)
        layout.add_widget(info_line)
        layout.add_widget(self.downloads_display)

        return layout

    def on_start(self, **kwargs):

        if platform == "android":
            from android.permissions import request_permissions, Permission
            request_permissions([Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE])

    def uid_url_input(self, instance, value):
        self.url = value

    # Download the videos in a different thread so the ui still works.
    def download_playlist_thread(self):
        try:
            self.download_status.text = "Fetching playlist"
            self.submit.text = "Stop"
            self.is_downloading = True

            # Get playlist.
            playlist = YouLoadPlayList(self.url)

            # Set directory and other stuff.
            playlist.set_download_directory(self.folder_display.text)
            playlist.download_type = self.download_type_chooser.current_type
            playlist.prepare_for_download()
            self.downloads_display.text = f"Downloading to {playlist.folder_name}\n"

            # Download each video
            for i in range(playlist.video_count):
                # Stop this mother fucker
                if self.should_stop_download:
                    break

                self.download_status.text = f"Downloading {i+1}/{playlist.video_count}"
                self.downloads_display.text += playlist.download_video(i) + "\n"

            # Complete download.
            self.download_status.text = "Download complete"
        except FileExistsError:
            self.download_status.text = "Folder already exists"
        except KeyError:
            self.download_status.text = "Error getting playlist"
        except AttributeError:
            self.download_status.text = "Download type not supported"
        except Exception as e:
            self.downloads_display.text = repr(e)

        self.submit.text = "Download"
        self.is_downloading = False

    def stop_download(self):
        self.download_status.text = "Stopping download"
        self.should_stop_download = True

    def submit_cb(self, instance):
        # Is already downloading something.
        if self.is_downloading:
            self.stop_download()
            return

        # Start download thread.
        download_thread = threading.Thread(target=self.download_playlist_thread)
        self.should_stop_download = False
        download_thread.start()

    def choose_folder_cb(self, instance):
        self.file_chooser.open()

    def on_stop(self):
        self.stop_download()