Update app.py
Browse files
app.py
CHANGED
@@ -636,6 +636,9 @@ def main():
|
|
636 |
st.session_state.current_url = None
|
637 |
st.session_state.google_creds = None
|
638 |
st.session_state.selected_files = []
|
|
|
|
|
|
|
639 |
|
640 |
st.title("Advanced File Downloader")
|
641 |
|
@@ -874,6 +877,35 @@ def main():
|
|
874 |
st.header("Bing Search Mode")
|
875 |
query = st.text_input("Enter search query", key="search_query_input")
|
876 |
num_results = st.slider("Number of results", 1, 50, 5, key="num_results_slider")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
877 |
if st.button("Search", key="search_btn"):
|
878 |
if query:
|
879 |
async def run_search():
|
@@ -886,26 +918,116 @@ def main():
|
|
886 |
with st.spinner("Searching..."):
|
887 |
urls = await dm.search_bing()
|
888 |
if urls:
|
|
|
889 |
st.success(f"Found {len(urls)} results!")
|
890 |
for i, url in enumerate(urls, 1):
|
891 |
with st.expander(f"Result {i}: {url}", expanded=(i == 1)):
|
892 |
if st.button(f"Deep Search Result {i}", key=f"deep_search_result_{i}"):
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
sublink_limit=max_sublinks,
|
897 |
-
timeout=sublink_timeout
|
898 |
-
)
|
899 |
-
if files:
|
900 |
-
st.session_state.discovered_files = files
|
901 |
-
st.session_state.current_url = url
|
902 |
-
st.session_state.selected_files = []
|
903 |
-
safe_rerun()
|
904 |
-
else:
|
905 |
-
st.warning("No files found on this page.")
|
906 |
else:
|
907 |
st.warning("No search results found.")
|
908 |
asyncio.run(run_search())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
909 |
|
910 |
else: # PDF Summarizer mode
|
911 |
if summarizer is None:
|
@@ -931,7 +1053,6 @@ def main():
|
|
931 |
except Exception as e:
|
932 |
st.error(f"Error summarizing PDF: {e}")
|
933 |
|
934 |
-
|
935 |
if __name__ == "__main__":
|
936 |
try:
|
937 |
main()
|
|
|
636 |
st.session_state.current_url = None
|
637 |
st.session_state.google_creds = None
|
638 |
st.session_state.selected_files = []
|
639 |
+
st.session_state.do_deep_search = False # Add this
|
640 |
+
st.session_state.deep_search_url = None # Add this
|
641 |
+
st.session_state.search_results = [] # Add this
|
642 |
|
643 |
st.title("Advanced File Downloader")
|
644 |
|
|
|
877 |
st.header("Bing Search Mode")
|
878 |
query = st.text_input("Enter search query", key="search_query_input")
|
879 |
num_results = st.slider("Number of results", 1, 50, 5, key="num_results_slider")
|
880 |
+
|
881 |
+
# Check if deep search was requested
|
882 |
+
if st.session_state.get('do_deep_search', False):
|
883 |
+
url_to_search = st.session_state.get('deep_search_url')
|
884 |
+
st.write(f"Running deep search on: {url_to_search}")
|
885 |
+
|
886 |
+
async def perform_deep_search():
|
887 |
+
async with DownloadManager(
|
888 |
+
use_proxy=use_proxy,
|
889 |
+
proxy=proxy
|
890 |
+
) as dm:
|
891 |
+
files = await dm.deep_search(
|
892 |
+
url=url_to_search,
|
893 |
+
custom_ext_list=custom_extensions.split(',') if custom_extensions else [],
|
894 |
+
sublink_limit=max_sublinks,
|
895 |
+
timeout=sublink_timeout
|
896 |
+
)
|
897 |
+
if files:
|
898 |
+
st.session_state.discovered_files = files
|
899 |
+
st.session_state.current_url = url_to_search
|
900 |
+
st.session_state.selected_files = []
|
901 |
+
else:
|
902 |
+
st.warning("No files found on this page.")
|
903 |
+
|
904 |
+
# Clear the deep search flag after execution
|
905 |
+
st.session_state.do_deep_search = False
|
906 |
+
|
907 |
+
asyncio.run(perform_deep_search())
|
908 |
+
|
909 |
if st.button("Search", key="search_btn"):
|
910 |
if query:
|
911 |
async def run_search():
|
|
|
918 |
with st.spinner("Searching..."):
|
919 |
urls = await dm.search_bing()
|
920 |
if urls:
|
921 |
+
st.session_state.search_results = urls # Store URLs in session state
|
922 |
st.success(f"Found {len(urls)} results!")
|
923 |
for i, url in enumerate(urls, 1):
|
924 |
with st.expander(f"Result {i}: {url}", expanded=(i == 1)):
|
925 |
if st.button(f"Deep Search Result {i}", key=f"deep_search_result_{i}"):
|
926 |
+
st.session_state.deep_search_url = url # Store the URL to search
|
927 |
+
st.session_state.do_deep_search = True # Flag to perform deep search
|
928 |
+
safe_rerun() # Rerun to apply state change
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
929 |
else:
|
930 |
st.warning("No search results found.")
|
931 |
asyncio.run(run_search())
|
932 |
+
|
933 |
+
# Display search results if they exist
|
934 |
+
if hasattr(st.session_state, 'search_results') and st.session_state.search_results:
|
935 |
+
urls = st.session_state.search_results
|
936 |
+
st.success(f"Found {len(urls)} results!")
|
937 |
+
for i, url in enumerate(urls, 1):
|
938 |
+
with st.expander(f"Result {i}: {url}", expanded=(i == 1)):
|
939 |
+
if st.button(f"Deep Search Result {i}", key=f"deep_search_result_saved_{i}"):
|
940 |
+
st.session_state.deep_search_url = url
|
941 |
+
st.session_state.do_deep_search = True
|
942 |
+
safe_rerun()
|
943 |
+
|
944 |
+
# If files were discovered in a previous search, show them
|
945 |
+
if st.session_state.discovered_files:
|
946 |
+
files = st.session_state.discovered_files
|
947 |
+
st.success(f"Found {len(files)} files on {st.session_state.current_url}!")
|
948 |
+
|
949 |
+
# File selection and download UI
|
950 |
+
col1, col2 = st.columns([1, 4])
|
951 |
+
with col1:
|
952 |
+
if st.button("Select All", key="bing_select_all_btn"):
|
953 |
+
st.session_state.selected_files = list(range(len(files)))
|
954 |
+
safe_rerun()
|
955 |
+
if st.button("Clear Selection", key="bing_clear_selection_btn"):
|
956 |
+
st.session_state.selected_files = []
|
957 |
+
safe_rerun()
|
958 |
+
|
959 |
+
selected_files = st.multiselect(
|
960 |
+
"Select files to download",
|
961 |
+
options=list(range(len(files))),
|
962 |
+
default=st.session_state.selected_files,
|
963 |
+
format_func=lambda x: f"{files[x]['filename']} ({files[x]['size']})",
|
964 |
+
key="bing_file_multiselect"
|
965 |
+
)
|
966 |
+
st.session_state.selected_files = selected_files
|
967 |
+
|
968 |
+
if selected_files:
|
969 |
+
col1, col2, col3, col4 = st.columns(4)
|
970 |
+
with col1:
|
971 |
+
download_dir = st.text_input("Download Directory", value="./downloads", key="bing_download_dir_input")
|
972 |
+
with col2:
|
973 |
+
create_zip = st.checkbox("Create ZIP file", value=True, key="bing_create_zip_checkbox")
|
974 |
+
with col3:
|
975 |
+
delete_after = st.checkbox("Delete after creating ZIP", key="bing_delete_after_checkbox")
|
976 |
+
with col4:
|
977 |
+
upload_to_drive = st.checkbox("Upload to Google Drive", key="bing_upload_drive_checkbox")
|
978 |
+
|
979 |
+
if st.button("Download Selected", key="bing_download_btn"):
|
980 |
+
if not os.path.exists(download_dir):
|
981 |
+
os.makedirs(download_dir)
|
982 |
+
|
983 |
+
async def download_files():
|
984 |
+
downloaded_paths = []
|
985 |
+
progress_bar = st.progress(0)
|
986 |
+
status_text = st.empty()
|
987 |
+
async with DownloadManager(use_proxy=use_proxy, proxy=proxy) as dm:
|
988 |
+
for i, idx in enumerate(selected_files):
|
989 |
+
progress = (i + 1) / len(selected_files)
|
990 |
+
file_info = files[idx]
|
991 |
+
status_text.text(f"Downloading {file_info['filename']}... ({i+1}/{len(selected_files)})")
|
992 |
+
progress_bar.progress(progress)
|
993 |
+
path = await dm.download_file(file_info, download_dir, st.session_state.current_url)
|
994 |
+
if path:
|
995 |
+
downloaded_paths.append(path)
|
996 |
+
status_text.empty()
|
997 |
+
progress_bar.empty()
|
998 |
+
return downloaded_paths
|
999 |
+
|
1000 |
+
downloaded = asyncio.run(download_files())
|
1001 |
+
|
1002 |
+
if downloaded:
|
1003 |
+
st.success(f"Successfully downloaded {len(downloaded)} files")
|
1004 |
+
if create_zip:
|
1005 |
+
zip_path = create_zip_file(downloaded, download_dir)
|
1006 |
+
st.success(f"Created ZIP file: {zip_path}")
|
1007 |
+
with open(zip_path, "rb") as f:
|
1008 |
+
zip_data = f.read()
|
1009 |
+
st.download_button("Download ZIP", data=zip_data, file_name=os.path.basename(zip_path), mime="application/zip")
|
1010 |
+
|
1011 |
+
if upload_to_drive and st.session_state.get('google_creds'):
|
1012 |
+
with st.spinner("Uploading to Google Drive..."):
|
1013 |
+
drive_id = google_drive_upload(zip_path, st.session_state.google_creds)
|
1014 |
+
if not isinstance(drive_id, str) or not drive_id.startswith("Error"):
|
1015 |
+
st.success(f"Uploaded to Google Drive. File ID: {drive_id}")
|
1016 |
+
else:
|
1017 |
+
st.error(drive_id)
|
1018 |
+
|
1019 |
+
if delete_after:
|
1020 |
+
for path in downloaded:
|
1021 |
+
try:
|
1022 |
+
os.remove(path)
|
1023 |
+
except Exception as e:
|
1024 |
+
st.warning(f"Could not delete {path}: {e}")
|
1025 |
+
st.info("Deleted original files after ZIP creation")
|
1026 |
+
else:
|
1027 |
+
for path in downloaded:
|
1028 |
+
with open(path, "rb") as f:
|
1029 |
+
file_data = f.read()
|
1030 |
+
st.download_button(f"Download {os.path.basename(path)}", data=file_data, file_name=os.path.basename(path))
|
1031 |
|
1032 |
else: # PDF Summarizer mode
|
1033 |
if summarizer is None:
|
|
|
1053 |
except Exception as e:
|
1054 |
st.error(f"Error summarizing PDF: {e}")
|
1055 |
|
|
|
1056 |
if __name__ == "__main__":
|
1057 |
try:
|
1058 |
main()
|