Spaces:
Runtime error
Runtime error
Update app.py
Browse files4 unmodified lines567891011122 unmodified lines15161718192021224 unmodified lines272829303132333435363738394041424344454647484950512 unmodified lines54555657584 unmodified linesimport urllibimport base64def download_file(url, local_filename):if url.startswith('http://') or url.startswith('https://'): # add this linetry:with requests.get(url, stream=True) as r:r.raise_for_status()2 unmodified linesf.write(chunk)return local_filenameexcept requests.exceptions.HTTPError as err:print(f"HTTP error occurred: {err}") # or use loggingdef download_html_and_files(url):html_content = requests.get(url).text4 unmodified linesfor link in soup.find_all('a'):file_url = urllib.parse.urljoin(base_url, link.get('href'))local_filename = urllib.parse.urlparse(file_url).path.split('/')[-1]if local_filename: # add this linelink['href'] = local_filenamedownload_file(file_url, local_filename)with open("index.html", "w") as file:file.write(str(soup))def list_files(directory_path='.'):return [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]def main():st.sidebar.title('Bulk Download Tool')url = st.sidebar.text_input('Please enter a URL to bulk download text and files')if st.sidebar.button('📥 Get All the Content'):download_html_and_files(url)st.sidebar.write('Download complete. Here are the files you can download:')for file in list_files():st.sidebar.markdown(get_download_link(file), unsafe_allow_html=True)def get_download_link(file):with open(file, "rb") as f:2 unmodified lineshref = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{file}\'>Click to download {file}</a>'return hrefif __name__ == "__main__":main()4 unmodified lines5678910111213142 unmodified lines171819202122234 unmodified lines282930313233343536373839404142432 unmodified lines464748495051525354555657585960616263644 unmodified linesimport urllibimport base64EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md']def download_file(url, local_filename):if url.startswith('http://') or url.startswith('https://'):try:with requests.get(url, stream=True) as r:r.raise_for_status()2 unmodified linesf.write(chunk)return local_filenameexcept requests.exceptions.HTTPError as err:print(f"HTTP error occurred: {err}")def download_html_and_files(url):html_content = requests.get(url).text4 unmodified linesfor link in soup.find_all('a'):file_url = urllib.parse.urljoin(base_url, link.get('href'))local_filename = urllib.parse.urlparse(file_url).path.split('/')[-1]if local_filename:link['href'] = local_filenamedownload_file(file_url, local_filename)with open("index.html", "w") as file:file.write(str(soup))def list_files(directory_path='.'):files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]return [f for f in files if f not in EXCLUDED_FILES]def get_download_link(file):with open(file, "rb") as f:2 unmodified lineshref = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{file}\'>Click to download {file}</a>'return hrefdef show_download_links():st.sidebar.write('Here are the files you can download:')for file in list_files():st.sidebar.markdown(get_download_link(file), unsafe_allow_html=True)def main():st.sidebar.title('Bulk Download Tool')url = st.sidebar.text_input('Please enter a URL to bulk download text and files')if st.sidebar.button('📥 Get All the Content'):download_html_and_files(url)show_download_links()if st.sidebar.button('📂 Show Download Links'):show_download_links()if __name__ == "__main__":main()
app.py+21-15