import time
import getpass
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException

# --- Configuration ---
COMPANY_URL = "https://www.linkedin.com/company/euipo/posts/?feedView=all"
OUTPUT_FILE = "linkedin_post_links.xlsx"
SCROLL_PAUSE_TIME = 10  # Increased wait time for better loading
TARGET_LINKS = 200     # Minimum number of links to collect
MAX_SCROLLS = 50       # Maximum number of scrolls to prevent infinite loops

def login_to_linkedin(driver, email, password):
    """Navigates to LinkedIn login page and signs in."""
    print("Navigating to LinkedIn login page...")
    driver.get("https://www.linkedin.com/login")
    time.sleep(2)

    try:
        print("Entering credentials...")
        email_field = driver.find_element(By.ID, "username")
        email_field.send_keys(email)

        password_field = driver.find_element(By.ID, "password")
        password_field.send_keys(password)
        password_field.send_keys(Keys.RETURN)
        print("Login successful.")
        time.sleep(5)
    except NoSuchElementException:
        print("Could not find login fields. The page structure may have changed.")
        driver.quit()
        exit()

def scrape_post_links(driver, url):
    """Scrolls dynamically until target number of links is reached."""
    print(f"Navigating to company page: {url}")
    driver.get(url)
    time.sleep(10)  # Longer initial load time

    post_links = set()
    scroll_count = 0
    consecutive_no_new = 0
    max_consecutive = 5  # Allow more attempts before stopping

    print(f"Starting collection (target: {TARGET_LINKS} links)...")
    
    while len(post_links) < TARGET_LINKS and scroll_count < MAX_SCROLLS:
        # Get current posts
        current_posts = driver.find_elements(By.CSS_SELECTOR, "div[data-urn*='urn:li:activity:']")
        previous_count = len(post_links)

        # Process posts
        for post in current_posts:
            try:
                urn = post.get_attribute("data-urn")
                if urn:
                    full_link = f"https://www.linkedin.com/feed/update/{urn}"
                    post_links.add(full_link)
            except Exception as e:
                print(f"Error processing post: {e}")

        # Check for progress
        new_count = len(post_links)
        if new_count == previous_count:
            consecutive_no_new += 1
            print(f"No new posts in scroll #{scroll_count}. Consecutive: {consecutive_no_new}")
        else:
            consecutive_no_new = 0
            print(f"Added {new_count - previous_count} new posts. Total: {new_count}/{TARGET_LINKS}")

        # Scroll down
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        scroll_count += 1
        print(f"Scroll #{scroll_count} completed")

        # Wait for content to load
        time.sleep(SCROLL_PAUSE_TIME)

        # Check stop conditions
        if consecutive_no_new >= max_consecutive:
            print(f"Stopping - No new posts for {max_consecutive} consecutive scrolls")
            break

    print(f"\nFinal count: {len(post_links)} unique posts found")
    return list(post_links)

def save_to_excel(links, filename):
    """Saves links to Excel file."""
    if not links:
        print("No links found to save")
        return
        
    print(f"Saving {len(links)} links to {filename}...")
    df = pd.DataFrame(links, columns=['Post URL'])
    df.to_excel(filename, index=False)
    print("File saved successfully.")

if __name__ == "__main__":
    linkedin_email = input("Enter your LinkedIn Email: ")
    linkedin_password = getpass.getpass("Enter your LinkedIn Password: ")

    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service)
    driver.implicitly_wait(10)

    try:
        login_to_linkedin(driver, linkedin_email, linkedin_password)
        
        # Handle security check
        if "feed" not in driver.current_url and "login" in driver.current_url:
            print("\nPlease complete security check in browser window")
            input("Press Enter after logging in manually...")

        all_links = scrape_post_links(driver, COMPANY_URL)
        save_to_excel(all_links, OUTPUT_FILE)

    finally:
        print("Closing browser")
        driver.quit()


ok, now I need a code that will fetch the body text of the post from each of the posts, the code first need to open the post and fetch the body text of the post