Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -29,42 +29,29 @@ if not os.path.exists("history.json"):
|
|
29 |
with open("history.json", "w") as f:
|
30 |
json.dump({}, f)
|
31 |
|
32 |
-
|
33 |
-
import base64
|
34 |
-
import zipfile
|
35 |
-
import streamlit as st
|
36 |
-
|
37 |
-
def zip_subdirs(start_dir):
|
38 |
for subdir, dirs, files in os.walk(start_dir):
|
39 |
if subdir != start_dir: # Skip the root directory
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
# Convert size to a more readable format (e.g., KB, MB)
|
55 |
-
if zip_size < 1024:
|
56 |
-
size_str = f"{zip_size} B"
|
57 |
-
elif zip_size < 1024**2:
|
58 |
-
size_str = f"{zip_size / 1024:.2f} KB"
|
59 |
-
else:
|
60 |
-
size_str = f"{zip_size / 1024**2:.2f} MB"
|
61 |
-
|
62 |
# Create the download link
|
63 |
with open(zip_file, 'rb') as f:
|
64 |
bytes = f.read()
|
65 |
b64 = base64.b64encode(bytes).decode()
|
66 |
-
link_name = os.path.basename(
|
67 |
-
href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}">{link_name} - {num_files} files, {size_str}</a>'
|
68 |
return href
|
69 |
|
70 |
@st.cache_resource
|
@@ -251,12 +238,20 @@ def main():
|
|
251 |
os.remove(file)
|
252 |
st.experimental_rerun()
|
253 |
|
|
|
254 |
if st.sidebar.button("⬇️ Download All"):
|
255 |
start_directory = '.' # Current directory
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
# Expander for showing URL history and download links
|
261 |
with st.expander("URL History and Downloaded Files"):
|
262 |
try:
|
|
|
29 |
with open("history.json", "w") as f:
|
30 |
json.dump({}, f)
|
31 |
|
32 |
+
def zip_subdirs(start_dir, history):
|
|
|
|
|
|
|
|
|
|
|
33 |
for subdir, dirs, files in os.walk(start_dir):
|
34 |
if subdir != start_dir: # Skip the root directory
|
35 |
+
# Find the URL associated with the subdir in history
|
36 |
+
url_key = next((url for url, path in history.items() if path == subdir), None)
|
37 |
+
if url_key:
|
38 |
+
# Use a sanitized version of the URL as the zip filename
|
39 |
+
sanitized_url = hashlib.md5(url_key.encode()).hexdigest()
|
40 |
+
zip_filename = os.path.join(start_dir, sanitized_url + '.zip')
|
41 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
42 |
+
for file in files:
|
43 |
+
file_path = os.path.join(subdir, file)
|
44 |
+
zipf.write(file_path, os.path.relpath(file_path, start_dir))
|
45 |
+
st.write(f"Added: {file_path}")
|
46 |
+
yield zip_filename, url_key
|
47 |
+
|
48 |
+
def get_zip_download_link(zip_file, url, num_files, size_str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# Create the download link
|
50 |
with open(zip_file, 'rb') as f:
|
51 |
bytes = f.read()
|
52 |
b64 = base64.b64encode(bytes).decode()
|
53 |
+
link_name = os.path.basename(url) # Use URL as the link name
|
54 |
+
href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}.zip">{link_name} - {num_files} files, {size_str}</a>'
|
55 |
return href
|
56 |
|
57 |
@st.cache_resource
|
|
|
238 |
os.remove(file)
|
239 |
st.experimental_rerun()
|
240 |
|
241 |
+
|
242 |
if st.sidebar.button("⬇️ Download All"):
|
243 |
start_directory = '.' # Current directory
|
244 |
+
with open("history.json", "r") as f:
|
245 |
+
history = json.load(f)
|
246 |
+
for zip_file, url in zip_subdirs(start_directory, history):
|
247 |
+
# Get file count and size info
|
248 |
+
with zipfile.ZipFile(zip_file, 'r') as zipf:
|
249 |
+
num_files = len(zipf.namelist())
|
250 |
+
zip_size = os.path.getsize(zip_file)
|
251 |
+
size_str = f"{zip_size / 1024**2:.2f} MB" if zip_size >= 1024**2 else f"{zip_size / 1024:.2f} KB"
|
252 |
+
# Display enhanced download link with URL, file count, and size
|
253 |
+
st.sidebar.markdown(get_zip_download_link(zip_file, url, num_files, size_str), unsafe_allow_html=True)
|
254 |
+
|
255 |
# Expander for showing URL history and download links
|
256 |
with st.expander("URL History and Downloaded Files"):
|
257 |
try:
|