import os import subprocess import sys def install_playwright_dependencies(): try: # Update package list os.system('apt-get update -y') # Install required dependencies including GTK dependencies = [ 'libnss3', 'libnspr4', 'libatk1.0-0', 'libatk-bridge2.0-0', 'libcups2', 'libxcomposite1', 'libxdamage1', 'libatspi2.0-0', 'libgtk-3-0', # Add GTK dependencies 'libgdk-3-0' ] dependency_command = f"apt-get install -y {' '.join(dependencies)}" os.system(dependency_command) # Install playwright and its browsers os.system('playwright install') os.system('python -m playwright install') st.success("Successfully installed Playwright dependencies") except Exception as e: st.error(f"Error installing dependencies: {e}") st.error("Please run these commands manually in your terminal:") st.code("apt-get update") st.code(f"apt-get install -y {' '.join(dependencies)}") st.code("playwright install") import streamlit as st # Must be the first Streamlit command st.set_page_config(page_title="Advanced File Downloader", layout="wide") # Import other required packages import spacy import spacy.cli @st.cache_resource def load_models(): try: # Try to load spaCy model try: nlp = spacy.load("en_core_web_sm") except OSError: st.info("Downloading spaCy model...") spacy.cli.download("en_core_web_sm") nlp = spacy.load("en_core_web_sm") # Load SentenceTransformer with offline handling try: from sentence_transformers import SentenceTransformer model_name = 'all-MiniLM-L6-v2' cache_dir = os.path.expanduser('~/.cache/torch/sentence_transformers') if os.path.exists(os.path.join(cache_dir, model_name)): semantic_model = SentenceTransformer(os.path.join(cache_dir, model_name)) else: st.warning(f"Downloading SentenceTransformer model {model_name}...") semantic_model = SentenceTransformer(model_name) except Exception as e: st.error(f"Error loading SentenceTransformer: {e}") st.info("Continuing without semantic search capability...") semantic_model = None # Load Transformers pipeline with offline handling try: from transformers import pipeline, AutoModelForSeq2SeqGeneration, AutoTokenizer model_name = "facebook/bart-large-cnn" cache_dir = os.path.expanduser('~/.cache/huggingface/transformers') if os.path.exists(os.path.join(cache_dir, model_name)): summarizer = pipeline("summarization", model=model_name) else: st.warning(f"Downloading Transformer model {model_name}...") summarizer = pipeline("summarization") except Exception as e: st.error(f"Error loading Transformers: {e}") st.info("Continuing without summarization capability...") summarizer = None return nlp, semantic_model, summarizer except Exception as e: st.error(f"Error loading models: {e}") return None, None, None # Initialize models with better error handling with st.spinner("Loading models..."): nlp_model, semantic_model, summarizer = load_models() if nlp_model is None: st.error("Failed to load essential NLP model. The application cannot continue.") st.stop() else: # Continue with available features based on which models loaded successfully if semantic_model is None: st.warning("Semantic search features will be disabled.") if summarizer is None: st.warning("PDF summarization features will be disabled.") # Rest of your imports and code here... # Rest of your code... import os import subprocess from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError import asyncio import logging from urllib.parse import urlparse import re from pathlib import Path from io import BytesIO import random from bs4 import BeautifulSoup from PyPDF2 import PdfReader import zipfile import tempfile import mimetypes import requests # -------------------- spaCy Model Setup -------------------- import spacy import spacy.cli from spacy.language import Language @Language.factory("spacy-curated-transformers_RobertaTransformer_v1") def dummy_roberta_transformer(nlp, name): def dummy(doc): return doc return dummy @st.cache_resource def load_nlp_model(): try: nlp_model = spacy.load("en_core_web_sm") except OSError: st.write("Model en_core_web_sm not found. Downloading it now...") spacy.cli.download("en_core_web_sm") nlp_model = spacy.load("en_core_web_sm") return nlp_model nlp_model = load_nlp_model() # Also load SentenceTransformer for semantic re-ranking. from sentence_transformers import SentenceTransformer, util @st.cache_resource def load_semantic_model(): return SentenceTransformer('all-MiniLM-L6-v2') semantic_model = load_semantic_model() # -------------------- Transformers Summarization Setup -------------------- from transformers import pipeline @st.cache_resource def load_summarizer(): return pipeline("summarization") summarizer = load_summarizer() def summarize_pdf_url(pdf_url): try: with st.spinner("Downloading and processing PDF..."): response = requests.get(pdf_url, stream=True) temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") with open(temp_pdf.name, "wb") as f: f.write(response.content) reader = PdfReader(temp_pdf.name) text = " ".join([page.extract_text() or "" for page in reader.pages]) os.remove(temp_pdf.name) limited_text = text[:3000] summary = summarizer(limited_text, max_length=200, min_length=50, do_sample=False) return summary[0]["summary_text"] except Exception as e: return f"Error summarizing PDF: {e}" # -------------------- Google API Setup -------------------- GOOGLE_OAUTH_CONFIG = { "web": { "client_id": "your_client_id", "project_id": "your_project_id", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_secret": "your_client_secret", "redirect_uris": ["your_redirect_uri"] } } import google_auth_oauthlib.flow import googleapiclient.discovery import google.auth.transport.requests def get_google_auth_url(): client_config = GOOGLE_OAUTH_CONFIG["web"] flow = google_auth_oauthlib.flow.Flow.from_client_config( {"web": client_config}, scopes=["https://www.googleapis.com/auth/drive.file"] ) flow.redirect_uri = client_config["redirect_uris"][0] authorization_url, _ = flow.authorization_url( access_type="offline", include_granted_scopes="true", prompt="consent" ) return authorization_url def exchange_code_for_credentials(auth_code): if not auth_code.strip(): return None, "No code provided." try: client_config = GOOGLE_OAUTH_CONFIG["web"] flow = google_auth_oauthlib.flow.Flow.from_client_config( {"web": client_config}, scopes=["https://www.googleapis.com/auth/drive.file"] ) flow.redirect_uri = client_config["redirect_uris"][0] flow.fetch_token(code=auth_code.strip()) creds = flow.credentials if not creds or not creds.valid: return None, "Could not validate credentials. Check code and try again." return creds, "Google Sign-In successful!" except Exception as e: return None, f"Error during token exchange: {e}" # -------------------- Playwright Setup -------------------- def install_playwright_dependencies(): os.environ['PLAYWRIGHT_BROWSERS_PATH'] = os.path.expanduser("~/.cache/ms-playwright") os.environ['LD_LIBRARY_PATH'] = '/usr/lib/playwright:/usr/lib/x86_64-linux-gnu' try: subprocess.run(['python3', '-m', 'playwright', 'install', 'chromium'], check=True) except Exception as e: st.error(f"Error installing Playwright: {e}") # Initialize Playwright dependencies install_playwright_dependencies() # -------------------- Logging Setup -------------------- logging.basicConfig( filename='advanced_download_log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' ) logger = logging.getLogger() # -------------------- Shared Utils -------------------- USER_AGENTS = [ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0', ] def get_random_user_agent(): return random.choice(USER_AGENTS) def sizeof_fmt(num, suffix='B'): for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: if abs(num) < 1024.0: return f"{num:3.1f}{unit}{suffix}" num /= 1024.0 return f"{num:.1f}Y{suffix}" # ---------- Human-like Interactions ------------- async def human_like_scroll(page): scroll_height = await page.evaluate('document.body.scrollHeight') viewport_height = await page.evaluate('window.innerHeight') current_scroll = 0 while current_scroll < scroll_height: await page.evaluate(f'window.scrollTo(0, {current_scroll})') await asyncio.sleep(random.uniform(0.5, 1.5)) current_scroll += viewport_height * random.uniform(0.5, 1.5) scroll_height = await page.evaluate('document.body.scrollHeight') async def human_like_interactions(page): await page.mouse.move(random.randint(0, 1000), random.randint(0, 1000)) await asyncio.sleep(random.uniform(0.5, 1.5)) await page.mouse.click(random.randint(0, 1000), random.randint(0, 1000)) await asyncio.sleep(random.uniform(0.5, 1.5)) await page.evaluate("window.scrollBy(0, window.innerHeight / 2)") await asyncio.sleep(random.uniform(0.5, 1.5)) # ---------- NLP Helpers ------------- def nlp_preprocess(query: str) -> str: doc = nlp_model(query) tokens = [token.lemma_.lower() for token in doc if not token.is_stop and token.is_alpha] processed = " ".join(tokens) return processed if processed.strip() else query def nlp_extract_entities(text: str): doc = nlp_model(text) return [(ent.text, ent.label_) for ent in doc.ents] # ---------- AI-enhanced Query Preprocessing ------------- def ai_preprocess_query(query: str) -> str: return query class DownloadManager: def __init__(self, use_proxy=False, proxy=None, query=None, num_results=5): self.use_proxy = use_proxy self.proxy = proxy self.query = query self.num_results = num_results self.playwright = None self.browser = None self.context = None self.page = None async def __aenter__(self): self.playwright = await async_playwright().start() opts = {"headless": True} if self.use_proxy and self.proxy: opts["proxy"] = {"server": self.proxy} self.browser = await self.playwright.chromium.launch(**opts) self.context = await self.browser.new_context(user_agent=get_random_user_agent()) self.page = await self.context.new_page() await self.page.set_extra_http_headers({ 'Accept-Language': 'en-US,en;q=0.9', 'Accept-Encoding': 'gzip, deflate, br', 'Referer': 'https://www.bing.com/' }) return self async def __aexit__(self, exc_type, exc_val, exc_tb): if self.browser: await self.browser.close() if self.playwright: await self.playwright.stop() async def get_file_size(self, url): try: async with self.context.new_page() as page: response = await page.request.head(url, timeout=15000) length = response.headers.get('Content-Length', None) if length: return sizeof_fmt(int(length)) else: return "Unknown Size" except Exception: return "Unknown Size" async def get_pdf_metadata(self, url): try: async with self.context.new_page() as page: resp = await page.request.get(url, timeout=15000) if resp.ok: content = await resp.body() pdf = BytesIO(content) reader = PdfReader(pdf) return { 'Title': reader.metadata.get('/Title', 'N/A') if reader.metadata else 'N/A', 'Author': reader.metadata.get('/Author', 'N/A') if reader.metadata else 'N/A', 'Pages': len(reader.pages), } else: return {} except Exception: return {} async def extract_real_download_url(self, url): try: async with self.context.new_page() as page: response = await page.goto(url, wait_until='networkidle', timeout=30000) if response and response.headers.get('location'): return response.headers['location'] content_type = response.headers.get('content-type', '') if 'text/html' not in content_type.lower(): return url content = await page.content() return page.url except Exception as e: logger.error(f"Error extracting real download URL: {e}") return url async def extract_downloadable_files(self, url, custom_ext_list): found_files = [] try: response = await self.page.goto(url, timeout=30000, wait_until='networkidle') if not response: return [] final_url = self.page.url if '.php' in final_url or 'download' in final_url: real_url = await self.extract_real_download_url(final_url) if real_url != final_url: found_files.append({ 'url': real_url, 'filename': os.path.basename(urlparse(real_url).path) or 'downloaded_file', 'size': await self.get_file_size(real_url), 'metadata': {} }) return found_files await self.page.wait_for_load_state('networkidle', timeout=30000) content = await self.page.content() soup = BeautifulSoup(content, 'html.parser') default_exts = ['.pdf', '.docx', '.doc', '.zip', '.rar', '.mp3', '.mp4', '.avi', '.mkv', '.png', '.jpg', '.jpeg', '.gif'] all_exts = set(default_exts + [ext.strip().lower() for ext in custom_ext_list if ext.strip()]) parsed_base = urlparse(final_url) base_url = f"{parsed_base.scheme}://{parsed_base.netloc}" for a in soup.find_all('a', href=True): href = a['href'].strip() if any(href.lower().endswith(ext) for ext in all_exts): file_url = href if href.startswith('http') else ( f"{base_url}{href}" if href.startswith('/') else f"{base_url}/{href}" ) size_str = await self.get_file_size(file_url) meta = {} if file_url.lower().endswith('.pdf'): meta = await self.get_pdf_metadata(file_url) found_files.append({ 'url': file_url, 'filename': os.path.basename(file_url.split('?')[0]), 'size': size_str, 'metadata': meta }) elif ("drive.google.com" in href) or ("docs.google.com" in href): file_id = None for pattern in [r'/file/d/([^/]+)', r'id=([^&]+)', r'open\?id=([^&]+)']: match = re.search(pattern, href) if match: file_id = match.group(1) break if file_id: direct_url = f"https://drive.google.com/uc?export=download&id={file_id}" filename = file_id try: response = await self.page.request.head(direct_url, timeout=15000) cd = response.headers.get("Content-Disposition", "") if cd: mt = re.search(r'filename\*?="?([^";]+)', cd) if mt: filename = mt.group(1).strip('"').strip() found_files.append({ 'url': direct_url, 'filename': filename, 'size': await self.get_file_size(direct_url), 'metadata': {} }) except Exception as e: logger.error(f"Error processing Google Drive link: {e}") return found_files except Exception as e: logger.error(f"Error extracting files from {url}: {e}") return [] async def download_file(self, file_info, save_dir, referer): file_url = file_info['url'] fname = file_info['filename'] path = os.path.join(save_dir, fname) base, ext = os.path.splitext(fname) counter = 1 while os.path.exists(path): path = os.path.join(save_dir, f"{base}_{counter}{ext}") counter += 1 os.makedirs(save_dir, exist_ok=True) try: if "drive.google.com" in file_url: import gdown try: st.write(f"Downloading from Google Drive: {fname}") output = gdown.download(file_url, path, quiet=False) if output: return path return None except Exception as e: logger.error(f"Google Drive download error: {e}") return None async with self.context.new_page() as page: st.write(f"Downloading: {fname}") headers = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Referer': referer } response = await page.request.get(file_url, headers=headers, timeout=30000) if response.status == 200: content = await response.body() with open(path, 'wb') as f: f.write(content) return path else: logger.error(f"Download failed with status {response.status}: {file_url}") return None except Exception as e: logger.error(f"Error downloading {file_url}: {e}") return None async def search_bing(self): if not self.query: return [], [] search_query = self.query if "filetype:pdf" not in search_query.lower(): search_query += " filetype:pdf" search_url = f"https://www.bing.com/search?q={search_query}&count={self.num_results}" try: await self.page.goto(search_url, timeout=30000) await self.page.wait_for_selector('li.b_algo', timeout=30000) await human_like_scroll(self.page) results = [] elements = await self.page.query_selector_all('li.b_algo') for element in elements: link = await element.query_selector('h2 a') if link: url = await link.get_attribute('href') if url: results.append(url) return results[:self.num_results] except Exception as e: logger.error(f"Bing search error: {e}") return [] async def get_sublinks(self, url, limit=100): try: await self.page.goto(url, timeout=30000) content = await self.page.content() soup = BeautifulSoup(content, 'html.parser') parsed_base = urlparse(url) base_url = f"{parsed_base.scheme}://{parsed_base.netloc}" links = set() for a in soup.find_all('a', href=True): href = a['href'].strip() if href.startswith('http'): links.add(href) elif href.startswith('/'): links.add(f"{base_url}{href}") return list(links)[:limit] except Exception as e: logger.error(f"Error getting sublinks: {e}") return [] async def deep_search(self, url, custom_ext_list=None, sublink_limit=100): if not custom_ext_list: custom_ext_list = [] progress_text = st.empty() progress_bar = st.progress(0) try: # Search main page progress_text.text("Analyzing main page...") main_files = await self.extract_downloadable_files(url, custom_ext_list) # Get and search sublinks progress_text.text("Getting sublinks...") sublinks = await self.get_sublinks(url, sublink_limit) if not sublinks: progress_bar.progress(1.0) return main_files # Process sublinks all_files = main_files total_links = len(sublinks) for i, sublink in enumerate(sublinks, 1): progress_text.text(f"Processing sublink {i}/{total_links}: {sublink}") progress_bar.progress(i/total_links) sub_files = await self.extract_downloadable_files(sublink, custom_ext_list) all_files.extend(sub_files) # Make results unique seen_urls = set() unique_files = [] for f in all_files: if f['url'] not in seen_urls: seen_urls.add(f['url']) unique_files.append(f) progress_text.text(f"Found {len(unique_files)} unique files") progress_bar.progress(1.0) return unique_files except Exception as e: logger.error(f"Deep search error: {e}") return [] def main(): if 'initialized' not in st.session_state: st.session_state.initialized = True st.session_state.discovered_files = [] st.session_state.current_url = None st.session_state.google_creds = None st.title("Advanced File Downloader") with st.sidebar: st.header("Settings") mode = st.radio("Select Mode", ["Manual URL", "Bing Search", "PDF Summarizer"]) with st.expander("Advanced Options"): custom_extensions = st.text_input( "Custom File Extensions", placeholder=".csv, .txt, .epub" ) use_proxy = st.checkbox("Use Proxy") proxy = st.text_input("Proxy URL", placeholder="http://proxy:port") if mode == "Manual URL": st.header("Manual URL Mode") url = st.text_input("Enter URL", placeholder="https://example.com") if st.button("Deep Search", use_container_width=True): if url: async def run_deep_search(): async with DownloadManager(use_proxy=use_proxy, proxy=proxy) as dm: with st.spinner("Searching for files..."): files = await dm.deep_search( url=url, custom_ext_list=custom_extensions.split(',') if custom_extensions else [] ) st.session_state.discovered_files = files st.session_state.current_url = url return files files = asyncio.run(run_deep_search()) if files: st.success(f"Found {len(files)} files!") with st.expander("Found Files", expanded=True): for i, file in enumerate(files): col1, col2 = st.columns([3, 1]) with col1: st.write(f"{i+1}. {file['filename']}") with col2: st.write(f"Size: {file['size']}") # Download section st.subheader("Download Files") selected_files = st.multiselect( "Select files to download", range(len(files)), format_func=lambda x: f"{files[x]['filename']} ({files[x]['size']})" ) if selected_files: col1, col2 = st.columns([3, 1]) with col1: download_dir = st.text_input("Download Directory", value="./downloads") with col2: if st.button("Download Selected", use_container_width=True): async def download_files(): async with DownloadManager(use_proxy=use_proxy, proxy=proxy) as dm: paths = [] progress_text = st.empty() progress_bar = st.progress(0) for i, idx in enumerate(selected_files): progress = (i + 1) / len(selected_files) progress_text.text(f"Downloading {files[idx]['filename']}...") progress_bar.progress(progress) path = await dm.download_file( files[idx], download_dir, url ) if path: paths.append(path) progress_text.empty() progress_bar.empty() return paths downloaded = asyncio.run(download_files()) if downloaded: st.success(f"Successfully downloaded {len(downloaded)} files to {download_dir}") # Create zip file if multiple files were downloaded if len(downloaded) > 1: zip_path = os.path.join(download_dir, "downloads.zip") with zipfile.ZipFile(zip_path, 'w') as zipf: for file in downloaded: zipf.write(file, os.path.basename(file)) st.success(f"Created zip file: {zip_path}") else: st.warning("No files found.") elif mode == "Bing Search": st.header("Bing Search Mode") query = st.text_input("Enter search query") num_results = st.slider("Number of results", 1, 50, 5) if st.button("Search"): if query: async def run_search(): async with DownloadManager( use_proxy=use_proxy, proxy=proxy, query=query, num_results=num_results ) as dm: with st.spinner("Searching..."): urls = await dm.search_bing() if urls: st.success(f"Found {len(urls)} results!") for i, url in enumerate(urls, 1): with st.expander(f"Result {i}: {url}", expanded=i==1): if st.button(f"Deep Search This Result {i}"): files = await dm.deep_search( url=url, custom_ext_list=custom_extensions.split(',') if custom_extensions else [] ) if files: st.session_state.discovered_files = files st.session_state.current_url = url st.success(f"Found {len(files)} files!") with st.expander("Found Files", expanded=True): for j, file in enumerate(files): col1, col2 = st.columns([3, 1]) with col1: st.write(f"{j+1}. {file['filename']}") with col2: st.write(f"Size: {file['size']}") selected_files = st.multiselect( "Select files to download", range(len(files)), format_func=lambda x: f"{files[x]['filename']} ({files[x]['size']})" ) if selected_files: col1, col2 = st.columns([3, 1]) with col1: download_dir = st.text_input("Download Directory", value="./downloads") with col2: if st.button("Download Selected Files"): progress_text = st.empty() progress_bar = st.progress(0) paths = [] for k, idx in enumerate(selected_files): progress = (k + 1) / len(selected_files) progress_text.text(f"Downloading {files[idx]['filename']}...") progress_bar.progress(progress) path = await dm.download_file( files[idx], download_dir, url ) if path: paths.append(path) progress_text.empty() progress_bar.empty() if paths: st.success(f"Successfully downloaded {len(paths)} files to {download_dir}") if len(paths) > 1: zip_path = os.path.join(download_dir, "downloads.zip") with zipfile.ZipFile(zip_path, 'w') as zipf: for file in paths: zipf.write(file, os.path.basename(file)) st.success(f"Created zip file: {zip_path}") else: st.warning("No files found on this page.") else: st.warning("No search results found.") asyncio.run(run_search()) else: # PDF Summarizer mode if summarizer is None: st.error("PDF summarization is not available due to model loading errors.") else: st.header("PDF Summarizer") pdf_url = st.text_input("Enter PDF URL") if st.button("Summarize"): if pdf_url: with st.spinner("Generating summary..."): summary = summarize_pdf_url(pdf_url) st.write("Summary:") st.write(summary) if __name__ == "__main__": try: main() except Exception as e: st.error(f"An error occurred: {str(e)}") logger.error(f"Application error: {str(e)}", exc_info=True)