Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
import os
|
5 |
+
import urllib
|
6 |
+
import base64
|
7 |
+
|
8 |
+
EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md','.gitattributes', "backup.py","Dockerfile"]
|
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()
|
15 |
+
with open(local_filename, 'wb') as f:
|
16 |
+
for chunk in r.iter_content(chunk_size=8192):
|
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
|
24 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
25 |
+
|
26 |
+
base_url = urllib.parse.urlunparse(urllib.parse.urlparse(url)._replace(path='', params='', query='', fragment=''))
|
27 |
+
|
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:
|
44 |
+
bytes = f.read()
|
45 |
+
b64 = base64.b64encode(bytes).decode()
|
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()
|