File size: 2,678 Bytes
7fbaf23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from pytube import YouTube

hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
.stApp{
background-image: linear-gradient(115deg,#FFAF00,#FFC500,#FFD600,#FCED00,#F9F380,#F6F7CD);
  animation: rotate 7s linear infinite;
}
@keyframes rotate {
  100%{
    filter: hue-rotate(-360deg)
  }
}
</style>
"""

# JavaScript code for confetti animation
confetti_animation_script = """
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script>
<script>
    function triggerConfetti() {
        confetti({
            angle: randomInRange(55, 125),
            particleCount: randomInRange(50, 100),
            origin: { y: 0.6 },
            spread: randomInRange(50, 70),
        });
    }

    function randomInRange(min, max) {
        return Math.random() * (max - min) + min;
    }
</script>
"""

st.markdown(hide_streamlit_style, unsafe_allow_html=True)

class YouTubeDownloader:
    @staticmethod
    def run():
        st.header("")
        url = st.text_input("Enter Here")
        start_button = st.button("Start")

        if start_button:
            if url:
                YouTubeDownloader.validate_url(url)
                with st.expander("preview video"):
                    st.video(url)
                YouTubeDownloader.cleanup()
                file_ = YouTubeDownloader.download_video(url)
                st.video(file_)
                YouTubeDownloader.helper_message()
                st.markdown(confetti_animation_script, unsafe_allow_html=True)  # Include confetti script

        st.markdown("")

    @staticmethod
    def download_video(url):
        with st.spinner("Downloading..."):
            local_file = (
                YouTube(url)
                .streams.filter(progressive=True, file_extension="mp4")
                .first()
                .download()
            )
            st.success("Downloaded")
        return local_file

    @staticmethod
    def validate_url(url):
        import validators

        if not validators.url(url):
            st.error("Hi there 👋 URL seems invalid 👽")
            st.stop()

    @classmethod
    def cleanup(cls):
        import pathlib
        import glob

        junks = glob.glob("*.mp4")
        for junk in junks:
            pathlib.Path(junk).unlink()

    @classmethod
    def helper_message(cls):
        st.write(
            "> To save  to the local computer, "
            "click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
        )


if __name__ == "__main__":
    YouTubeDownloader.run()