import os
import csv
import pandas as pd
from playwright.sync_api import sync_playwright

APPLICATION_PAGE_URL = "https://iprsearch.ipindia.gov.in/PublicSearch/PublicationSearch/ApplicationStatus"
PDF_DIRECTORY = "applications_pdfs"

def scrape_application_data(application_number):
    """
    Scrape application data for a single application number.
    """
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)  
        page = browser.new_page()

        try:
            print(f"Visiting the website for application number: {application_number}")
            page.goto(APPLICATION_PAGE_URL, wait_until="domcontentloaded", timeout=60000)

            page.wait_for_selector('input[name="ApplicationNumber"]', timeout=60000)
            page.fill('input[name="ApplicationNumber"]', application_number)

            print("Please enter the CAPTCHA manually in the browser and submit the form.")

            # Wait for the page to navigate after CAPTCHA submission
            page.wait_for_url("https://iprsearch.ipindia.gov.in/PatentSearch/PatentSearch/ViewApplicationStatus", timeout=90000)

            if page.locator("text=Sorry, we can't load the resource").is_visible():
                print(f"Failed to load data for application number {application_number}: Sorry, we can't load the resource")
                return {"Application Number": application_number, "Status": "Resource Not Loaded"}

            print(f"Successfully loaded application status page for application number {application_number}.")

            # Extract details
            application_number_text = safe_extract_text(page.locator('td:has-text("APPLICATION NUMBER") + td'))
            application_type = safe_extract_text(page.locator('td:has-text("APPLICATION TYPE") + td'))
            date_of_filing = safe_extract_text(page.locator('td:has-text("DATE OF FILING") + td'))
            applicant_name = safe_extract_text(page.locator('td:has-text("APPLICANT NAME") + td strong'))
            title_of_invention = safe_extract_text(page.locator('td:has-text("TITLE OF INVENTION") + td'))
            field_of_invention = safe_extract_text(page.locator('td:has-text("FIELD OF INVENTION") + td'))
            email = safe_extract_text(page.locator('td:has-text("E-MAIL (As Per Record)") + td'))
            pct_international_application_number = safe_extract_text(page.locator('td:has-text("PCT INTERNATIONAL APPLICATION NUMBER") + td'))
            priority_date = safe_extract_text(page.locator('td:has-text("PRIORITY DATE") + td'))

            # Return the extracted details
            return {
                'Application Number': application_number_text,
                'Application Type': application_type,
                'Date of Filing': date_of_filing,
                'Applicant Name': applicant_name,
                'Title of Invention': title_of_invention,
                'Field of Invention': field_of_invention,
                'Email': email,
                'PCT International Application Number': pct_international_application_number,
                'Priority Date': priority_date
            }

        except Exception as e:
            print(f"Error during scraping for application number {application_number}: {e}")
            return {"Application Number": application_number, "Status": "Error", "Error Message": str(e)}

        finally:
            browser.close()

def safe_extract_text(locator, default='N/A'):
    """
    Safely extract text from a Playwright locator.
    """
    try:
        return locator.inner_text().strip()
    except Exception:
        return default

def save_to_csv(data, filename):
    """
    Append data to a CSV file.
    """
    if data:
        file_exists = os.path.isfile(filename)
        with open(filename, mode='a', newline='', encoding='utf-8') as file:
            writer = csv.DictWriter(file, fieldnames=data.keys())
            if not file_exists:
                writer.writeheader()
            writer.writerow(data)

if __name__ == "__main__":
    input_file = r"C:\Users\Administrator\Desktop\webscrapper\o1mini\application1.xlsx"
    output_file = "data3.csv"

    print("Reading application numbers from Excel file...")
    df = pd.read_excel(input_file)

    # Make sure there is a column for Application Numbers
    if 'Application number' not in df.columns:
        print("Column 'Application number' not found in Excel file.")
        exit()

    # Process each application number
    for idx, row in df.iterrows():
        application_number = str(row['Application number'])  # Change this to your specific column name
        print(f"\nProcessing application number: {application_number}")

        application_data = scrape_application_data(application_number)
        print(f"Saving extracted application data for application number {application_number}...")
        save_to_csv(application_data, output_file)   this code is extarcting only No., Application type,Application Date , Applicant Name , Title of invention,Field of invention,email,PCT international App no., Priority Date 
N/A,PCT NATIONAL PHASE APPLICATION,01/03/2016,XIAOMI INC.,METHOD AND APPARATUS FOR INPUT PROCESSING,COMPUTER SCIENCE,,PCT/CN2015/099308,23/11/2015
N/A,PCT NATIONAL PHASE APPLICATION,09/02/2016,XIAOMI INC.,METHOD AND DEVICE FOR DISPLAYING A TARGER OBJECT,COMPUTER SCIENCE,info@iprattorneys.com,PCT/CN2015/099296,19/08/2015
N/A,PCT NATIONAL PHASE APPLICATION,02/02/2016,XIAOMI INC.,METHOD DEVICE AND SYSTEM FOR SETTING EQUIPMENT WORKING STATE,COMMUNICATION,,PCT/CN2015/099311,29/10/2015  these columns ,,..i wanted all  <table class="table-striped" border="0" cellpadding="0" cellspacing="4" width="100%">
                    <tbody>
                        <tr>
                            <th colspan="2" style="text-align: center">Application Details</th>

                        </tr>

                        <tr>
                            <td style="width: 250px">APPLICATION NUMBER</td>
                            <td>

                                201627007186             
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 250px">APPLICATION TYPE</td>
                            <td>

                                PCT NATIONAL PHASE APPLICATION
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 250px">DATE OF FILING</td>
                            <td>
                                01/03/2016
                            </td>
                        </tr>
                            <tr>
                                <td style="width: 250px">APPLICANT NAME</td>
                                <td>
                                        <strong>XIAOMI INC.
                                            <br />
                                        </strong>  

                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">TITLE OF INVENTION</td>
                                <td>METHOD AND APPARATUS FOR INPUT PROCESSING  </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">FIELD OF INVENTION</td>
                                <td>

                                    COMPUTER SCIENCE
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">E-MAIL (As Per Record)</td>
                                <td>
                                    
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">ADDITIONAL-EMAIL (As Per Record)</td>
                                <td>
                                    info@iprattorneys.com
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">E-MAIL (UPDATED Online)</td>
                                <td>
                                    <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">PCT INTERNATIONAL APPLICATION NUMBER</td>
                                <td>PCT/CN2015/099308   </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">PCT INTERNATIONAL FILING DATE</td>
                                <td>29/12/2015</td>
                            </tr>
                            <tr>
                                <td style="width: 250px">PRIORITY DATE</td>
                                <td>23/11/2015</td>
                            </tr>
                            <tr>
                                <td style="width: 250px">REQUEST FOR EXAMINATION DATE</td>
                                    <td>01/03/2016</td>
                            </tr>
                            <tr>
                                <td style="width: 250px">PUBLICATION DATE (U/S 11A)</td>
                                <td>10/11/2017</td>
                            </tr>
                            <tr>
                                <td style="width: 250px">FIRST EXAMINATION REPORT DATE</td>
                                <td>
                                    24/12/2019
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">Date Of Certificate Issue</td>
                                <td>
                                    17/04/2023
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 250px">POST GRANT JOURNAL DATE</td>
                                <td>21/04/2023</td>
                            </tr>
                            <tr>
                                <td style="width: 250px">REPLY TO FER DATE</td>
                                <td>25/02/2020</td>
                            </tr>

                    </tbody>
                </table>  of them