euler314 commited on
Commit
7e696de
·
verified ·
1 Parent(s): 0444076

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -7
app.py CHANGED
@@ -542,6 +542,7 @@ def main():
542
  st.session_state.discovered_files = []
543
  st.session_state.current_url = None
544
  st.session_state.google_creds = None
 
545
 
546
  st.title("Advanced File Downloader")
547
 
@@ -590,8 +591,9 @@ def main():
590
  custom_ext_list=custom_extensions.split(',') if custom_extensions else [],
591
  sublink_limit=max_sublinks
592
  )
593
- st.session_state.discovered_files = files
594
- st.session_state.current_url = url
 
595
  return files
596
 
597
  files = asyncio.run(run_deep_search())
@@ -603,17 +605,22 @@ def main():
603
  with col1:
604
  if st.button("Select All"):
605
  st.session_state.selected_files = list(range(len(files)))
 
606
  if st.button("Clear Selection"):
607
  st.session_state.selected_files = []
 
608
 
609
  # File selection
610
  selected_files = st.multiselect(
611
  "Select files to download",
612
- range(len(files)),
613
- default=st.session_state.get('selected_files', []),
614
  format_func=lambda x: f"{files[x]['filename']} ({files[x]['size']})"
615
  )
616
 
 
 
 
617
  if selected_files:
618
  col1, col2, col3, col4 = st.columns(4)
619
  with col1:
@@ -681,6 +688,97 @@ def main():
681
  else:
682
  st.warning("No files found.")
683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
684
  elif mode == "Bing Search":
685
  st.header("Bing Search Mode")
686
  query = st.text_input("Enter search query")
@@ -707,12 +805,11 @@ def main():
707
  custom_ext_list=custom_extensions.split(',') if custom_extensions else [],
708
  sublink_limit=max_sublinks
709
  )
710
- # Reuse the same file handling logic as Manual URL mode
711
  if files:
712
  st.session_state.discovered_files = files
713
  st.session_state.current_url = url
714
- st.success(f"Found {len(files)} files!")
715
- # Add file selection and download UI here (same as Manual URL mode)
716
  else:
717
  st.warning("No files found on this page.")
718
  else:
 
542
  st.session_state.discovered_files = []
543
  st.session_state.current_url = None
544
  st.session_state.google_creds = None
545
+ st.session_state.selected_files = []
546
 
547
  st.title("Advanced File Downloader")
548
 
 
591
  custom_ext_list=custom_extensions.split(',') if custom_extensions else [],
592
  sublink_limit=max_sublinks
593
  )
594
+ if files:
595
+ st.session_state.discovered_files = files
596
+ st.session_state.current_url = url
597
  return files
598
 
599
  files = asyncio.run(run_deep_search())
 
605
  with col1:
606
  if st.button("Select All"):
607
  st.session_state.selected_files = list(range(len(files)))
608
+ st.experimental_rerun()
609
  if st.button("Clear Selection"):
610
  st.session_state.selected_files = []
611
+ st.experimental_rerun()
612
 
613
  # File selection
614
  selected_files = st.multiselect(
615
  "Select files to download",
616
+ options=list(range(len(files))),
617
+ default=st.session_state.selected_files,
618
  format_func=lambda x: f"{files[x]['filename']} ({files[x]['size']})"
619
  )
620
 
621
+ # Update session state
622
+ st.session_state.selected_files = selected_files
623
+
624
  if selected_files:
625
  col1, col2, col3, col4 = st.columns(4)
626
  with col1:
 
688
  else:
689
  st.warning("No files found.")
690
 
691
+ # Display current files if they exist in session state
692
+ elif st.session_state.discovered_files:
693
+ files = st.session_state.discovered_files
694
+ st.success(f"Found {len(files)} files!")
695
+
696
+ # Select All/Clear Selection buttons
697
+ col1, col2 = st.columns([1, 4])
698
+ with col1:
699
+ if st.button("Select All"):
700
+ st.session_state.selected_files = list(range(len(files)))
701
+ st.experimental_rerun()
702
+ if st.button("Clear Selection"):
703
+ st.session_state.selected_files = []
704
+ st.experimental_rerun()
705
+
706
+ # File selection
707
+ selected_files = st.multiselect(
708
+ "Select files to download",
709
+ options=list(range(len(files))),
710
+ default=st.session_state.selected_files,
711
+ format_func=lambda x: f"{files[x]['filename']} ({files[x]['size']})"
712
+ )
713
+
714
+ # Update session state
715
+ st.session_state.selected_files = selected_files
716
+
717
+ if selected_files:
718
+ col1, col2, col3, col4 = st.columns(4)
719
+ with col1:
720
+ download_dir = st.text_input("Download Directory", value="./downloads")
721
+ with col2:
722
+ create_zip = st.checkbox("Create ZIP file", value=True)
723
+ with col3:
724
+ delete_after = st.checkbox("Delete after creating ZIP")
725
+ with col4:
726
+ upload_to_drive = st.checkbox("Upload to Google Drive")
727
+
728
+ if st.button("Download Selected"):
729
+ if not os.path.exists(download_dir):
730
+ os.makedirs(download_dir)
731
+
732
+ async def download_files():
733
+ downloaded_paths = []
734
+ progress_bar = st.progress(0)
735
+ status_text = st.empty()
736
+
737
+ async with DownloadManager(use_proxy=use_proxy, proxy=proxy) as dm:
738
+ for i, idx in enumerate(selected_files):
739
+ progress = (i + 1) / len(selected_files)
740
+ file_info = files[idx]
741
+
742
+ status_text.text(f"Downloading {file_info['filename']}... ({i+1}/{len(selected_files)})")
743
+ progress_bar.progress(progress)
744
+
745
+ path = await dm.download_file(
746
+ file_info,
747
+ download_dir,
748
+ st.session_state.current_url
749
+ )
750
+ if path:
751
+ downloaded_paths.append(path)
752
+
753
+ status_text.empty()
754
+ progress_bar.empty()
755
+ return downloaded_paths
756
+
757
+ downloaded = asyncio.run(download_files())
758
+
759
+ if downloaded:
760
+ st.success(f"Successfully downloaded {len(downloaded)} files")
761
+
762
+ if create_zip or upload_to_drive:
763
+ zip_path = create_zip_file(downloaded, download_dir)
764
+ st.success(f"Created ZIP file: {zip_path}")
765
+
766
+ if upload_to_drive and st.session_state.get('google_creds'):
767
+ with st.spinner("Uploading to Google Drive..."):
768
+ drive_id = google_drive_upload(zip_path, st.session_state.google_creds)
769
+ if not isinstance(drive_id, str) or not drive_id.startswith("Error"):
770
+ st.success(f"Uploaded to Google Drive. File ID: {drive_id}")
771
+ else:
772
+ st.error(drive_id)
773
+
774
+ if delete_after:
775
+ for path in downloaded:
776
+ try:
777
+ os.remove(path)
778
+ except Exception as e:
779
+ st.warning(f"Could not delete {path}: {e}")
780
+ st.info("Deleted original files after ZIP creation")
781
+
782
  elif mode == "Bing Search":
783
  st.header("Bing Search Mode")
784
  query = st.text_input("Enter search query")
 
805
  custom_ext_list=custom_extensions.split(',') if custom_extensions else [],
806
  sublink_limit=max_sublinks
807
  )
 
808
  if files:
809
  st.session_state.discovered_files = files
810
  st.session_state.current_url = url
811
+ st.session_state.selected_files = []
812
+ st.experimental_rerun()
813
  else:
814
  st.warning("No files found on this page.")
815
  else: