import re
import os

import pandas as pd

# creating a variable for identifying the folder containing the documents

PATH_TO_MAGAZINES = r"C:\...\AbbyFineReader_Digitized_Issues"

# listdir for getting a list of all the documents inside the folder

magazines= os.listdir(PATH_TO_MAGAZINES)
print(magazines)

# Creating an empty table organized according to Title, Author, Magazine and Issue

Spanish_Magazines = pd.DataFrame()
for magazine in magazines:

    base_name = os.path.splitext(magazine)[0]

    magazine_name = base_name.split("_")[0]
    magazine_issue = base_name.split("_")[1]
    magazine_year = base_name.split("_")[2]


    print("Magazine Name: ", magazine_name, "Magazine Issue :", magazine_issue)


    file = os.path.join(PATH_TO_MAGAZINES, magazine)

    with open(file, "r", encoding="utf-8") as f:
        
        # Open the file in read-only mode with utf-8 encoding
        text = f.read()

        # Define regex (regular expressions) patterns

        """
        ^_: Matches lines starting with a specific character.
        .*: Matches any characters after the initial character.
        [^_]$: Ensures the line does not end with the specific character.
        """
        title_pattern = r"^_.*[^_]$"
        author_pattern = r"_(.*?)_"  # Matches specific pattern within underscores
        page_pattern = r"Page (\d+)"  # Matches the page number

        # Extract data

        titles = re.findall(title_pattern, text, re.MULTILINE)

        # Erase unwanted characters at the beginning of titles

        titles = [title[1:] for title in titles]

        authors = re.findall(author_pattern, text, re.MULTILINE)
        pages = re.findall(page_pattern, text)

        # Print the extracted titles
        for title in titles:
            print(title)

        for author in authors:
            print(author)

        # Combining results into a structured format (table)

        results = {"Author": authors, "Title":titles }
        temp_data = pd.DataFrame(results)
        temp_data["Magazine"] = magazine_name
        temp_data["Issue"] = magazine_issue
        temp_data["Year"] = magazine_year


        Spanish_Magazines = pd.concat([Spanish_Magazines, temp_data])

        
        # Apply a simple function to handle missing or incorrect values
        
Spanish_Magazines["Author"] = Spanish_Magazines["Author"].apply(lambda x: x if x !="0" else "Anonymous")

Spanish_Magazines["Title"] = Spanish_Magazines["Title"].apply(lambda x: x if x !="0" else "Untitled")

print(Spanish_Magazines)

# Specify the path where you want to save the CSV file
output_file_path = r"C:\...\Spanish_Magazines_Index.csv"

# Save the DataFrame to a CSV file
Spanish_Magazines.to_csv(output_file_path, index=False, encoding='utf-8')
