Nymbo commited on
Commit
20a0bbe
·
verified ·
1 Parent(s): d48d430

temp folder strat

Browse files
Files changed (1) hide show
  1. app.py +38 -30
app.py CHANGED
@@ -1,42 +1,50 @@
1
  import gradio as gr
2
  import os
3
  import subprocess
4
- import tempfile
5
  import shutil
6
  from zipfile import ZipFile
7
 
 
 
 
8
  def fetch(urls, ext):
9
- # Create a temporary directory to store downloaded files
10
- with tempfile.TemporaryDirectory() as temp_dir:
11
- downloaded_files = []
12
- url_list = urls.splitlines() # Split multiple URLs by line
13
-
14
- for i, url in enumerate(url_list):
15
- filename = f"file_{i+1}.{ext}" # Generate a generic filename for each URL
16
- file_path = os.path.join(temp_dir, filename)
17
-
18
- opts = {
19
- "mp3": "-f \"ba\" -x --audio-format mp3",
20
- "mp4": "-f \"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best\"",
21
- "flac": "-f \"ba\" -x --audio-format flac",
22
- "aac": "-f \"ba\" -x --audio-format aac"
23
- }[ext]
24
-
25
- try:
26
- # Run yt-dlp command using subprocess
27
- subprocess.run(f"yt-dlp {opts} {url} -o \"{file_path}\"", shell=True, check=True)
28
- downloaded_files.append(file_path)
29
- except subprocess.CalledProcessError as e:
30
- return f"Error downloading file {url}: {str(e)}"
31
 
32
- # Create a zip file with all downloaded files
33
- zip_filename = os.path.join(temp_dir, "downloaded_files.zip")
34
- with ZipFile(zip_filename, 'w') as zipf:
35
- for file in downloaded_files:
36
- zipf.write(file, os.path.basename(file)) # Add file to zip, with filename
 
37
 
38
- # Return the path to the zip file for downloading
39
- return zip_filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Create the Gradio interface
42
  interface = gr.Interface(
 
1
  import gradio as gr
2
  import os
3
  import subprocess
 
4
  import shutil
5
  from zipfile import ZipFile
6
 
7
+ # A folder to store downloads in a Hugging Face environment
8
+ DOWNLOAD_FOLDER = "downloads"
9
+
10
  def fetch(urls, ext):
11
+ # Ensure the download folder exists
12
+ if not os.path.exists(DOWNLOAD_FOLDER):
13
+ os.makedirs(DOWNLOAD_FOLDER)
14
+
15
+ downloaded_files = []
16
+ url_list = urls.splitlines() # Split multiple URLs by line
17
+
18
+ for i, url in enumerate(url_list):
19
+ filename = f"file_{i+1}.{ext}" # Generate a generic filename for each URL
20
+ file_path = os.path.join(DOWNLOAD_FOLDER, filename)
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ opts = {
23
+ "mp3": "-f \"ba\" -x --audio-format mp3",
24
+ "mp4": "-f \"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best\"",
25
+ "flac": "-f \"ba\" -x --audio-format flac",
26
+ "aac": "-f \"ba\" -x --audio-format aac"
27
+ }[ext]
28
 
29
+ try:
30
+ # Run yt-dlp command using subprocess
31
+ subprocess.run(f"yt-dlp {opts} {url} -o \"{file_path}\"", shell=True, check=True)
32
+ downloaded_files.append(file_path)
33
+ except subprocess.CalledProcessError as e:
34
+ return f"Error downloading file {url}: {str(e)}"
35
+
36
+ # Create a zip file with all downloaded files
37
+ zip_filename = os.path.join(DOWNLOAD_FOLDER, "downloaded_files.zip")
38
+ with ZipFile(zip_filename, 'w') as zipf:
39
+ for file in downloaded_files:
40
+ zipf.write(file, os.path.basename(file)) # Add file to zip, with filename
41
+
42
+ # Cleanup individual files after zipping
43
+ for file in downloaded_files:
44
+ os.remove(file)
45
+
46
+ # Return the path to the zip file for downloading
47
+ return zip_filename
48
 
49
  # Create the Gradio interface
50
  interface = gr.Interface(