Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,10 @@ import os
|
|
| 5 |
import urllib
|
| 6 |
import base64
|
| 7 |
|
|
|
|
|
|
|
| 8 |
def download_file(url, local_filename):
|
| 9 |
-
if url.startswith('http://') or url.startswith('https://'):
|
| 10 |
try:
|
| 11 |
with requests.get(url, stream=True) as r:
|
| 12 |
r.raise_for_status()
|
|
@@ -15,8 +17,7 @@ def download_file(url, local_filename):
|
|
| 15 |
f.write(chunk)
|
| 16 |
return local_filename
|
| 17 |
except requests.exceptions.HTTPError as err:
|
| 18 |
-
print(f"HTTP error occurred: {err}")
|
| 19 |
-
|
| 20 |
|
| 21 |
def download_html_and_files(url):
|
| 22 |
html_content = requests.get(url).text
|
|
@@ -27,25 +28,16 @@ def download_html_and_files(url):
|
|
| 27 |
for link in soup.find_all('a'):
|
| 28 |
file_url = urllib.parse.urljoin(base_url, link.get('href'))
|
| 29 |
local_filename = urllib.parse.urlparse(file_url).path.split('/')[-1]
|
| 30 |
-
if local_filename:
|
| 31 |
link['href'] = local_filename
|
| 32 |
download_file(file_url, local_filename)
|
| 33 |
|
| 34 |
with open("index.html", "w") as file:
|
| 35 |
file.write(str(soup))
|
| 36 |
|
| 37 |
-
|
| 38 |
def list_files(directory_path='.'):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def main():
|
| 42 |
-
st.sidebar.title('Bulk Download Tool')
|
| 43 |
-
url = st.sidebar.text_input('Please enter a URL to bulk download text and files')
|
| 44 |
-
if st.sidebar.button('📥 Get All the Content'):
|
| 45 |
-
download_html_and_files(url)
|
| 46 |
-
st.sidebar.write('Download complete. Here are the files you can download:')
|
| 47 |
-
for file in list_files():
|
| 48 |
-
st.sidebar.markdown(get_download_link(file), unsafe_allow_html=True)
|
| 49 |
|
| 50 |
def get_download_link(file):
|
| 51 |
with open(file, "rb") as f:
|
|
@@ -54,5 +46,19 @@ def get_download_link(file):
|
|
| 54 |
href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{file}\'>Click to download {file}</a>'
|
| 55 |
return href
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
main()
|
|
|
|
| 5 |
import urllib
|
| 6 |
import base64
|
| 7 |
|
| 8 |
+
EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md']
|
| 9 |
+
|
| 10 |
def download_file(url, local_filename):
|
| 11 |
+
if url.startswith('http://') or url.startswith('https://'):
|
| 12 |
try:
|
| 13 |
with requests.get(url, stream=True) as r:
|
| 14 |
r.raise_for_status()
|
|
|
|
| 17 |
f.write(chunk)
|
| 18 |
return local_filename
|
| 19 |
except requests.exceptions.HTTPError as err:
|
| 20 |
+
print(f"HTTP error occurred: {err}")
|
|
|
|
| 21 |
|
| 22 |
def download_html_and_files(url):
|
| 23 |
html_content = requests.get(url).text
|
|
|
|
| 28 |
for link in soup.find_all('a'):
|
| 29 |
file_url = urllib.parse.urljoin(base_url, link.get('href'))
|
| 30 |
local_filename = urllib.parse.urlparse(file_url).path.split('/')[-1]
|
| 31 |
+
if local_filename:
|
| 32 |
link['href'] = local_filename
|
| 33 |
download_file(file_url, local_filename)
|
| 34 |
|
| 35 |
with open("index.html", "w") as file:
|
| 36 |
file.write(str(soup))
|
| 37 |
|
|
|
|
| 38 |
def list_files(directory_path='.'):
|
| 39 |
+
files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
|
| 40 |
+
return [f for f in files if f not in EXCLUDED_FILES]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def get_download_link(file):
|
| 43 |
with open(file, "rb") as f:
|
|
|
|
| 46 |
href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{file}\'>Click to download {file}</a>'
|
| 47 |
return href
|
| 48 |
|
| 49 |
+
def show_download_links():
|
| 50 |
+
st.sidebar.write('Here are the files you can download:')
|
| 51 |
+
for file in list_files():
|
| 52 |
+
st.sidebar.markdown(get_download_link(file), unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
def main():
|
| 55 |
+
st.sidebar.title('Bulk Download Tool')
|
| 56 |
+
url = st.sidebar.text_input('Please enter a URL to bulk download text and files')
|
| 57 |
+
if st.sidebar.button('📥 Get All the Content'):
|
| 58 |
+
download_html_and_files(url)
|
| 59 |
+
show_download_links()
|
| 60 |
+
if st.sidebar.button('📂 Show Download Links'):
|
| 61 |
+
show_download_links()
|
| 62 |
+
|
| 63 |
if __name__ == "__main__":
|
| 64 |
main()
|