from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.waits import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Set up the webdriver
driver = webdriver.Chrome()

# Navigate to the site
driver.get("https://search.libraryofleaks.org/")

# Get search term from user
search_term = input("Introduceți termenul de căutare: ")

# Find the search input field and enter the search term
search_input = driver.find_element(By.ID, "search-input")
search_input.send_keys(search_term)

# Find the search button and click it
search_button = driver.find_element(By.ID, "search-button")
search_button.click()

# Wait for the results to load
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "results")))

# Define desired file extensions
desired_extensions = [".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt", ".csv"]

# Function to get file links from the current page
def get_file_links():
    file_links = []
    # Assuming that the results are in a div with class "results"
    results_div = driver.find_element(By.CLASS_NAME, "results")
    # Assuming that each result is in a div with class "result"
    for result in results_div.find_elements(By.CLASS_NAME, "result"):
        # Assuming that the link is in an a tag
        link = result.find_element(By.TAG_NAME, "a").get_attribute("href")
        # Check if the link has any of the desired file extensions
        if any(link.endswith(ext) for ext in desired_extensions):
            file_links.append(link)
    return file_links

# Function to get the next page link
def get_next_page_link():
    try:
        next_button = driver.find_element(By.CLASS_NAME, "next-page")
        return next_button.get_attribute("href")
    except:
        return None

# Collect all file links
all_file_links = []
current_page = driver.current_url
while current_page:
    file_links = get_file_links()
    all_file_links.extend(file_links)
    next_page_link = get_next_page_link()
    if next_page_link:
        driver.get(next_page_link)
        time.sleep(2)
        current_page = driver.current_url
    else:
        break

# Print the file links
for link in all_file_links:
    print(link)

# Close the browser
driver.quit()
