aboutsummaryrefslogtreecommitdiffstats
path: root/src/youload_app.py
blob: e28411682a4206ea1ca19bbc1b0bf73756c91bbb (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
#! /usr/bin/python3

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.filechooser import FileChooser

import threading

import pytube

from youload_playlist import YouLoadPlayList

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

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

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

        # Submit button.
        self.submit = Button(text="Download", size_hint=(0.25, 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")

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

        # 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 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):
        self.download_status.text = "Fetching playlist"
        self.submit.text = "Stop"
        self.is_downloading = True

        # Get playlist.
        try:
            playlist = YouLoadPlayList(self.url)
        except KeyError:
            self.download_status.text = "Error getting playlist"
            return

        playlist.prepare_for_download()
        self.downloads_display.text = f"Downloading {self.url}\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"

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

    def stop_download(self):
        self.should_stop_download = True

    def submit_cb(self, instance):
        # Is already downloading something.
        if self.is_downloading:
            self.download_status.text = "Stopping download"
            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 on_stop(self):
        self.stop_download()