awacke1 commited on
Commit
03f155a
1 Parent(s): df611ad

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import os
5
+ import urllib
6
+
7
+ def download_file(url, local_filename):
8
+ with requests.get(url, stream=True) as r:
9
+ r.raise_for_status()
10
+ with open(local_filename, 'wb') as f:
11
+ for chunk in r.iter_content(chunk_size=8192):
12
+ f.write(chunk)
13
+ return local_filename
14
+
15
+ def download_html_and_files(url):
16
+ html_content = requests.get(url).text
17
+ soup = BeautifulSoup(html_content, 'html.parser')
18
+
19
+ base_url = urllib.parse.urlunparse(urllib.parse.urlparse(url)._replace(path='', params='', query='', fragment=''))
20
+
21
+ for link in soup.find_all('a'):
22
+ file_url = urllib.parse.urljoin(base_url, link.get('href'))
23
+ local_filename = urllib.parse.urlparse(file_url).path.split('/')[-1]
24
+ link['href'] = local_filename
25
+ download_file(file_url, local_filename)
26
+
27
+ with open("index.html", "w") as file:
28
+ file.write(str(soup))
29
+
30
+ def list_files(directory_path='.'):
31
+ return [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
32
+
33
+ def main():
34
+ st.sidebar.title('Bulk Download Tool')
35
+ url = st.sidebar.text_input('Please enter a URL to bulk download text and files')
36
+ if st.sidebar.button('📥 Get All the Content'):
37
+ download_html_and_files(url)
38
+ st.sidebar.write('Download complete. Here are the files you can download:')
39
+ for file in list_files():
40
+ st.sidebar.markdown(get_download_link(file), unsafe_allow_html=True)
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
+ if __name__ == "__main__":
50
+ main()