import pandas as pd
from tqdm import tqdm
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from concurrent.futures import ThreadPoolExecutor

# Read the CSV files with encoding
# top_domains_df = pd.read_csv('Top1MillionDomians.csv', encoding='utf-8', dtype=str)
malware_urls_df = pd.read_csv('Malurl1.csv', encoding='utf-8', dtype=str)

# Get the first column from each DataFrame
# top_domains = top_domains_df.iloc[:, 0].tolist()
malware_urls = malware_urls_df.iloc[:, 0].tolist()

# Initialize Chrome options
chrome_options = Options()
#chrome_options.add_argument('--headless')
###chrome_options.add_argument('--disable-gpu')
##chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument('--disable-dev-shm-usage')

chrome_options.headless = True

# Function to process a URL
def process_url(url):
    try:
        driver = webdriver.Chrome(options=chrome_options)
        driver.set_page_load_timeout(15)  # Maximum 15 seconds per URL
        driver.get(url)
        # Add your processing logic here
        driver.quit()
        return url, True
    except:
        driver.quit()
        return url, False

def perform_request(url):
    try:
        response = requests.get("http://" + url, timeout=5)
        return url, response.status_code == 200
    except Exception as e:
        print(f"Error processing URL {url}: {e}")
        return url, False

def process_with_progress(url, pbar):
    result = perform_request(url)
    update_progress_bar(result, pbar)
    return result

def update_progress_bar(result, pbar):
    url, success = result
    if success:
        pbar.set_description(f"Processing: {url} (Success)")
    else:
        pbar.set_description(f"Processing: {url} (Error)")
    pbar.update(1)
    print(f"Processed URL: {url} (Success: {success})")

if __name__ == '__main__':
    batch_size = 500
    num_processes = 4  # Number of parallel processes
    
    with tqdm(total=len(malware_urls), desc="Processing URLs") as pbar:
        with ThreadPoolExecutor(max_workers=num_processes) as executor:
            futures = []
            for url in malware_urls:
                future = executor.submit(process_with_progress, url, pbar)
                futures.append(future)
            
            for future in futures:
                future.result()
    
    print("All URLs processed.")