import asyncio
from playwright.async_api import async_playwright
import re
import csv

# Login credentials
LOGIN_URL = "https://example.com/login"  # Change this
TARGET_URL = "https://example.com/emails-page"  # Change this
USERNAME = "your_username"
PASSWORD = "your_password"

# Extract emails from text
def extract_emails(text):
    email_regex = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
    return set(re.findall(email_regex, text))

# Save emails to CSV
def save_emails_to_csv(emails, filename="emails.csv"):
    with open(filename, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["Email Addresses"])
        for email in emails:
            writer.writerow([email])
    print(f"Emails saved to {filename}")

async def scrape_emails():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)  # Change to False to see the browser
        page = await browser.new_page()

        # Step 1: Login
        await page.goto(LOGIN_URL)
        await page.fill("input[name='username']", USERNAME)
        await page.fill("input[name='password']", PASSWORD)
        await page.click("button[type='submit']")  # Adjust if needed
        await page.wait_for_load_state("networkidle")

        # Step 2: Navigate to target page
        await page.goto(TARGET_URL)
        await page.wait_for_load_state("networkidle")

        all_emails = set()

        while True:
            # Extract emails from current page
            content = await page.content()
            emails = extract_emails(content)
            all_emails.update(emails)

            print(f"Found {len(emails)} emails on this page.")

            # Try to click "Next" button for pagination
            try:
                next_button = await page.query_selector("a.next")  # Update selector if needed
                if next_button:
                    await next_button.click()
                    await page.wait_for_load_state("networkidle")
                else:
                    break  # No next page, exit loop
            except:
                break  # No next button found, exit loop

        await browser.close()

        # Save emails
        save_emails_to_csv(all_emails)
        print(f"\nTotal emails scraped: {len(all_emails)}")

# Run the scraper
asyncio.run(scrape_emails())
