Adapting commited on
Commit
9c676f9
1 Parent(s): 092a5eb

Upload 6 files

Browse files
Files changed (6) hide show
  1. app.py +32 -0
  2. requirements.txt +3 -0
  3. tube/__init__.py +3 -0
  4. tube/download.py +48 -0
  5. tube/utils.py +36 -0
  6. tube/var.py +4 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tube as tb
3
+
4
+ tb.clear_cache()
5
+
6
+
7
+ md = '''
8
+ # YouTube Downloader
9
+ '''
10
+
11
+ st.markdown(md)
12
+
13
+
14
+ url = st.text_input(
15
+ placeholder="https://www.youtube.com/",
16
+ label='**Enter the url of the youtube:**',
17
+ key='title'
18
+ )
19
+
20
+
21
+
22
+ if url is not None and ('https' in url or 'http' in url):
23
+ tb.download_yt(url)
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ pytube==12.1.2
3
+
tube/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .download import download_yt
2
+ from .utils import clear_cache
3
+ from .var import OUTPUT_DIR
tube/download.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from pytube import YouTube
3
+
4
+ import streamlit as st
5
+ from .utils import clear_cache
6
+
7
+
8
+
9
+
10
+ def download_yt(yt_url:str, output_dir:str = './downloads'):
11
+ yt = YouTube(yt_url)
12
+
13
+ prompt = st.markdown(f'''`downloading...`''')
14
+
15
+ while True:
16
+ try:
17
+ yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(
18
+ output_path=output_dir,
19
+ filename='download.mp4'
20
+ )
21
+ prompt.empty()
22
+ break
23
+ except Exception as e:
24
+ print(e)
25
+
26
+ download_file(folder_name= output_dir)
27
+
28
+
29
+
30
+
31
+
32
+ def download_file(folder_name):
33
+ def tmp(*,folder_name:str):
34
+ st.session_state["title"] = ""
35
+ clear_cache(folder_name)
36
+
37
+
38
+ with open(Path('downloads').joinpath('download.mp4'), "rb") as file:
39
+ btn = st.download_button(
40
+ label="Download",
41
+ data=file,
42
+ file_name='download.mp4',
43
+ on_click= tmp,kwargs=dict(
44
+ folder_name = folder_name
45
+ )
46
+ )
47
+
48
+
tube/utils.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ import streamlit as st
3
+ from pathlib import Path
4
+ from .var import OUTPUT_DIR
5
+
6
+
7
+
8
+
9
+ def compress_folder_2_zip(output_filename: str, dir_name:str):
10
+ path = Path(output_filename+'.zip')
11
+ if path.exists():
12
+ return
13
+
14
+ prompt = st.info('Start compressing...')
15
+ with st.spinner("Compressing"):
16
+ shutil.make_archive(output_filename.replace('.zip', ''), 'zip', dir_name)
17
+ prompt.empty()
18
+
19
+
20
+ def remove_dir_rec(pth):
21
+ pth = Path(pth)
22
+ if pth.exists():
23
+ for child in pth.glob('*'):
24
+ if child.is_file():
25
+ child.unlink()
26
+ else:
27
+ remove_dir_rec(child)
28
+ pth.rmdir()
29
+ def clear_cache(dir_name:str = OUTPUT_DIR):
30
+ remove_dir_rec(dir_name)
31
+
32
+
33
+
34
+
35
+ if __name__ == '__main__':
36
+ compress_folder_2_zip('test',dir_name='../downloads')
tube/var.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ PROJECT_ROOT = Path(__package__).absolute().parent
4
+ OUTPUT_DIR = Path(PROJECT_ROOT).joinpath('downloads')