Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,11 @@ from bs4 import BeautifulSoup
|
|
| 7 |
import hashlib
|
| 8 |
import json
|
| 9 |
import mimetypes
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md','.gitattributes', "backup.py","Dockerfile"]
|
| 12 |
|
| 13 |
-
# Create a history.json file if it doesn't exist yet
|
| 14 |
if not os.path.exists("history.json"):
|
| 15 |
with open("history.json", "w") as f:
|
| 16 |
json.dump({}, f)
|
|
@@ -36,12 +37,10 @@ def download_html_and_files(url, subdir):
|
|
| 36 |
file_url = urllib.parse.urljoin(base_url, link.get('href'))
|
| 37 |
local_filename = os.path.join(subdir, urllib.parse.urlparse(file_url).path.split('/')[-1])
|
| 38 |
|
| 39 |
-
# Skip if the local filename is a directory
|
| 40 |
if not local_filename.endswith('/') and local_filename != subdir:
|
| 41 |
link['href'] = local_filename
|
| 42 |
download_file(file_url, local_filename)
|
| 43 |
|
| 44 |
-
# Save the modified HTML content
|
| 45 |
with open(os.path.join(subdir, "index.html"), "w") as file:
|
| 46 |
file.write(str(soup))
|
| 47 |
|
|
@@ -56,123 +55,35 @@ def get_download_link(file):
|
|
| 56 |
href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
|
| 57 |
return href
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
if is_text_file(file_path):
|
| 81 |
-
file_content = ''
|
| 82 |
-
if 'edit_content' in st.session_state and st.session_state['edit_content'][0] == file_path:
|
| 83 |
-
file_content = st.session_state['edit_content'][1]
|
| 84 |
-
else:
|
| 85 |
-
with open(file_path, "r", encoding='utf-8') as f:
|
| 86 |
-
file_content = f.read()
|
| 87 |
-
|
| 88 |
-
edited_content = st.text_area(f"Edit {os.path.basename(file_path)}:", value=file_content, height=250, key=f"textarea_{file_path}")
|
| 89 |
-
if st.button("💾 Save", key=f"save_{file_path}"):
|
| 90 |
-
with open(file_path, "w", encoding='utf-8') as f:
|
| 91 |
-
f.write(edited_content)
|
| 92 |
-
st.success(f"File {os.path.basename(file_path)} saved!")
|
| 93 |
-
st.session_state['edit_content'] = (file_path, edited_content)
|
| 94 |
-
else:
|
| 95 |
-
st.info(f"This is a binary file ({os.path.basename(file_path)}) and cannot be edited.")
|
| 96 |
-
|
| 97 |
-
# Column 2: Download link
|
| 98 |
-
with col2:
|
| 99 |
-
st.markdown("Download")
|
| 100 |
-
st.markdown(get_download_link(file_path), unsafe_allow_html=True)
|
| 101 |
-
|
| 102 |
-
# Column 3: Delete button
|
| 103 |
-
with col3:
|
| 104 |
-
if st.button(f"🗑️ Delete", key=f"delete_{file_path}"):
|
| 105 |
-
os.remove(file_path)
|
| 106 |
-
st.success(f"File {os.path.basename(file_path)} deleted!")
|
| 107 |
-
# Update the listing by removing the deleted file
|
| 108 |
-
st.experimental_rerun()
|
| 109 |
-
|
| 110 |
-
def show_download_links(subdir):
|
| 111 |
-
st.write(f'Files in {subdir}:')
|
| 112 |
-
for file in list_files(subdir):
|
| 113 |
-
file_path = os.path.join(subdir, file)
|
| 114 |
-
if os.path.isfile(file_path):
|
| 115 |
-
file_column(file_path)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
def generate_hash_key(path, counter):
|
| 120 |
-
"""Generate a unique hash key for a given file path and counter."""
|
| 121 |
-
return hashlib.md5(f"{path}_{counter}".encode()).hexdigest()
|
| 122 |
-
|
| 123 |
-
def show_file_operations(file_path):
|
| 124 |
-
# Increment counter for each file path
|
| 125 |
-
counter_key = f"counter_{file_path}"
|
| 126 |
-
if counter_key in st.session_state:
|
| 127 |
-
st.session_state[counter_key] += 1
|
| 128 |
-
else:
|
| 129 |
-
st.session_state[counter_key] = 1
|
| 130 |
-
|
| 131 |
-
# Unique hash keys for each file and operation based on their path and counter
|
| 132 |
-
counter = st.session_state[counter_key]
|
| 133 |
-
edit_button_key = f"edit_button_{generate_hash_key(file_path, counter)}"
|
| 134 |
-
save_button_key = f"save_button_{generate_hash_key(file_path, counter)}"
|
| 135 |
-
delete_button_key = f"delete_button_{generate_hash_key(file_path, counter)}"
|
| 136 |
-
content_key = f"content_{generate_hash_key(file_path, counter)}"
|
| 137 |
-
|
| 138 |
-
# Start Edit operation
|
| 139 |
-
if st.button(f"✏️ Edit {os.path.basename(file_path)}", key=edit_button_key):
|
| 140 |
-
if edit_button_key not in st.session_state:
|
| 141 |
-
with open(file_path, "r") as f:
|
| 142 |
-
st.session_state[content_key] = f.read()
|
| 143 |
-
st.session_state[edit_button_key] = True
|
| 144 |
-
|
| 145 |
-
# Display text area for editing if in edit mode
|
| 146 |
-
if st.session_state.get(edit_button_key, False):
|
| 147 |
-
edited_content = st.text_area("Edit the file content:", value=st.session_state.get(content_key, ""), height=250, key=content_key)
|
| 148 |
-
|
| 149 |
-
# Save button
|
| 150 |
-
if st.button(f"💾 Save {os.path.basename(file_path)}", key=save_button_key):
|
| 151 |
-
with open(file_path, "w") as f:
|
| 152 |
-
f.write(edited_content)
|
| 153 |
-
new_file_size = os.path.getsize(file_path)
|
| 154 |
-
download_link = get_download_link(file_path)
|
| 155 |
-
st.markdown(f"✅ File **{os.path.basename(file_path)}** saved! ([{download_link}]) - New size: {new_file_size} bytes", unsafe_allow_html=True)
|
| 156 |
-
st.session_state[edit_button_key] = False # Exit edit mode
|
| 157 |
-
|
| 158 |
-
# Delete button
|
| 159 |
-
if st.button(f"🗑️ Delete {os.path.basename(file_path)}", key=delete_button_key):
|
| 160 |
-
os.remove(file_path)
|
| 161 |
-
st.markdown(f"🎉 File {os.path.basename(file_path)} deleted!")
|
| 162 |
-
# Remove state variables related to the deleted file
|
| 163 |
-
st.session_state.pop(edit_button_key, None)
|
| 164 |
-
st.session_state.pop(content_key, None)
|
| 165 |
-
|
| 166 |
|
| 167 |
def main():
|
| 168 |
st.sidebar.title('Web Datasets Bulk Downloader')
|
| 169 |
url = st.sidebar.text_input('Please enter a Web URL to bulk download text and files')
|
| 170 |
|
| 171 |
-
# Load history
|
| 172 |
with open("history.json", "r") as f:
|
| 173 |
history = json.load(f)
|
| 174 |
|
| 175 |
-
# Save the history of URL entered as a json file
|
| 176 |
if url:
|
| 177 |
subdir = hashlib.md5(url.encode()).hexdigest()
|
| 178 |
if not os.path.exists(subdir):
|
|
@@ -190,11 +101,13 @@ def main():
|
|
| 190 |
for subdir in history.values():
|
| 191 |
show_download_links(subdir)
|
| 192 |
|
| 193 |
-
# Display history as markdown
|
| 194 |
with st.expander("URL History and Downloaded Files"):
|
| 195 |
for url, subdir in history.items():
|
| 196 |
st.markdown(f"#### {url}")
|
| 197 |
show_download_links(subdir)
|
| 198 |
|
| 199 |
-
if
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import hashlib
|
| 8 |
import json
|
| 9 |
import mimetypes
|
| 10 |
+
import shutil
|
| 11 |
+
from zipfile import ZipFile
|
| 12 |
|
| 13 |
+
EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md', '.gitattributes', "backup.py", "Dockerfile"]
|
| 14 |
|
|
|
|
| 15 |
if not os.path.exists("history.json"):
|
| 16 |
with open("history.json", "w") as f:
|
| 17 |
json.dump({}, f)
|
|
|
|
| 37 |
file_url = urllib.parse.urljoin(base_url, link.get('href'))
|
| 38 |
local_filename = os.path.join(subdir, urllib.parse.urlparse(file_url).path.split('/')[-1])
|
| 39 |
|
|
|
|
| 40 |
if not local_filename.endswith('/') and local_filename != subdir:
|
| 41 |
link['href'] = local_filename
|
| 42 |
download_file(file_url, local_filename)
|
| 43 |
|
|
|
|
| 44 |
with open(os.path.join(subdir, "index.html"), "w") as file:
|
| 45 |
file.write(str(soup))
|
| 46 |
|
|
|
|
| 55 |
href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
|
| 56 |
return href
|
| 57 |
|
| 58 |
+
def delete_all_files():
|
| 59 |
+
for root, dirs, files in os.walk(".", topdown=False):
|
| 60 |
+
for name in files:
|
| 61 |
+
if name not in EXCLUDED_FILES:
|
| 62 |
+
os.remove(os.path.join(root, name))
|
| 63 |
+
for name in dirs:
|
| 64 |
+
shutil.rmtree(os.path.join(root, name))
|
| 65 |
+
st.success("All files and folders deleted successfully!")
|
| 66 |
+
|
| 67 |
+
def create_zip_and_get_link():
|
| 68 |
+
zip_filename = "all_files.zip"
|
| 69 |
+
with ZipFile(zip_filename, 'w') as zipf:
|
| 70 |
+
for root, dirs, files in os.walk(".", topdown=False):
|
| 71 |
+
for file in files:
|
| 72 |
+
if file not in EXCLUDED_FILES and file != zip_filename:
|
| 73 |
+
zipf.write(os.path.join(root, file))
|
| 74 |
+
with open(zip_filename, "rb") as f:
|
| 75 |
+
bytes = f.read()
|
| 76 |
+
b64 = base64.b64encode(bytes).decode()
|
| 77 |
+
href = f'<a href="data:file/zip;base64,{b64}" download=\'{zip_filename}\'>🔽 Download All Files</a>'
|
| 78 |
+
st.markdown(href, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def main():
|
| 81 |
st.sidebar.title('Web Datasets Bulk Downloader')
|
| 82 |
url = st.sidebar.text_input('Please enter a Web URL to bulk download text and files')
|
| 83 |
|
|
|
|
| 84 |
with open("history.json", "r") as f:
|
| 85 |
history = json.load(f)
|
| 86 |
|
|
|
|
| 87 |
if url:
|
| 88 |
subdir = hashlib.md5(url.encode()).hexdigest()
|
| 89 |
if not os.path.exists(subdir):
|
|
|
|
| 101 |
for subdir in history.values():
|
| 102 |
show_download_links(subdir)
|
| 103 |
|
|
|
|
| 104 |
with st.expander("URL History and Downloaded Files"):
|
| 105 |
for url, subdir in history.items():
|
| 106 |
st.markdown(f"#### {url}")
|
| 107 |
show_download_links(subdir)
|
| 108 |
|
| 109 |
+
if st.sidebar.button('🗑️ Delete All'):
|
| 110 |
+
delete_all_files()
|
| 111 |
+
|
| 112 |
+
if st.sidebar.button('📦 Download All'):
|
| 113 |
+
create_zip_and_get_link
|